Pelles C forum

Pelles C => Bug reports => Topic started by: JohnF on October 26, 2010, 09:22:33 AM

Title: localtime bug
Post by: JohnF on October 26, 2010, 09:22:33 AM
I've tried this with two other compilers which both give the correct time. It was ok until recently, it looks like the clock has gone back early by 1 hour.

Code: [Select]
#include <stdio.h>
#include <time.h>

int main(void)
{
time_t now = time(NULL);
struct tm * ltm = localtime(&now);

printf("%0.2d:%0.2d:%0.2d\n",
ltm->tm_hour,
ltm->tm_min,
ltm->tm_sec);
return 0;
}

John
Title: Re: localtime bug
Post by: TimoVJL on October 26, 2010, 10:03:05 AM
daylight saving is 0 ???

Code: [Select]
#include <stdio.h>
#include <time.h>

int main(void)
{
time_t now = time(NULL);
struct tm * ltm = localtime(&now);

printf("%0.2d:%0.2d:%0.2d %d\n",
ltm->tm_hour,
ltm->tm_min,
ltm->tm_sec,
ltm->tm_isdst);
return 0;
}

http://24timezones.com (http://24timezones.com)
Title: Re: localtime bug
Post by: JohnF on October 26, 2010, 10:39:57 AM
Yes, clocks should go back an hour on the last Sunday in October.

John
Title: Re: localtime bug
Post by: CommonTater on October 26, 2010, 11:03:23 AM
Yes, clocks should go back an hour on the last Sunday in October.

John


Except in Canada and the USA where it's the second sunday in March until the first Sunday in November.
Title: Re: localtime bug
Post by: JohnF on October 26, 2010, 11:15:12 AM
Yes, clocks should go back an hour on the last Sunday in October.

John


Except in Canada and the USA where it's the second sunday in March until the first Sunday in November.


I don't live in either of those places! :)

John
Title: Re: localtime bug
Post by: CommonTater on October 26, 2010, 01:05:32 PM
I don't live in either of those places! :)

John


Unfortunately, I do.
Title: Re: localtime bug
Post by: Pelle on October 27, 2010, 03:00:08 PM
AFAIK, nothing directly changed in this area for a long time. I get the same (wrong) result with 6.0. I need to investigate...
Title: Re: localtime bug
Post by: JohnF on October 27, 2010, 08:36:49 PM
AFAIK, nothing directly changed in this area for a long time. I get the same (wrong) result with 6.0. I need to investigate...

Thanks.

John
Title: Re: localtime bug
Post by: AlexN on October 28, 2010, 07:26:31 AM
I don't know, if it help anyone, but GetLocalTime() seems to work correct for me.
Code: [Select]
#include <windows.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
SYSTEMTIME lt;

GetLocalTime(&lt);
printf("%4d-%02d-%02d %2d:%02d:%02d\n",
lt.wYear, lt.wMonth, lt.wDay,
lt.wHour, lt.wMinute, lt.wSecond);

return 0;
}
Title: Re: localtime bug
Post by: AlexN on October 28, 2010, 08:23:39 AM
I think, it must be an error in Pelles runtime library, because if you link to the program the library msvcrt.lib the result is correct for me.
Title: Re: localtime bug
Post by: Vortex on October 28, 2010, 11:01:13 AM
The assembly translation below is linked against msvcrt.lib and works fine :

Code: [Select]
.386
.model flat,stdcall
option casemap:none

include     LocalTime.inc

.data

msg         db '%4d-%02d-%02d %2d:%02d:%02d',13,10,0


.code

start:

    call    main
    invoke  ExitProcess,0

main PROC USES esi

LOCAL LocalTime:SYSTEMTIME

    lea     esi,LocalTime
    invoke  GetLocalTime,esi

    invoke  printf,ADDR msg,\
            SYSTEMTIME.wYear[esi], SYSTEMTIME.wMonth[esi],\
            SYSTEMTIME.wDay[esi],SYSTEMTIME.wHour[esi],\
            SYSTEMTIME.wMinute[esi],SYSTEMTIME.wSecond[esi]
    ret

main ENDP

END start
Title: Re: localtime bug
Post by: Pelle on October 29, 2010, 02:18:54 PM
It was a problem with GetTimeZoneInformation(), day-in-month format, and the fact that DST ends in Europe on the 31st (October) this year. Should be fixed now (time zones in Windows is a royal mess, so you never really know...).
Title: Re: localtime bug
Post by: Vortex on October 31, 2010, 11:59:25 AM
Another version linked against pocrt.lib