Fix for mktime() issue

Holger Weiß holger at CIS.FU-Berlin.DE
Sat Mar 20 02:33:44 CET 2010


* Albrecht Dreß <albrecht.dress at arcor.de> [2010-03-18 19:29]:
> <snip>
>         t=localtime((time_t *)&test_time);
>         test_time_year=t->tm_year;
>         test_time_mon=t->tm_mon;
>         test_time_mday=t->tm_mday;
>         test_time_wday=t->tm_wday;
>
>         /* calculate the start of the day (midnight, 00:00 hours) when the specified test time occurs */
>         t->tm_sec=0;
>         t->tm_min=0;
>         t->tm_hour=0;
>         /* Removed for the moment. This fixes a bug where the timeperiod is incorrectly calculated */
>         /* See t-tap/test_timeperiods for a test failure */
>         /* t->tm_isdst=-1; */
>         midnight=(unsigned long)mktime(t);
> </snip>
>
> Regarding the tm_isdst field, this code "initialises" it implicitly by
> calling localtime() but then assumes for the calculation of midnight
> that the dst setting is still correct which may be plain wrong.
>
> Example: test_time is 1269734400 (Mar 28 01:00:00 2010 CET), midnight
> will correctly be calculated as Mar 28 00:00:00 2010.  If test_time is
> 1269763200 (Mar 28 10:00:00 2010 CEST), midnight is calculated as Mar 27
> 23:00:00 2010 which is obviously wrong.  This miscalculation is fixed,
> again, by properly initialising tm_isdst to -1.
>
> Thus, either the "tests" missed the dst change cases, or the real use of
> midnight is not midnight but something else.

The real use of the midnight value is not "12 o'clock at night," but
"the zero point of the local time of the day," which differs from "12
o'clock at night" if tm_isdst changed since 12 o'clock at night.  That
is, the offset of 10:00 AM from this zero point is 36,000 seconds,
regardless of whether tm_isdst changed since 12 o'clock at night.

> In the latter case, the fix is of course to re-write the time span
> calculation to work properly (taking into account that days may also be
> 23 or 25 hours long, when dst changes)

What makes you believe that the code in question does not work properly?

Holger

------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev




More information about the Developers mailing list