Thread View: borland.public.cppbuilder.nativeapi
2 messages
2 total messages
Started by dave_c
Fri, 14 Mar 2008 14:35
time.h bug?
Author: dave_c
Date: Fri, 14 Mar 2008 14:35
Date: Fri, 14 Mar 2008 14:35
52 lines
2490 bytes
2490 bytes
Hello. I'm having trouble with some time.h functions. the result is different for different c++ builder versions. I have the following code: //--------------------------------------------------------------------- #include "time.h" #include "stdio.h" //... long t = time(0); struct tm *ltime = localtime(&t); char timestr[16]; strftime( timestr, 16 , "%H:%M:%S", ltime); char ResultBuffer[1000]; sprintf(ResultBuffer,"%s (%u) dst=%u", timestr, t,ltime->tm_isdst); Edit1->Text = ResultBuffer; //--------------------------------------------------------------------- When executing this code in Borland C++Builder 6 and Codegear C++Builder 2007 I get different results: C++Builder 6: 14:24:40 (1205501080) dst=0 C++Builder 2007: 14:24:40 (1205497480) dst=1 So the result (in local time) is the same, but I'm using the value from time(0) (which in this case is 1205501080 or 1205497480) to do calculations compared to a time I receive from an external DLL. in C++Builder 6 the time calculation was correct, but in C++ Builder 2007 I get calculation errors of 3600 seconds. It seems that the time(0) function in C++Builder 2007 seems to return the local time, instead of the GMT time? I don't know what has changed in the libraries, so I don't know if a fix now will still work within 6 months or next year. What correction do I have to do to make the calculations work properly? I am in timezone GMT+1:00 (so my _timezone has a value of -3600) Can someone please clarify this for me? Regards, Dave
Re: time.h bug?
Author: Bob Gonder
Date: Fri, 14 Mar 2008 17:25
Date: Fri, 14 Mar 2008 17:25
48 lines
2376 bytes
2376 bytes
dave_c wrote: >When executing this code in Borland C++Builder 6 and Codegear C++Builder > 2007 I get different results: > >C++Builder 6: >14:24:40 (1205501080) dst=0 > >C++Builder 2007: >14:24:40 (1205497480) dst=1 The values you show are the same. Only difference is the DST adjustment. Just guessing here... When BCB6 was written, DST started in April. RAD2007 has been patched to understand that DST started last week, and may change on the whim of the powers that be. >So the result (in local time) is the same, but I'm using the value from >time(0) (which in this case is 1205501080 or 1205497480) to do >calculations compared to a time I receive from an external DLL. in >C++Builder 6 the time calculation was correct, but in C++ Builder 2007 I >get calculation errors of 3600 seconds. If you want to compare the times, use a time comparison function, or manually adjust using the tm_isdst flag. long t1 = time(0); long t2 = DLLtime(); long diff1 = difftime( t1, t2 ); #define ADJUST( t ) (t.tm_isdst>0?3600:0) long t1 = time(0); long t2 = DLLtime(); struct tm ptime1 = *localtime(&t1); struct tm ptime2 = *localtime(&t2); long diff2 = (t1-ADJUST(time1)) - (t2-ADJUST(time2)) ; >It seems that the time(0) function in C++Builder 2007 seems to return >the local time, instead of the GMT time? No, localtime(&t) returns local time. Try gmtime(&t)
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads