Pelles C forum

Pelles C => Bug reports => Topic started by: Greg on December 21, 2004, 08:38:12 AM

Title: _ftime problem
Post 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:
Code: [Select]

#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,
Title: _ftime
Post by: Robert on December 21, 2004, 08:58:46 AM
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] );
}
Title: The Time Zone >>>>
Post by: Robert on December 21, 2004, 09:08:31 AM
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
Title: _ftime bug
Post by: tiwag on December 21, 2004, 09:21:21 AM
if you link with msvcrt.dll
Code: [Select]

#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)
Title: _ftime problem
Post by: Greg on December 21, 2004, 07:28:18 PM
Yes, the timezone and dstflag values are correct using msvcrt.lib. They don't seem to be correct with crt.lib or pocrt.lib.
Title: Re: _ftime problem
Post by: Pelle on December 22, 2004, 03:22:59 PM
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
Title: _ftime problem
Post by: tiwag on December 22, 2004, 09:33:42 PM
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
Title: _ftime problem
Post by: Greg on December 22, 2004, 10:23:36 PM
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.  :)
Title: _ftime problem
Post by: Pelle on December 23, 2004, 06:53:54 AM
Quote from: "Greg"
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
Title: _ftime problem
Post by: Pelle on December 29, 2004, 06:12:46 AM
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
Title: UTC offset wrong
Post by: Robert on December 29, 2004, 08:07:45 AM
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
Title: _ftime problem
Post by: Pelle on December 29, 2004, 07:06:52 PM
Sigh. Thanks for the info - back to the drawing board...

Pelle
Title: _ftime problem
Post by: Pelle on December 30, 2004, 02:56:33 AM
New try - maybe this works better...?

Pelle
Title: New Time
Post by: Robert on December 30, 2004, 05:49:26 AM
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
Title: January 1st is 0
Post by: Robert on December 30, 2004, 07:07:19 AM
Pelle:

I am wrong, there is no problem with day of year.

January 1st = day 0

Robert Wishlaw
Title: _ftime problem
Post by: Pelle on December 30, 2004, 11:21:02 PM
Thanks for the update, Robert! Seems to work in two time-zones at least...

Pelle
Title: _ftime problem
Post by: tiwag on December 31, 2004, 09:35:18 AM
Hi Pelle,

timetest also works correct here at my location using CET

WinXP SP2
Title: _ftime problem
Post by: Pelle on December 31, 2004, 04:08:19 PM
Hello tiwag,

Thanks for the info - good to know.

Pelle
Title: _ftime problem
Post by: Greg on January 11, 2005, 04:04:06 AM
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.
Title: _ftime problem
Post by: Pelle on January 11, 2005, 06:35:20 PM
Thanks! That's what I needed - some good news... ;)

Pelle
Title: _ftime problem
Post by: Garvan on January 13, 2005, 11:32:46 AM
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
Title: _ftime problem
Post by: Pelle on January 13, 2005, 03:33:28 PM
Hello Garvan,

Quote from: "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.

Quote from: "Garvan"

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
Title: _ftime problem
Post by: Greg on January 13, 2005, 10:54:31 PM
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.
Title: _ftime problem
Post by: Greg on January 14, 2005, 12:00:52 AM
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.
Title: _ftime problem
Post by: Pelle on January 14, 2005, 12:56:00 AM
OK, here is the code - I have added a call to gmtime() for completeness.

Pelle
Title: _ftime problem
Post by: Greg on January 14, 2005, 03:29:40 AM
timetest.c returns all the correct values here ( GMT-8 ) using both crt.lib and pocrt.lib version 3.00.0 Beta #1.
Title: _ftime problem
Post by: Pelle on January 14, 2005, 04:17:57 AM
Thanks for the info, Greg. It works here too (GMT+1) - not sure why it fails on some machines.

Pelle
Title: _ftime problem
Post by: Greg on January 14, 2005, 04:43:00 AM
Pelle,

Quote
(GMT+1)


You are either up very late or up very early.  :)
Title: _ftime problem
Post by: Pelle on January 14, 2005, 04:51:13 AM
Way too late... but I promise, I will go to bed now...  :shock:

Pelle
Title: _ftime problem
Post by: Garvan on January 14, 2005, 06:52:12 AM
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
Title: _ftime problem
Post by: Pelle on January 14, 2005, 02:42:16 PM
Hello Garvan,

Quote from: "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
Title: _ftime problem
Post by: Garvan on January 15, 2005, 08:30:57 AM
Hi:

This is what I get! Same result with VC++

GetTimeZoneInformation() returned TIME_ZONE_ID_UNKNOWN
(Windows cannot determine the current time zone...)


Garvan
Title: _ftime problem
Post by: Greg on January 15, 2005, 08:43:56 PM
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
Title: GetLocalTime Not the Same if Adjusting for Daylight
Post by: Robert on January 16, 2005, 01:04:44 AM
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
Title: _ftime problem
Post by: Greg on January 16, 2005, 01:40:43 AM
Re: GetTimeZoneInformation

The meanings of the returned value changed with Windows NT. Some older code examples use the Windows 9x meanings.
Title: _ftime problem
Post by: Greg on January 16, 2005, 03:31:27 AM
An example of what I'm trying to say:
Code: [Select]

#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;
}
Title: _ftime problem
Post by: Garvan on January 17, 2005, 11:45:35 AM
Hi:

Quote from: "Greg"

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.
Title: _ftime problem
Post by: Pelle on January 24, 2005, 02:17:51 PM
Thank you very much for the information, Greg!

Pelle