Leaked source code of windows server 2003
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.

368 lines
8.3 KiB

  1. /*++
  2. Copyright (c) 1989-2000 Microsoft Corporation
  3. Module Name:
  4. TimeSup.c
  5. Abstract:
  6. This module implements the Fat Time conversion support routines
  7. // @@BEGIN_DDKSPLIT
  8. Author:
  9. Gary Kimura [GaryKi] 19-Feb-1990
  10. Revision History:
  11. // @@END_DDKSPLIT
  12. --*/
  13. #include "FatProcs.h"
  14. #ifdef ALLOC_PRAGMA
  15. #pragma alloc_text(PAGE, FatNtTimeToFatTime)
  16. #pragma alloc_text(PAGE, FatFatDateToNtTime)
  17. #pragma alloc_text(PAGE, FatFatTimeToNtTime)
  18. #pragma alloc_text(PAGE, FatGetCurrentFatTime)
  19. #endif
  20. BOOLEAN
  21. FatNtTimeToFatTime (
  22. IN PIRP_CONTEXT IrpContext,
  23. IN PLARGE_INTEGER NtTime,
  24. IN BOOLEAN Rounding,
  25. OUT PFAT_TIME_STAMP FatTime,
  26. OUT OPTIONAL PCHAR TenMsecs
  27. )
  28. /*++
  29. Routine Description:
  30. This routine converts an NtTime value to its corresponding Fat time value.
  31. Arguments:
  32. NtTime - Supplies the Nt GMT Time value to convert from
  33. Rounding - Indicates whether the NT time should be rounded up to a FAT boundary.
  34. This should only be done *once* in the lifetime of a timestamp (important
  35. for tunneling, which will cause a timestamp to pass through at least twice).
  36. If true, rounded up. If false, rounded down to 10ms boundary. This obeys
  37. the rules for non-creation time and creation times (respectively).
  38. FatTime - Receives the equivalent Fat time value
  39. TenMsecs - Optionally receive the number of tens of milliseconds the NtTime, after
  40. any rounding, is greater than the FatTime
  41. Return Value:
  42. BOOLEAN - TRUE if the Nt time value is within the range of Fat's
  43. time range, and FALSE otherwise
  44. --*/
  45. {
  46. TIME_FIELDS TimeFields;
  47. //
  48. // Convert the input to the a time field record.
  49. //
  50. if (Rounding) {
  51. //
  52. // Add almost two seconds to round up to the nearest double second.
  53. //
  54. NtTime->QuadPart = NtTime->QuadPart + AlmostTwoSeconds;
  55. }
  56. ExSystemTimeToLocalTime( NtTime, NtTime );
  57. RtlTimeToTimeFields( NtTime, &TimeFields );
  58. //
  59. // Check the range of the date found in the time field record
  60. //
  61. if ((TimeFields.Year < 1980) || (TimeFields.Year > (1980 + 127))) {
  62. ExLocalTimeToSystemTime( NtTime, NtTime );
  63. return FALSE;
  64. }
  65. //
  66. // The year will fit in Fat so simply copy over the information
  67. //
  68. FatTime->Time.DoubleSeconds = (USHORT)(TimeFields.Second / 2);
  69. FatTime->Time.Minute = (USHORT)(TimeFields.Minute);
  70. FatTime->Time.Hour = (USHORT)(TimeFields.Hour);
  71. FatTime->Date.Year = (USHORT)(TimeFields.Year - 1980);
  72. FatTime->Date.Month = (USHORT)(TimeFields.Month);
  73. FatTime->Date.Day = (USHORT)(TimeFields.Day);
  74. if (TenMsecs) {
  75. if (!Rounding) {
  76. //
  77. // If the number of seconds was not divisible by two, then there
  78. // is another second of time (1 sec, 3 sec, etc.) Note we round down
  79. // the number of milleconds onto tens of milleseconds boundaries.
  80. //
  81. *TenMsecs = (TimeFields.Milliseconds / 10) +
  82. ((TimeFields.Second % 2) * 100);
  83. } else {
  84. //
  85. // If we rounded up, we have in effect changed the NT time. Therefore,
  86. // it does not differ from the FAT time.
  87. //
  88. *TenMsecs = 0;
  89. }
  90. }
  91. if (Rounding) {
  92. //
  93. // Slice off non-FAT boundary time and convert back to 64bit form
  94. //
  95. TimeFields.Milliseconds = 0;
  96. TimeFields.Second -= TimeFields.Second % 2;
  97. } else {
  98. //
  99. // Round down to 10ms boundary
  100. //
  101. TimeFields.Milliseconds -= TimeFields.Milliseconds % 10;
  102. }
  103. //
  104. // Convert back to NT time
  105. //
  106. (VOID) RtlTimeFieldsToTime(&TimeFields, NtTime);
  107. ExLocalTimeToSystemTime( NtTime, NtTime );
  108. UNREFERENCED_PARAMETER( IrpContext );
  109. return TRUE;
  110. }
  111. LARGE_INTEGER
  112. FatFatDateToNtTime (
  113. IN PIRP_CONTEXT IrpContext,
  114. IN FAT_DATE FatDate
  115. )
  116. /*++
  117. Routine Description:
  118. This routine converts a Fat datev value to its corresponding Nt GMT
  119. Time value.
  120. Arguments:
  121. FatDate - Supplies the Fat Date to convert from
  122. Return Value:
  123. LARGE_INTEGER - Receives the corresponding Nt Time value
  124. --*/
  125. {
  126. TIME_FIELDS TimeFields;
  127. LARGE_INTEGER Time;
  128. //
  129. // Pack the input time/date into a time field record
  130. //
  131. TimeFields.Year = (USHORT)(FatDate.Year + 1980);
  132. TimeFields.Month = (USHORT)(FatDate.Month);
  133. TimeFields.Day = (USHORT)(FatDate.Day);
  134. TimeFields.Hour = (USHORT)0;
  135. TimeFields.Minute = (USHORT)0;
  136. TimeFields.Second = (USHORT)0;
  137. TimeFields.Milliseconds = (USHORT)0;
  138. //
  139. // Convert the time field record to Nt LARGE_INTEGER, and set it to zero
  140. // if we were given a bogus time.
  141. //
  142. if (!RtlTimeFieldsToTime( &TimeFields, &Time )) {
  143. Time.LowPart = 0;
  144. Time.HighPart = 0;
  145. } else {
  146. ExLocalTimeToSystemTime( &Time, &Time );
  147. }
  148. return Time;
  149. UNREFERENCED_PARAMETER( IrpContext );
  150. }
  151. LARGE_INTEGER
  152. FatFatTimeToNtTime (
  153. IN PIRP_CONTEXT IrpContext,
  154. IN FAT_TIME_STAMP FatTime,
  155. IN UCHAR TenMilliSeconds
  156. )
  157. /*++
  158. Routine Description:
  159. This routine converts a Fat time value pair to its corresponding Nt GMT
  160. Time value.
  161. Arguments:
  162. FatTime - Supplies the Fat Time to convert from
  163. TenMilliSeconds - A 10 Milisecond resolution
  164. Return Value:
  165. LARGE_INTEGER - Receives the corresponding Nt GMT Time value
  166. --*/
  167. {
  168. TIME_FIELDS TimeFields;
  169. LARGE_INTEGER Time;
  170. //
  171. // Pack the input time/date into a time field record
  172. //
  173. TimeFields.Year = (USHORT)(FatTime.Date.Year + 1980);
  174. TimeFields.Month = (USHORT)(FatTime.Date.Month);
  175. TimeFields.Day = (USHORT)(FatTime.Date.Day);
  176. TimeFields.Hour = (USHORT)(FatTime.Time.Hour);
  177. TimeFields.Minute = (USHORT)(FatTime.Time.Minute);
  178. TimeFields.Second = (USHORT)(FatTime.Time.DoubleSeconds * 2);
  179. if (TenMilliSeconds != 0) {
  180. TimeFields.Second += (USHORT)(TenMilliSeconds / 100);
  181. TimeFields.Milliseconds = (USHORT)((TenMilliSeconds % 100) * 10);
  182. } else {
  183. TimeFields.Milliseconds = (USHORT)0;
  184. }
  185. //
  186. // If the second value is greater than 59 then we truncate it to 0.
  187. // Note that this can't happen with a proper FAT timestamp.
  188. //
  189. if (TimeFields.Second > 59) {
  190. TimeFields.Second = 0;
  191. }
  192. //
  193. // Convert the time field record to Nt LARGE_INTEGER, and set it to zero
  194. // if we were given a bogus time.
  195. //
  196. if (!RtlTimeFieldsToTime( &TimeFields, &Time )) {
  197. Time.LowPart = 0;
  198. Time.HighPart = 0;
  199. } else {
  200. ExLocalTimeToSystemTime( &Time, &Time );
  201. }
  202. return Time;
  203. UNREFERENCED_PARAMETER( IrpContext );
  204. }
  205. FAT_TIME_STAMP
  206. FatGetCurrentFatTime (
  207. IN PIRP_CONTEXT IrpContext
  208. )
  209. /*++
  210. Routine Description:
  211. This routine returns the current system time in Fat time
  212. Arguments:
  213. Return Value:
  214. FAT_TIME_STAMP - Receives the current system time
  215. --*/
  216. {
  217. LARGE_INTEGER Time;
  218. TIME_FIELDS TimeFields;
  219. FAT_TIME_STAMP FatTime;
  220. //
  221. // Get the current system time, and map it into a time field record.
  222. //
  223. KeQuerySystemTime( &Time );
  224. ExSystemTimeToLocalTime( &Time, &Time );
  225. //
  226. // Always add almost two seconds to round up to the nearest double second.
  227. //
  228. Time.QuadPart = Time.QuadPart + AlmostTwoSeconds;
  229. (VOID)RtlTimeToTimeFields( &Time, &TimeFields );
  230. //
  231. // Now simply copy over the information
  232. //
  233. FatTime.Time.DoubleSeconds = (USHORT)(TimeFields.Second / 2);
  234. FatTime.Time.Minute = (USHORT)(TimeFields.Minute);
  235. FatTime.Time.Hour = (USHORT)(TimeFields.Hour);
  236. FatTime.Date.Year = (USHORT)(TimeFields.Year - 1980);
  237. FatTime.Date.Month = (USHORT)(TimeFields.Month);
  238. FatTime.Date.Day = (USHORT)(TimeFields.Day);
  239. UNREFERENCED_PARAMETER( IrpContext );
  240. return FatTime;
  241. }