News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

_ftime problem

Started by Greg, December 21, 2004, 08:38:12 AM

Previous topic - Next topic

Greg

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,

Robert

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] );
}

Robert

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

tiwag

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)

Greg

Yes, the timezone and dstflag values are correct using msvcrt.lib. They don't seem to be correct with crt.lib or pocrt.lib.

Pelle

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
/Pelle

tiwag

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

Greg

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.  :)

Pelle

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
/Pelle

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

Robert

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

Pelle

Sigh. Thanks for the info - back to the drawing board...

Pelle
/Pelle

Pelle

New try - maybe this works better...?

Pelle
/Pelle

Robert

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

Robert

Pelle:

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

January 1st = day 0

Robert Wishlaw