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

  1. /***
  2. *time.c - get current system time
  3. *
  4. *       Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines time() - gets the current system time and converts it to
  8. *                        internal (time_t) format time.
  9. *
  10. *******************************************************************************/
  11.  
  12.  
  13. #include <cruntime.h>
  14. #include <time.h>
  15. #include <internal.h>
  16.  
  17. #ifdef _WIN32
  18. #include <windows.h>
  19. #else  /* _WIN32 */
  20. #if defined (_M_MPPC) || defined (_M_M68K)
  21. #include <macos\osutils.h>     /* get DataTimeRec type */
  22. #endif  /* defined (_M_MPPC) || defined (_M_M68K) */
  23. #endif  /* _WIN32 */
  24.  
  25. #ifdef _WIN32
  26.  
  27. /*
  28.  * Cache holding the last time (GMT) for which the Daylight time status was
  29.  * determined by an API call.
  30.  */
  31. static SYSTEMTIME gmt_cache;
  32.  
  33. /*
  34.  * Three values of dstflag_cache and dstflag (local variable in code
  35.  * below)
  36.  */
  37. #define DAYLIGHT_TIME   1
  38. #define STANDARD_TIME   0
  39. #define UNKNOWN_TIME    -1
  40.  
  41. static int dstflag_cache;
  42.  
  43. #endif  /* _WIN32 */
  44.  
  45. /***
  46. *time_t time(timeptr) - Get current system time and convert to time_t value.
  47. *
  48. *Purpose:
  49. *       Gets the current date and time and stores it in internal (time_t)
  50. *       format. The time is returned and stored via the pointer passed in
  51. *       timeptr. If timeptr == NULL, the time is only returned, not stored in
  52. *       *timeptr. The internal (time_t) format is the number of seconds since
  53. *       00:00:00, Jan 1 1970 (UTC).
  54. *
  55. *       Note: We cannot use GetSystemTime since its return is ambiguous. In
  56. *       Windows NT, in return UTC. In Win32S, probably also Win32C, it
  57. *       returns local time.
  58. *
  59. *Entry:
  60. *       time_t *timeptr - pointer to long to store time in.
  61. *
  62. *Exit:
  63. *       returns the current time.
  64. *
  65. *Exceptions:
  66. *
  67. *******************************************************************************/
  68.  
  69. time_t __cdecl time (
  70.         time_t *timeptr
  71.         )
  72. {
  73.         time_t tim;
  74.  
  75. #ifdef _WIN32
  76.  
  77.         SYSTEMTIME loct, gmt;
  78.         TIME_ZONE_INFORMATION tzinfo;
  79.         DWORD tzstate;
  80.         int dstflag;
  81.  
  82.         /*
  83.          * Get local time from Win32
  84.          */
  85.         GetLocalTime( &loct );
  86.  
  87.         /*
  88.          * Determine whether or not the local time is a Daylight Saving
  89.          * Time. On Windows NT, the GetTimeZoneInformation API is *VERY*
  90.          * expensive. The scheme below is intended to avoid this API call in
  91.          * many important case by caching the GMT value and dstflag.In a
  92.          * subsequent call to time(), the cached value of dstflag is used
  93.          * unless the new GMT differs from the cached value at least in the
  94.          * minutes place.
  95.          */
  96.         GetSystemTime( &gmt );
  97.  
  98.         if ( (gmt.wMinute == gmt_cache.wMinute) &&
  99.              (gmt.wHour == gmt_cache.wHour) &&
  100.              (gmt.wDay == gmt_cache.wDay) &&
  101.              (gmt.wMonth == gmt_cache.wMonth) &&
  102.              (gmt.wYear == gmt_cache.wYear) )
  103.         {
  104.             dstflag = dstflag_cache;
  105.         }
  106.         else
  107.         {
  108.             if ( (tzstate = GetTimeZoneInformation( &tzinfo )) != 0xFFFFFFFF )
  109.             {
  110.                 /*
  111.                  * Must be very careful in determining whether or not DST is
  112.                  * really in effect.
  113.                  */
  114.                 if ( (tzstate == TIME_ZONE_ID_DAYLIGHT) &&
  115.                      (tzinfo.DaylightDate.wMonth != 0) &&
  116.                      (tzinfo.DaylightBias != 0) )
  117.                     dstflag = DAYLIGHT_TIME;
  118.                 else
  119.                     /*
  120.                      * When in doubt, assume standard time
  121.                      */
  122.                     dstflag = STANDARD_TIME;
  123.             }
  124.             else
  125.                 dstflag = UNKNOWN_TIME;
  126.  
  127.             dstflag_cache = dstflag;
  128.             gmt_cache = gmt;
  129.         }
  130.  
  131.         /* convert using our private routine */
  132.  
  133.         tim = __loctotime_t( (int)loct.wYear,
  134.                              (int)loct.wMonth,
  135.                              (int)loct.wDay,
  136.                              (int)loct.wHour,
  137.                              (int)loct.wMinute,
  138.                              (int)loct.wSecond,
  139.                              dstflag );
  140.  
  141. #else  /* _WIN32 */
  142. #if defined (_M_MPPC) || defined (_M_M68K)
  143.  
  144.         DateTimeRec dt;
  145.  
  146.         GetTime(&dt);
  147.         /* convert using our private routine */
  148.         tim = _gmtotime_t((int)dt.year,
  149.                           (int)dt.month,
  150.                           (int)dt.day,
  151.                           (int)dt.hour,
  152.                           dt.minute,
  153.                           dt.second);
  154.  
  155. #endif  /* defined (_M_MPPC) || defined (_M_M68K) */
  156. #endif  /* _WIN32 */
  157.  
  158.         if (timeptr)
  159.                 *timeptr = tim;         /* store time if requested */
  160.  
  161.         return tim;
  162. }
  163.  
  164.