home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / ftime.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  5KB  |  195 lines

  1. /***
  2. *ftime.c - return system time
  3. *
  4. *       Copyright (c) 1985-1998, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Returns the system date/time in a structure form.
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifdef _WIN32
  12.  
  13.  
  14.  
  15. #include <cruntime.h>
  16. #include <sys/types.h>
  17. #include <sys/timeb.h>
  18. #include <time.h>
  19. #include <dostypes.h>
  20. #include <msdos.h>
  21. #include <dos.h>
  22. #include <stdlib.h>
  23. #include <windows.h>
  24. #include <internal.h>
  25.  
  26. /*
  27.  * Number of 100 nanosecond units from 1/1/1601 to 1/1/1970
  28.  */
  29. #define EPOCH_BIAS  116444736000000000i64
  30.  
  31. /*
  32.  * Union to facilitate converting from FILETIME to unsigned __int64
  33.  */
  34. typedef union {
  35.         unsigned __int64 ft_scalar;
  36.         FILETIME ft_struct;
  37.         } FT;
  38.  
  39. /*
  40.  * Cache for the minutes count for with DST status was last assessed
  41.  */
  42. static time_t elapsed_minutes_cache = 0;
  43.  
  44. /*
  45.  * Three values of dstflag_cache
  46.  */
  47. #define DAYLIGHT_TIME   1
  48. #define STANDARD_TIME   0
  49. #define UNKNOWN_TIME    -1
  50.  
  51. /*
  52.  * Cache for the last determined DST status
  53.  */
  54. static int dstflag_cache = UNKNOWN_TIME;
  55.  
  56. /***
  57. *void _ftime(timeptr) - return DOS time in a structure
  58. *
  59. *Purpose:
  60. *       returns the current DOS time in a struct timeb structure
  61. *
  62. *Entry:
  63. *       struct timeb *timeptr - structure to fill in with time
  64. *
  65. *Exit:
  66. *       no return value -- fills in structure
  67. *
  68. *Exceptions:
  69. *
  70. *******************************************************************************/
  71.  
  72. _CRTIMP void __cdecl _ftime (
  73.         struct _timeb *tp
  74.         )
  75. {
  76.         FT nt_time;
  77.         time_t t;
  78.         TIME_ZONE_INFORMATION tzinfo;
  79.         DWORD tzstate;
  80.  
  81.         __tzset();
  82.  
  83.         tp->timezone = (short)(_timezone / 60);
  84.  
  85.         GetSystemTimeAsFileTime( &(nt_time.ft_struct) );
  86.  
  87.         /*
  88.          * Obtain the current DST status. Note the status is cached and only
  89.          * updated once per minute, if necessary.
  90.          */
  91.         if ( (t = (time_t)(nt_time.ft_scalar / 600000000i64))
  92.              != elapsed_minutes_cache )
  93.         {
  94.             if ( (tzstate = GetTimeZoneInformation( &tzinfo )) != 0xFFFFFFFF )
  95.             {
  96.                 /*
  97.                  * Must be very careful in determining whether or not DST is
  98.                  * really in effect.
  99.                  */
  100.                 if ( (tzstate == TIME_ZONE_ID_DAYLIGHT) &&
  101.                      (tzinfo.DaylightDate.wMonth != 0) &&
  102.                      (tzinfo.DaylightBias != 0) )
  103.                     dstflag_cache = DAYLIGHT_TIME;
  104.                 else
  105.                     /*
  106.                      * When in doubt, assume standard time
  107.                      */
  108.                     dstflag_cache = STANDARD_TIME;
  109.             }
  110.             else
  111.                 dstflag_cache = UNKNOWN_TIME;
  112.  
  113.             elapsed_minutes_cache = t;
  114.         }
  115.  
  116.         tp->dstflag = (short)dstflag_cache;
  117.  
  118.         tp->millitm = (unsigned short)((nt_time.ft_scalar / 10000i64) %
  119.                       1000i64);
  120.  
  121.         tp->time = (time_t)((nt_time.ft_scalar - EPOCH_BIAS) / 10000000i64);
  122. }
  123.  
  124.  
  125.  
  126. #else  /* _WIN32 */
  127.  
  128. #if defined (_M_MPPC) || defined (_M_M68K)
  129.  
  130.  
  131. #include <cruntime.h>
  132. #include <sys/types.h>
  133. #include <sys/timeb.h>
  134. #include <time.h>
  135. #include <stdlib.h>
  136. #include <internal.h>
  137. #include <macos\osutils.h>      /* get DataTimeRec type */
  138. #include <macos\memory.h>
  139. #include <macos\lowmem.h>
  140.  
  141. /***
  142. *void _ftime(timeptr) - return DOS time in a structure
  143. *
  144. *Purpose:
  145. *       returns the current DOS time in a struct timeb structure
  146. *
  147. *Entry:
  148. *       struct timeb *timeptr - structure to fill in with time
  149. *
  150. *Exit:
  151. *       no return value -- fills in structure
  152. *
  153. *Exceptions:
  154. *
  155. *******************************************************************************/
  156.  
  157. void __cdecl _ftime (
  158.         struct _timeb *tp
  159.         )
  160. {
  161.         struct tm tb;
  162.         DateTimeRec dt;
  163.  
  164.         _tzset();
  165.         tp->timezone = (short)(_timezone / 60);
  166.  
  167.         GetTime(&dt);
  168.  
  169.         /*set milliseconds*/
  170.  
  171.         tp->millitm = (unsigned short)( ((LMGetTicks() % 60) * 50) / 3);
  172.  
  173.         tb.tm_year = dt.year - 1900;
  174.         tb.tm_mday = dt.day;
  175.         tb.tm_mon = dt.month - 1;         //[1-12]=>[0-11]
  176.         tb.tm_hour = dt.hour;
  177.         tb.tm_min   = dt.minute;
  178.         tb.tm_sec   = dt.second;
  179.         tb.tm_wday = dt.dayOfWeek - 1;    //[1-7]=>[0-6]
  180.         tb.tm_isdst = -1;
  181.  
  182.         /*
  183.          * Call mktime() to compute time_t value and Daylight Savings Time
  184.          * flag.
  185.          */
  186.         tp->time = mktime(&tb);
  187.  
  188.         tp->dstflag = (short)(tb.tm_isdst);
  189. }
  190.  
  191.  
  192. #endif  /* defined (_M_MPPC) || defined (_M_M68K) */
  193.  
  194. #endif  /* _WIN32 */
  195.