Pelles C forum
Pelles C => Bug reports => Topic started by: Greg on December 21, 2004, 08:38:12 AM
-
I am having a problem with _ftime. It is not returning the correct values for timezone and dstflag in the _timeb structure. Here is an example:
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <sys\timeb.h>
void waitkey(void);
int main(void)
{
struct _timeb tmb;
_ftime(&tmb);
printf("\n");
printf("Seconds: %d \n", tmb.time);
printf("Milliseconds: %hu \n", tmb.millitm);
printf("UTC Offset: %hd minutes \n", tmb.timezone);
printf("Daylight Time: ");
if(tmb.dstflag == 0)
{
printf("False \n");
}
else if(tmb.dstflag > 0)
{
printf("True \n");
}
else // tmb.dstflag < 0
{
printf("Unknown \n");
}
waitkey();
return(0);
}
void waitkey(void)
{
printf("\nPress any key to continue...");
_getch();
printf("\n");
}
timezone is 16996, it should be 480.
dstflag is -1 (Unknown), it should be 0 (False).
I am using Pelle's C headers and libraries.
Thanks,
-
Hi Greg:
This is from
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_crt__ftime.asp
/* FTIME.C: This program uses _ftime to obtain the current
* time and then stores this time in timebuffer.
*/
#include <stdio.h>
#include <sys/timeb.h>
#include <time.h>
void main( void )
{
struct _timeb timebuffer;
char *timeline;
_ftime( &timebuffer );
timeline = ctime( & ( timebuffer.time ) );
printf( "The time is %.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20] );
}
-
Hi Greg:
:oops: I confirm your report of a faulty _timeb timezone.
VC7 gives me the correct 480
Pelle's C gives me 16996 minutes
-
if you link with msvcrt.dll
#pragma comment (lib, "msvcrt.lib")
it's also correct with PellesC.
here at my machine (Windows XP SP2 )
msvcrt gives (correct values):
UTC (-60 minutes)
DST (0)
pocrt gives:
UTC (60 minutes)
DST (-1)
-
Yes, the timezone and dstflag values are correct using msvcrt.lib. They don't seem to be correct with crt.lib or pocrt.lib.
-
At least the timezone value looks wrong - not even sure how you can get a value outside +/- ~ 12*60. Will look at it!
( is localtime() correct on your computer? )
Pelle
-
hi pelle,
localtime() :daylight saving time information (tm_isdst) is also incorrect when linking with crt.lib.
_ftime() : both values UTC offset (timezone) and dst (dstflag) are wrong when linking with crt.lib.
localtime() and _ftime() are correct when linking to msvcrt.lib
i use WinXP SP2, PellesC 2.90.8
greetings
-
Pelle,
I get the same results as tiwag. I am running Windows XP SP2 and Pelles C 2.90.8 also.
I don't want to come across as complaining. I really appreciate and admire what you are doing with Pelle's C. :)
-
I don't want to come across as complaining. I really appreciate and admire what you are doing with Pelle's C. :)
No problems - this should work too. Just a small pain to get the time zone stuff straight. I remember having trouble with this before.
Pelle
-
For standard C, it seems OK for the tm member tm_isdst to be negative - for "unknown". An implementation don't need to fill in a useful value. The _timeb member dstflag should probably always be >= 0 - to be compatible with MS. I now try to initialize both members.
Is the attached version working OK in your timezone - something other than Stockholm?
Note! The reported information will not be better than what Ms Windows reports...
Pelle
-
Pelle:
C:\ST060\Programming\Pelle's C\timetest>timetest
_ftime() reports:
seconds...........1104304267
milliseconds......406
UTC offset........-16996 minutes <<<<<<< should be 480
daylight saving...No
localtime() reports:
year..............2004
month.............12
day...............28
hours.............23
minutes...........11
seconds...........7
day of week.......Tue
day of year.......362
daylight saving...No
Robert wishlaw
-
Sigh. Thanks for the info - back to the drawing board...
Pelle
-
New try - maybe this works better...?
Pelle
-
Hi Pelle:
The UTC Offset works for me.
But the day of the year has not taken leap year into account. It is off by one less.
_ftime() reports:
seconds...........1104381744
milliseconds......500
UTC offset........480 minutes
daylight saving...No
localtime() reports:
year..............2004
month.............12
day...............29
hours.............20
minutes...........42
seconds...........24
day of week.......Wed
day of year.......363 <<<<2004 is Leap year should be 364
daylight saving...No
Robert Wishlaw
-
Pelle:
I am wrong, there is no problem with day of year.
January 1st = day 0
Robert Wishlaw
-
Thanks for the update, Robert! Seems to work in two time-zones at least...
Pelle
-
Hi Pelle,
timetest also works correct here at my location using CET
WinXP SP2
-
Hello tiwag,
Thanks for the info - good to know.
Pelle
-
The timezone issues with _ftime, localtime etc. are all corrected in the 3.00.0 Beta #1.
Thanks for all your efforts Pele. I'm liking Pelle's C more and more all the time.
-
Thanks! That's what I needed - some good news... ;)
Pelle
-
Hi Pelle:
_ftime() is still broken in version 3.00.0 beta 1. If fails in time-zones greater than +6 GMT. The timezone returns 0, and UTC time is returned instead of local time.
I tested your posted timetest.exe while in Holland, and it worked, but that was not much of a test, was it? I am in Bangkok (+7GMT) now and it does not work here.
Is the function _tzset(); needed? VC++ seams to work with or without it, and I could not find it in PellesC.
Garvan
-
Hello Garvan,
I tested your posted timetest.exe while in Holland, and it worked, but that was not much of a test, was it? I am in Bangkok (+7GMT) now and it does not work here.
Test for me, in the sense that if localtime() is working, there is no fundamental flaw in calculating the timezone offset. If only _ftime() is wrong, I should look in one place for the problem. If also localtime() is wrong, I should look at the "bigger picture". This is why I wanted as many people as possible, from different timezones, to report the results.
Is the function _tzset(); needed? VC++ seams to work with or without it, and I could not find it in PellesC.
According to the docs, the _tzset function uses the current setting of the environment variable TZ to assign values to three global variables.
I have similar code, that checks the environment variable TZ, but it's called automatically - no need to call any _tzset function (if I can't find the TZ variable, I fall back and ask Windows about the "TZ" value, which is the parsed/analyzed).
I tried changing the Windows timezone setting, on my Swedish Windows XP, to various zones over the world (+/- many hours from GMT). It worked for most places, but not for some. I wasn't sure if this is because my Swedish version lacks some "setting files", or if there is a problem *sometimes*.
Pelle
-
Those functions are all working correctly for me with the 3.00.0 Beta #1.
My timezone is Pacific Standard Time ( GMT-8 ) or TZ=PST8PDT.
-
Pelle,
May I suggest you post the source code for timetest.exe so people can try it in their time zone and everyone would be using the same code.
-
OK, here is the code - I have added a call to gmtime() for completeness.
Pelle
-
timetest.c returns all the correct values here ( GMT-8 ) using both crt.lib and pocrt.lib version 3.00.0 Beta #1.
-
Thanks for the info, Greg. It works here too (GMT+1) - not sure why it fails on some machines.
Pelle
-
Pelle,
(GMT+1)
You are either up very late or up very early. :)
-
Way too late... but I promise, I will go to bed now... :shock:
Pelle
-
Hi Pelle:
Sorry for not making my report clearer. localtime is also wrong.
This is a comparison result from running your code compiled with VC and then PellesC in a batch file. I am at GMT+7, using WinXP with the 3.0 beta 1 release.
Thanks
Garvan
VC++
_ftime() reports:
seconds...........1105680571
milliseconds......14
UTC offset........-420 minutes
daylight saving...No
localtime() reports:
year..............2005
month.............1
day...............14
hours.............12
minutes...........29
seconds...........31
day of week.......Fri
day of year.......13
daylight saving...No
gmtime() reports:
year.............2005
month............1
day..............14
hours............5
minutes..........29
seconds..........31
day of week......Fri
day of year......13
daylight saving..No
-----------
PellesC
_ftime() reports:
seconds...........1105680571
milliseconds......34
UTC offset........0 minutes
daylight saving...Unknown
localtime() reports:
year..............2005
month.............1
day...............14
hours.............5
minutes...........29
seconds...........31
day of week.......Fri
day of year.......13
daylight saving...Unknown
gmtime() reports:
year.............2005
month............1
day..............14
hours............5
minutes..........29
seconds..........31
day of week......Fri
day of year......13
daylight saving..No
-
Hello Garvan,
Sorry for not making my report clearer. localtime is also wrong.
No problems.
I use the Windows API function GetTimeZoneInformation() to get the time zone bias - it's the best I can find, but I'm no expert in this area.
If I set it to "(GMT+07:00) Bangkok, Hanoi, ..." on my Windows XP, I get TIME_ZONE_ID_UNKNOWN, but if I set it to "(GMT+07:00) Krasnoyarsk" I get TIM_ZONE_ID_STANDARD, Bias = -420.
I have attached the a simple test program for GetTimeZoneInformation() - what does it return on your computer?
Pelle
-
Hi:
This is what I get! Same result with VC++
GetTimeZoneInformation() returned TIME_ZONE_ID_UNKNOWN
(Windows cannot determine the current time zone...)
Garvan
-
Garvan & Pelle,
That's what it should return under Windows NT/2000/XP. (GMT+07:00) Bangkok, Hanoi, Jakarta does not use Daylight Saving Time. So GetTimeZoneInformation() will return TIME_ZONE_ID_UNKNOWN, which is misleading.
See MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/gettimezoneinformation.asp
It boils down to:
TIME_ZONE_ID_DAYLIGHT = Daylight Time
TIME_ZONE_ID_STANDARD or TIME_ZONE_ID_UNKNOWN = Standard Time
TIME_ZONE_ID_INVALID = GetTimeZoneInformation() failed
-
For a related problem please see
SetLocalTime/GetLocalTime Not the Same if Adjusting for Daylight Savings Time
http://support.microsoft.com/kb/q234735/
Robert Wishlaw
-
Re: GetTimeZoneInformation
The meanings of the returned value changed with Windows NT. Some older code examples use the Windows 9x meanings.
-
An example of what I'm trying to say:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
int main(void)
{
TIME_ZONE_INFORMATION tzi;
switch (GetTimeZoneInformation(&tzi))
{
case TIME_ZONE_ID_DAYLIGHT:
/* Windows 9x/ME/NT/2000/XP - The current time zone is on Daylight Saving Time */
printf("GetTimeZoneInformation() returned TIME_ZONE_ID_DAYLIGHT:\n");
printf("Bias: %ld\n", tzi.Bias);
printf("DaylightName: %ls\n", tzi.DaylightName); /* This is a Unicode string */
printf("DaylightBias: %ld\n", tzi.DaylightBias);
break;
case TIME_ZONE_ID_STANDARD:
/* Windows NT/2000/XP - the current time zone is on Standard Time */
/* Windows 9x/ME - The current time zone does not use Daylight Saving Time or
the current time zone is on Standard Time */
printf("GetTimeZoneInformation() returned TIME_ZONE_ID_STANDARD:\n");
printf("Bias: %ld\n", tzi.Bias);
printf("StandardName: %ls\n", tzi.StandardName); /* This is a Unicode string */
printf("StandardBias: %ld\n", tzi.StandardBias);
break;
case TIME_ZONE_ID_UNKNOWN:
/* Windows NT/2000/XP - The current time zone does not use Daylight Saving Time */
printf("GetTimeZoneInformation() returned TIME_ZONE_ID_UNKNOWN:\n");
printf("Bias: %ld\n", tzi.Bias);
printf("StandardName: %ls\n", tzi.StandardName); /* This is a Unicode string */
printf("StandardBias: %ld\n", tzi.StandardBias);
break;
case TIME_ZONE_ID_INVALID:
printf("Error calling GetTimeZoneInformation()\n");
break;
default:
printf("GetTimeZoneInformation() returned unknown value\n");
break;
}
return 0;
}
-
Hi:
That's what it should return under Windows NT/2000/XP. (GMT+07:00) Bangkok, Hanoi, Jakarta does not use Daylight Saving Time. So GetTimeZoneInformation() will return TIME_ZONE_ID_UNKNOWN, which is misleading.
Thanks for this.
Garvan.
-
Thank you very much for the information, Greg!
Pelle