Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

The reverse of mktime()

656 views
Skip to first unread message

Guillaume Dargaud

unread,
Apr 2, 2012, 7:45:04 AM4/2/12
to
Hello all,
in following with my thread from last week about timezone conversions, I
decided to use UTC for time references. But there's still something wrong.

I have a time string, which I use to load a struct tm and then convert to a
time_t. First problem is that mktime uses local time. I thought, OK, I'll
just use it as if it was a UTC time, no difference.
And 2nd problem, if I call localtime to convert back, I'm off by one hour
and that doesn't make sense to me:

struct tm TM={0}, *TM2;
char *Pos="20120329-111359.dat"
sscanf (Pos, "%04d%02d%02d-%02d%02d%02d",
&TM.tm_year, &TM.tm_mon, &TM.tm_mday, &TM.tm_hour, &TM.tm_min, &TM.tm_sec);
TM.tm_year-=1900;
TM.tm_mon--;

Time=mktime(&TM); // tm_hour==11
TM2 = localtime (&Time); // tm_hour==12 - Doesn't make sense

Aren't mktime and localtime converse functions ?!?
--
Guillaume Dargaud
http://www.gdargaud.net/

Azazel

unread,
Apr 2, 2012, 8:31:18 AM4/2/12
to
On 2012-04-02, Guillaume Dargaud wrote:
> in following with my thread from last week about timezone conversions,
> I decided to use UTC for time references. But there's still something
> wrong.
>
> I have a time string, which I use to load a struct tm and then convert
> to a time_t. First problem is that mktime uses local time. I thought,
> OK, I'll just use it as if it was a UTC time, no difference.
> And 2nd problem, if I call localtime to convert back, I'm off by one
> hour and that doesn't make sense to me:
>
> struct tm TM={0}, *TM2;
> char *Pos="20120329-111359.dat"
> sscanf (Pos, "%04d%02d%02d-%02d%02d%02d",
> &TM.tm_year, &TM.tm_mon, &TM.tm_mday, &TM.tm_hour, &TM.tm_min,
> &TM.tm_sec);
> TM.tm_year-=1900;
> TM.tm_mon--;

TM.tm_isdst == 0

> Time=mktime(&TM); // tm_hour==11
> TM2 = localtime (&Time); // tm_hour==12 - Doesn't make sense

TM2->tm_isdst == 1

--
Az.

Azazel

unread,
Apr 2, 2012, 8:47:50 AM4/2/12
to
Meant also to say that if you set TM.tm_isdst to -1 mktime should
attempt to work for itself whether DST is in effect:

[azazel@kadath:~/tmp] $ cat test.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int
main (int argc, char **argv)
{
struct tm TM = { 0 };

char *Pos="20120329-111359";
sscanf (Pos, "%04d%02d%02d-%02d%02d%02d",
&TM.tm_year,
&TM.tm_mon,
&TM.tm_mday,
&TM.tm_hour,
&TM.tm_min,
&TM.tm_sec);

TM.tm_year -= 1900;
TM.tm_mon --;
TM.tm_isdst = -1;

time_t Time = mktime (&TM);
struct tm *TM2 = localtime (&Time);

printf ("%d, %d\n", TM.tm_hour, TM2->tm_hour);
return 0;
}
[azazel@kadath:~/tmp] $ ./test
11, 11

--
Az.

Guillaume Dargaud

unread,
Apr 2, 2012, 8:47:59 AM4/2/12
to
> TM.tm_isdst == 0
>
>> Time=mktime(&TM); // tm_hour==11
>> TM2 = localtime (&Time); // tm_hour==12 - Doesn't make sense
>
> TM2->tm_isdst == 1
>

Hah, OK. Then I need to change it like this:

Time=mktime(&TM);
TM2 = localtime (&Time);
if (TM2->tm_isdst==1) Time+=3600;

Thanks
0 new messages