NO

Author Topic: localtime bug  (Read 6573 times)

JohnF

  • Guest
localtime bug
« 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

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: localtime bug
« Reply #1 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
« Last Edit: October 26, 2010, 10:24:37 AM by timovjl »
May the source be with you

JohnF

  • Guest
Re: localtime bug
« Reply #2 on: October 26, 2010, 10:39:57 AM »
Yes, clocks should go back an hour on the last Sunday in October.

John

CommonTater

  • Guest
Re: localtime bug
« Reply #3 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.

JohnF

  • Guest
Re: localtime bug
« Reply #4 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

CommonTater

  • Guest
Re: localtime bug
« Reply #5 on: October 26, 2010, 01:05:32 PM »
I don't live in either of those places! :)

John


Unfortunately, I do.

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: localtime bug
« Reply #6 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...
/Pelle

JohnF

  • Guest
Re: localtime bug
« Reply #7 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

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: localtime bug
« Reply #8 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;
}
best regards
 Alex ;)

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: localtime bug
« Reply #9 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.
best regards
 Alex ;)

Offline Vortex

  • Member
  • *
  • Posts: 802
    • http://www.vortex.masmcode.com
Re: localtime bug
« Reply #10 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
Code it... That's all...

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: localtime bug
« Reply #11 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...).
/Pelle

Offline Vortex

  • Member
  • *
  • Posts: 802
    • http://www.vortex.masmcode.com
Re: localtime bug
« Reply #12 on: October 31, 2010, 11:59:25 AM »
Another version linked against pocrt.lib
Code it... That's all...