Skupiny Google už nepodporují nová předplatná ani příspěvky Usenet. Historický obsah lze zobrazit stále.

The reverse of mktime()

650 zobrazení
Přeskočit na první nepřečtenou zprávu

Guillaume Dargaud

nepřečteno,
2. 4. 2012 7:45:0402.04.12
komu:
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

nepřečteno,
2. 4. 2012 8:31:1802.04.12
komu:
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

nepřečteno,
2. 4. 2012 8:47:5002.04.12
komu:
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

nepřečteno,
2. 4. 2012 8:47:5902.04.12
komu:
> 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 nových zpráv