NO

Author Topic: _ftime problem  (Read 29200 times)

Greg

  • Guest
_ftime problem
« 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,

Offline Robert

  • Member
  • *
  • Posts: 247
_ftime
« Reply #1 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] );
}

Offline Robert

  • Member
  • *
  • Posts: 247
The Time Zone >>>>
« Reply #2 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

tiwag

  • Guest
_ftime bug
« Reply #3 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)

Greg

  • Guest
_ftime problem
« Reply #4 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.

Offline Pelle

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

tiwag

  • Guest
_ftime problem
« Reply #6 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

Greg

  • Guest
_ftime problem
« Reply #7 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.  :)

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
_ftime problem
« Reply #8 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
/Pelle

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
_ftime problem
« Reply #9 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
/Pelle

Offline Robert

  • Member
  • *
  • Posts: 247
UTC offset wrong
« Reply #10 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

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
_ftime problem
« Reply #11 on: December 29, 2004, 07:06:52 PM »
Sigh. Thanks for the info - back to the drawing board...

Pelle
/Pelle

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
_ftime problem
« Reply #12 on: December 30, 2004, 02:56:33 AM »
New try - maybe this works better...?

Pelle
/Pelle

Offline Robert

  • Member
  • *
  • Posts: 247
New Time
« Reply #13 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

Offline Robert

  • Member
  • *
  • Posts: 247
January 1st is 0
« Reply #14 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