Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.0 KiB

  1. /***
  2. *ftime.c - return system time
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Returns the system date/time in a structure form.
  8. *
  9. *Revision History:
  10. * 03-??-84 RLB initial version
  11. * 05-17-86 SKS ported to OS/2
  12. * 03-09-87 SKS correct Daylight Savings Time flag
  13. * 11-18-87 SKS Change tzset() to __tzset()
  14. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  15. * 10-03-88 JCR 386: Change DOS calls to SYS calls
  16. * 10-04-88 JCR 386: Removed 'far' keyword
  17. * 10-10-88 GJF Made API names match DOSCALLS.H
  18. * 04-12-89 JCR New syscall interface
  19. * 05-25-89 JCR 386 OS/2 calls use '_syscall' calling convention
  20. * 03-20-90 GJF Replaced _LOAD_DS with _CALLTYPE1, added #include
  21. * <cruntime.h>, removed some leftover 16-bit support
  22. * and fixed the copyright. Also, cleaned up the
  23. * formatting a bit.
  24. * 07-25-90 SBM Removed '32' from API names
  25. * 08-13-90 SBM Compiles cleanly with -W3
  26. * 08-20-90 SBM Removed old incorrect, redundant tp->dstflag assignment
  27. * 10-04-90 GJF New-style function declarator.
  28. * 12-04-90 SRW Changed to include <oscalls.h> instead of <doscalls.h>
  29. * 12-06-90 SRW Added _CRUISER_ and _WIN32 conditionals.
  30. * 01-21-91 GJF ANSI naming.
  31. * 01-23-92 GJF Change in time zone field name for Win32, to support
  32. * crtdll.dll [_WIN32_].
  33. * 03-30-93 GJF Revised to use mktime(). Also purged Cruiser support.
  34. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  35. * 04-07-93 SKS Add _CRTIMP keyword for CRT DLL model
  36. * Restore correct spelling of "timezone" struct member.
  37. * 07-15-93 GJF Call __tzset() instead of _tzset().
  38. * 02-10-95 GJF Appended Mac version of source file (somewhat cleaned
  39. * up), with appropriate #ifdef-s.
  40. * 01-15-98 GJF Complete rewrite of _ftime to eliminate incorrect dst
  41. * status during the fall-back hour.
  42. * 05-17-99 PML Remove all Macintosh support.
  43. * 10-27-99 GB Remove #inlcude <dostypes.h>
  44. *
  45. *******************************************************************************/
  46. #ifndef _POSIX_
  47. #include <cruntime.h>
  48. #include <sys/types.h>
  49. #include <sys/timeb.h>
  50. #include <time.h>
  51. #include <msdos.h>
  52. #include <dos.h>
  53. #include <stdlib.h>
  54. #include <windows.h>
  55. #include <internal.h>
  56. /*
  57. * Number of 100 nanosecond units from 1/1/1601 to 1/1/1970
  58. */
  59. #define EPOCH_BIAS 116444736000000000i64
  60. /*
  61. * Union to facilitate converting from FILETIME to unsigned __int64
  62. */
  63. typedef union {
  64. unsigned __int64 ft_scalar;
  65. FILETIME ft_struct;
  66. } FT;
  67. /*
  68. * Cache for the minutes count for with DST status was last assessed
  69. */
  70. static time_t elapsed_minutes_cache = 0;
  71. /*
  72. * Three values of dstflag_cache
  73. */
  74. #define DAYLIGHT_TIME 1
  75. #define STANDARD_TIME 0
  76. #define UNKNOWN_TIME -1
  77. /*
  78. * Cache for the last determined DST status
  79. */
  80. static int dstflag_cache = UNKNOWN_TIME;
  81. /***
  82. *void _ftime(timeptr) - return DOS time in a structure
  83. *
  84. *Purpose:
  85. * returns the current DOS time in a struct timeb structure
  86. *
  87. *Entry:
  88. * struct timeb *timeptr - structure to fill in with time
  89. *
  90. *Exit:
  91. * no return value -- fills in structure
  92. *
  93. *Exceptions:
  94. *
  95. *******************************************************************************/
  96. _CRTIMP void __cdecl _ftime (
  97. struct _timeb *tp
  98. )
  99. {
  100. FT nt_time;
  101. time_t t;
  102. TIME_ZONE_INFORMATION tzinfo;
  103. DWORD tzstate;
  104. __tzset();
  105. tp->timezone = (short)(_timezone / 60);
  106. GetSystemTimeAsFileTime( &(nt_time.ft_struct) );
  107. /*
  108. * Obtain the current DST status. Note the status is cached and only
  109. * updated once per minute, if necessary.
  110. */
  111. if ( (t = (time_t)(nt_time.ft_scalar / 600000000i64))
  112. != elapsed_minutes_cache )
  113. {
  114. if ( (tzstate = GetTimeZoneInformation( &tzinfo )) != 0xFFFFFFFF )
  115. {
  116. /*
  117. * Must be very careful in determining whether or not DST is
  118. * really in effect.
  119. */
  120. if ( (tzstate == TIME_ZONE_ID_DAYLIGHT) &&
  121. (tzinfo.DaylightDate.wMonth != 0) &&
  122. (tzinfo.DaylightBias != 0) )
  123. dstflag_cache = DAYLIGHT_TIME;
  124. else
  125. /*
  126. * When in doubt, assume standard time
  127. */
  128. dstflag_cache = STANDARD_TIME;
  129. }
  130. else
  131. dstflag_cache = UNKNOWN_TIME;
  132. elapsed_minutes_cache = t;
  133. }
  134. tp->dstflag = (short)dstflag_cache;
  135. tp->millitm = (unsigned short)((nt_time.ft_scalar / 10000i64) %
  136. 1000i64);
  137. tp->time = (time_t)((nt_time.ft_scalar - EPOCH_BIAS) / 10000000i64);
  138. }
  139. #endif /* _POSIX_ */