Problem with mktime()

Jul 25, 2014 at 6:17pm
Hi guys, I'm now writing a program on my RedHat Linux Machine using gcc compiler, which will accept two dates as input and compute the difference between them.

However, I found a strange behavior with the mktime() as illustrated with below code segment.
=========
struct tm t;
t.tm_year=4;
t.tm_mon=9;
t.tm_day=30;
t.tm_hour=0;
t.tm_min=0;
t.tm_sec=0;

printf("%d-%d-%d %d:%d:%d\n", t.tm_year, t.tm_mon, t.tm_day, t.tm_hour, t.tm_min, t.tm_sec);
//result: 4-9-30 0:0:0, which means 1904 Oct 30 00:00:00

mktime(&t);

printf("%d-%d-%d %d:%d:%d\n", t.tm_year, t.tm_mon, t.tm_day, t.tm_hour, t.tm_min, t.tm_sec);
//result: 4-9-29 23:36:36, which means 1904 Oct 29 23:36:36
=========

As you can see the struct tm was shifted after mktime() which looks unreasonable.

I have done a lot of testing, and I found this strange behavior happens only for date assignment between 1904 Oct 30 00:00:00 - 00:23:24.

Besides, I have tested the same codes in my unix server (HP-UX), and it runs perfectly without any shifting.

I wonder if anyone have similar experience before, if anyone can give me hints on what's going on?
Jul 25, 2014 at 6:47pm
If the values are outside their normal ranges mktime will adjust all the values to their proper ranges. The tm struct does not have a member called tm_day (or at least it's standard). There are tm_mday, tm_wday and tm_yday. mktime only care about tm_mday and ignores tm_wday and tm_yday.
Last edited on Jul 25, 2014 at 6:50pm
Topic archived. No new replies allowed.