views:

65

answers:

1

I would like to determine the next date that has a day of the week value equal to something I specify.

For example, today is 6/21/2010 and the day of week value is 1 b/c today is a Monday. How do I find the next date with let's say a day of week value of 3.

I would like to consider all cases that come close to the end of something including month and year.

Is there any easy way to do this or do I have to manually check to see if it's close to the end of the month or year and make adjustments accordingly? I feel there is an easily and better solution than doing that.

A: 

You can stuff your values into a struct tm, and then normalize them with a call to mktime. This will ignore the day of week and day of year on input, and normalize the other values. For example, you can feed it an input of February 31st, and it'll convert that to March 3rd (or March 2nd for a leap year).

To use this, the normal sequence would be to call time to get the current time as a time_t. Then convert that to a struct tm with a call to localtime. Adjust the time in the struct tm as needed (e.g., if it says today is Tuesday, and you want Friday, add three to the day of the month). Then call mktime to normalize the values, in case you've (for example) overflowed from one week/month/year to the next.

Jerry Coffin