Question

I am making a calendar program. The 'expand repeating events' code is giving me no end of trouble. I am expanding events by using mktime() to get a 'pure' time value, then adding the repeat delta (in this case, 604800 seconds or 7 days) to it. localtime() is then used to get a calendar struct back out.

This happens:

Original event: September 10

{tm_sec = 0, tm_min = 0, tm_hour = 16, tm_mday = 10, tm_mon = 9,
 tm_year = 2012, tm_wday = 4, tm_yday = 283, tm_isdst = 0, 
 tm_gmtoff = -25200, tm_zone = 0x608ff0 "PDT"}

First repetition: September 17

{tm_sec = 0, tm_min = 0, tm_hour = 17, tm_mday = 17, tm_mon = 9,
 tm_year = 2012, tm_wday = 4, tm_yday = 290, tm_isdst = 1, 
 tm_gmtoff = -25200, tm_zone = 0x608ff0 "PDT"}

Second repetition: September 24

{tm_sec = 0, tm_min = 0, tm_hour = 16, tm_mday = 24, tm_mon = 9,
 tm_year = 2012, tm_wday = 4, tm_yday = 297, tm_isdst = 0,
 tm_gmtoff = -25200, tm_zone = 0x608ff0 "PDT"}

Third repetition: September 31?!

{tm_sec = 0, tm_min = 0, tm_hour = 16, tm_mday = 31, tm_mon = 9,
 tm_year = 2012, tm_wday = 4, tm_yday = 304, tm_isdst = 0,
 tm_gmtoff = -25200, tm_zone = 0x608ff0 "PDT"}

Does anyone have any idea what's going on here? Will I have to fill in for localtime() myself?

Was it helpful?

Solution

From the documentation: the members of the struct tm structure are 0-based (as usually in C).

int    tm_mon   month of year [0,11]

So actually the month numbered 9 is the 10th month, which is October, and it has 31 days.

OTHER TIPS

As H2CO3 pointed out, tm_mon is 0-based, so the date was actually October 31st, which makes sense.

I wanted to mention that simply adding 604800 seconds to advance 7 days is probably not what a user would want from a calendar program. 604800 is exactly seven days, but if the user wants a recurring event at 10:00 AM every seven days, adding 604800 seconds will not always result in a time at 10:00 AM because this ignores special considerations like Daylight Saving Time and leap seconds.

What you can do instead is add 7 to tm_mday. Don't worry about exceeding the number of days in the month because mktime() will correct it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top