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.

1367 lines
36 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. Time.c
  5. Abstract:
  6. This module implements the absolute time conversion routines for NT.
  7. Absolute LARGE_INTEGER in NT is represented by a 64-bit large integer accurate
  8. to 100ns resolution. The smallest time resolution used by this package
  9. is One millisecond. The basis for NT time is the start of 1601 which
  10. was chosen because it is the start of a new quadricentury. Some facts
  11. to note are:
  12. o At 100ns resolution 32 bits is good for about 429 seconds (or 7 minutes)
  13. o At 100ns resolution a large integer (i.e., 63 bits) is good for
  14. about 29,227 years, or around 10,675,199 days
  15. o At 1 second resolution 31 bits is good for about 68 years
  16. o At 1 second resolution 32 bits is good for about 136 years
  17. o 100ns Time (ignoring time less than a millisecond) can be expressed
  18. as two values, Days and Milliseconds. Where Days is the number of
  19. whole days and Milliseconds is the number of milliseconds for the
  20. partial day. Both of these values are ULONG.
  21. Given these facts most of the conversions are done by first splitting
  22. LARGE_INTEGER into Days and Milliseconds.
  23. Author:
  24. Gary Kimura [GaryKi] 26-Aug-1989
  25. Environment:
  26. Pure utility routine
  27. Revision History:
  28. --*/
  29. #include "ntrtlp.h"
  30. #if defined(ALLOC_PRAGMA) && defined(NTOS_KERNEL_RUNTIME)
  31. ULONG
  32. ElapsedDaysToYears (
  33. IN ULONG ElapsedDays
  34. );
  35. static
  36. VOID
  37. TimeToDaysAndFraction (
  38. IN PLARGE_INTEGER Time,
  39. OUT PULONG ElapsedDays,
  40. OUT PULONG Milliseconds
  41. );
  42. VOID
  43. DaysAndFractionToTime (
  44. IN ULONG ElapsedDays,
  45. IN ULONG Milliseconds,
  46. OUT PLARGE_INTEGER Time
  47. );
  48. #pragma alloc_text(PAGE, RtlCutoverTimeToSystemTime)
  49. #pragma alloc_text(PAGE, RtlTimeToElapsedTimeFields)
  50. #pragma alloc_text(PAGE, RtlSystemTimeToLocalTime)
  51. #pragma alloc_text(PAGE, RtlLocalTimeToSystemTime)
  52. #endif
  53. //
  54. // The following two tables map a day offset within a year to the month
  55. // containing the day. Both tables are zero based. For example, day
  56. // offset of 0 to 30 map to 0 (which is Jan).
  57. //
  58. CONST UCHAR LeapYearDayToMonth[366] = {
  59. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // January
  60. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // February
  61. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // March
  62. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // April
  63. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // May
  64. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, // June
  65. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, // July
  66. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // August
  67. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // September
  68. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // October
  69. 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, // November
  70. 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11}; // December
  71. CONST UCHAR NormalYearDayToMonth[365] = {
  72. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // January
  73. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // February
  74. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // March
  75. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // April
  76. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // May
  77. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, // June
  78. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, // July
  79. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // August
  80. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // September
  81. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // October
  82. 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, // November
  83. 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11}; // December
  84. //
  85. // The following two tables map a month index to the number of days preceding
  86. // the month in the year. Both tables are zero based. For example, 1 (Feb)
  87. // has 31 days preceding it. To help calculate the maximum number of days
  88. // in a month each table has 13 entries, so the number of days in a month
  89. // of index i is the table entry of i+1 minus the table entry of i.
  90. //
  91. CONST CSHORT LeapYearDaysPrecedingMonth[13] = {
  92. 0, // January
  93. 31, // February
  94. 31+29, // March
  95. 31+29+31, // April
  96. 31+29+31+30, // May
  97. 31+29+31+30+31, // June
  98. 31+29+31+30+31+30, // July
  99. 31+29+31+30+31+30+31, // August
  100. 31+29+31+30+31+30+31+31, // September
  101. 31+29+31+30+31+30+31+31+30, // October
  102. 31+29+31+30+31+30+31+31+30+31, // November
  103. 31+29+31+30+31+30+31+31+30+31+30, // December
  104. 31+29+31+30+31+30+31+31+30+31+30+31};
  105. CONST CSHORT NormalYearDaysPrecedingMonth[13] = {
  106. 0, // January
  107. 31, // February
  108. 31+28, // March
  109. 31+28+31, // April
  110. 31+28+31+30, // May
  111. 31+28+31+30+31, // June
  112. 31+28+31+30+31+30, // July
  113. 31+28+31+30+31+30+31, // August
  114. 31+28+31+30+31+30+31+31, // September
  115. 31+28+31+30+31+30+31+31+30, // October
  116. 31+28+31+30+31+30+31+31+30+31, // November
  117. 31+28+31+30+31+30+31+31+30+31+30, // December
  118. 31+28+31+30+31+30+31+31+30+31+30+31};
  119. //
  120. // The following definitions and declarations are some important constants
  121. // used in the time conversion routines
  122. //
  123. //
  124. // This is the week day that January 1st, 1601 fell on (a Monday)
  125. //
  126. #define WEEKDAY_OF_1601 1
  127. //
  128. // These are known constants used to convert 1970 and 1980 times to 1601
  129. // times. They are the number of seconds from the 1601 base to the start
  130. // of 1970 and the start of 1980. The number of seconds from 1601 to
  131. // 1970 is 369 years worth, or (369 * 365) + 89 leap days = 134774 days, or
  132. // 134774 * 86400 seconds, which is equal to the large integer defined
  133. // below. The number of seconds from 1601 to 1980 is 379 years worth, or etc.
  134. //
  135. const LARGE_INTEGER SecondsToStartOf1970 = {0xb6109100, 0x00000002};
  136. const LARGE_INTEGER SecondsToStartOf1980 = {0xc8df3700, 0x00000002};
  137. //
  138. // These are the magic numbers needed to do our extended division. The
  139. // only numbers we ever need to divide by are
  140. //
  141. // 10,000 = convert 100ns tics to millisecond tics
  142. //
  143. // 10,000,000 = convert 100ns tics to one second tics
  144. //
  145. // 86,400,000 = convert Millisecond tics to one day tics
  146. //
  147. const LARGE_INTEGER Magic10000 = {0xe219652c, 0xd1b71758};
  148. #define SHIFT10000 13
  149. const LARGE_INTEGER Magic10000000 = {0xe57a42bd, 0xd6bf94d5};
  150. #define SHIFT10000000 23
  151. const LARGE_INTEGER Magic86400000 = {0xfa67b90e, 0xc6d750eb};
  152. #define SHIFT86400000 26
  153. //
  154. // To make the code more readable we'll also define some macros to
  155. // do the actual division for use
  156. //
  157. #define Convert100nsToMilliseconds(LARGE_INTEGER) ( \
  158. RtlExtendedMagicDivide( (LARGE_INTEGER), Magic10000, SHIFT10000 ) \
  159. )
  160. #define ConvertMillisecondsTo100ns(MILLISECONDS) ( \
  161. RtlExtendedIntegerMultiply( (MILLISECONDS), 10000 ) \
  162. )
  163. #define Convert100nsToSeconds(LARGE_INTEGER) ( \
  164. RtlExtendedMagicDivide( (LARGE_INTEGER), Magic10000000, SHIFT10000000 ) \
  165. )
  166. #define ConvertSecondsTo100ns(SECONDS) ( \
  167. RtlExtendedIntegerMultiply( (SECONDS), 10000000 ) \
  168. )
  169. #define ConvertMillisecondsToDays(LARGE_INTEGER) ( \
  170. RtlExtendedMagicDivide( (LARGE_INTEGER), Magic86400000, SHIFT86400000 ) \
  171. )
  172. #define ConvertDaysToMilliseconds(DAYS) ( \
  173. Int32x32To64( (DAYS), 86400000 ) \
  174. )
  175. //
  176. // Local support routine
  177. //
  178. ULONG
  179. ElapsedDaysToYears (
  180. IN ULONG ElapsedDays
  181. )
  182. /*++
  183. Routine Description:
  184. This routine computes the number of total years contained in the indicated
  185. number of elapsed days. The computation is to first compute the number of
  186. 400 years and subtract that it, then do the 100 years and subtract that out,
  187. then do the number of 4 years and subtract that out. Then what we have left
  188. is the number of days with in a normalized 4 year block. Normalized being that
  189. the first three years are not leap years.
  190. Arguments:
  191. ElapsedDays - Supplies the number of days to use
  192. Return Value:
  193. ULONG - Returns the number of whole years contained within the input number
  194. of days.
  195. --*/
  196. {
  197. ULONG NumberOf400s;
  198. ULONG NumberOf100s;
  199. ULONG NumberOf4s;
  200. ULONG Years;
  201. //
  202. // A 400 year time block is 365*400 + 400/4 - 400/100 + 400/400 = 146097 days
  203. // long. So we simply compute the number of whole 400 year block and the
  204. // the number days contained in those whole blocks, and subtract if from the
  205. // elapsed day total
  206. //
  207. NumberOf400s = ElapsedDays / 146097;
  208. ElapsedDays -= NumberOf400s * 146097;
  209. //
  210. // A 100 year time block is 365*100 + 100/4 - 100/100 = 36524 days long.
  211. // The computation for the number of 100 year blocks is biased by 3/4 days per
  212. // 100 years to account for the extra leap day thrown in on the last year
  213. // of each 400 year block.
  214. //
  215. NumberOf100s = (ElapsedDays * 100 + 75) / 3652425;
  216. ElapsedDays -= NumberOf100s * 36524;
  217. //
  218. // A 4 year time block is 365*4 + 4/4 = 1461 days long.
  219. //
  220. NumberOf4s = ElapsedDays / 1461;
  221. ElapsedDays -= NumberOf4s * 1461;
  222. //
  223. // Now the number of whole years is the number of 400 year blocks times 400,
  224. // 100 year blocks time 100, 4 year blocks times 4, and the number of elapsed
  225. // whole years, taking into account the 3/4 day per year needed to handle the
  226. // leap year.
  227. //
  228. Years = (NumberOf400s * 400) +
  229. (NumberOf100s * 100) +
  230. (NumberOf4s * 4) +
  231. (ElapsedDays * 100 + 75) / 36525;
  232. return Years;
  233. }
  234. //
  235. // ULONG
  236. // NumberOfLeapYears (
  237. // IN ULONG ElapsedYears
  238. // );
  239. //
  240. // The number of leap years is simply the number of years divided by 4
  241. // minus years divided by 100 plus years divided by 400. This says
  242. // that every four years is a leap year except centuries, and the
  243. // exception to the exception is the quadricenturies
  244. //
  245. #define NumberOfLeapYears(YEARS) ( \
  246. ((YEARS) / 4) - ((YEARS) / 100) + ((YEARS) / 400) \
  247. )
  248. //
  249. // ULONG
  250. // ElapsedYearsToDays (
  251. // IN ULONG ElapsedYears
  252. // );
  253. //
  254. // The number of days contained in elapsed years is simply the number
  255. // of years times 365 (because every year has at least 365 days) plus
  256. // the number of leap years there are (i.e., the number of 366 days years)
  257. //
  258. #define ElapsedYearsToDays(YEARS) ( \
  259. ((YEARS) * 365) + NumberOfLeapYears(YEARS) \
  260. )
  261. //
  262. // BOOLEAN
  263. // IsLeapYear (
  264. // IN ULONG ElapsedYears
  265. // );
  266. //
  267. // If it is an even 400 or a non century leapyear then the
  268. // answer is true otherwise it's false
  269. //
  270. #define IsLeapYear(YEARS) ( \
  271. (((YEARS) % 400 == 0) || \
  272. ((YEARS) % 100 != 0) && ((YEARS) % 4 == 0)) ? \
  273. TRUE \
  274. : \
  275. FALSE \
  276. )
  277. //
  278. // ULONG
  279. // MaxDaysInMonth (
  280. // IN ULONG Year,
  281. // IN ULONG Month
  282. // );
  283. //
  284. // The maximum number of days in a month depend on the year and month.
  285. // It is the difference between the days to the month and the days
  286. // to the following month
  287. //
  288. #define MaxDaysInMonth(YEAR,MONTH) ( \
  289. IsLeapYear(YEAR) ? \
  290. LeapYearDaysPrecedingMonth[(MONTH) + 1] - \
  291. LeapYearDaysPrecedingMonth[(MONTH)] \
  292. : \
  293. NormalYearDaysPrecedingMonth[(MONTH) + 1] - \
  294. NormalYearDaysPrecedingMonth[(MONTH)] \
  295. )
  296. //
  297. // Internal Support routine
  298. //
  299. static
  300. VOID
  301. TimeToDaysAndFraction (
  302. IN PLARGE_INTEGER Time,
  303. OUT PULONG ElapsedDays,
  304. OUT PULONG Milliseconds
  305. )
  306. /*++
  307. Routine Description:
  308. This routine converts an input 64-bit time value to the number
  309. of total elapsed days and the number of milliseconds in the
  310. partial day.
  311. Arguments:
  312. Time - Supplies the input time to convert from
  313. ElapsedDays - Receives the number of elapsed days
  314. Milliseconds - Receives the number of milliseconds in the partial day
  315. Return Value:
  316. None
  317. --*/
  318. {
  319. LARGE_INTEGER TotalMilliseconds;
  320. LARGE_INTEGER Temp;
  321. //
  322. // Convert the input time to total milliseconds
  323. //
  324. TotalMilliseconds = Convert100nsToMilliseconds( *(PLARGE_INTEGER)Time );
  325. //
  326. // Convert milliseconds to total days
  327. //
  328. Temp = ConvertMillisecondsToDays( TotalMilliseconds );
  329. //
  330. // Set the elapsed days from temp, we've divided it enough so that
  331. // the high part must be zero.
  332. //
  333. *ElapsedDays = Temp.LowPart;
  334. //
  335. // Calculate the exact number of milliseconds in the elapsed days
  336. // and subtract that from the total milliseconds to figure out
  337. // the number of milliseconds left in the partial day
  338. //
  339. Temp.QuadPart = ConvertDaysToMilliseconds( *ElapsedDays );
  340. Temp.QuadPart = TotalMilliseconds.QuadPart - Temp.QuadPart;
  341. //
  342. // Set the fraction part from temp, the total number of milliseconds in
  343. // a day guarantees that the high part must be zero.
  344. //
  345. *Milliseconds = Temp.LowPart;
  346. //
  347. // And return to our caller
  348. //
  349. return;
  350. }
  351. //
  352. // Internal Support routine
  353. //
  354. //static
  355. VOID
  356. DaysAndFractionToTime (
  357. IN ULONG ElapsedDays,
  358. IN ULONG Milliseconds,
  359. OUT PLARGE_INTEGER Time
  360. )
  361. /*++
  362. Routine Description:
  363. This routine converts an input elapsed day count and partial time
  364. in milliseconds to a 64-bit time value.
  365. Arguments:
  366. ElapsedDays - Supplies the number of elapsed days
  367. Milliseconds - Supplies the number of milliseconds in the partial day
  368. Time - Receives the output time to value
  369. Return Value:
  370. None
  371. --*/
  372. {
  373. LARGE_INTEGER Temp;
  374. LARGE_INTEGER Temp2;
  375. //
  376. // Calculate the exact number of milliseconds in the elapsed days.
  377. //
  378. Temp.QuadPart = ConvertDaysToMilliseconds( ElapsedDays );
  379. //
  380. // Convert milliseconds to a large integer
  381. //
  382. Temp2.LowPart = Milliseconds;
  383. Temp2.HighPart = 0;
  384. //
  385. // add milliseconds to the whole day milliseconds
  386. //
  387. Temp.QuadPart = Temp.QuadPart + Temp2.QuadPart;
  388. //
  389. // Finally convert the milliseconds to 100ns resolution
  390. //
  391. *(PLARGE_INTEGER)Time = ConvertMillisecondsTo100ns( Temp );
  392. //
  393. // and return to our caller
  394. //
  395. return;
  396. }
  397. VOID
  398. RtlTimeToTimeFields (
  399. IN PLARGE_INTEGER Time,
  400. OUT PTIME_FIELDS TimeFields
  401. )
  402. /*++
  403. Routine Description:
  404. This routine converts an input 64-bit LARGE_INTEGER variable to its corresponding
  405. time field record. It will tell the caller the year, month, day, hour,
  406. minute, second, millisecond, and weekday corresponding to the input time
  407. variable.
  408. Arguments:
  409. Time - Supplies the time value to interpret
  410. TimeFields - Receives a value corresponding to Time
  411. Return Value:
  412. None
  413. --*/
  414. {
  415. ULONG Years;
  416. ULONG Month;
  417. ULONG Days;
  418. ULONG Hours;
  419. ULONG Minutes;
  420. ULONG Seconds;
  421. ULONG Milliseconds;
  422. //
  423. // First divide the input time 64 bit time variable into
  424. // the number of whole days and part days (in milliseconds)
  425. //
  426. TimeToDaysAndFraction( Time, &Days, &Milliseconds );
  427. //
  428. // Compute which weekday it is and save it away now in the output
  429. // variable. We add the weekday of the base day to bias our computation
  430. // which means that if one day has elapsed then we the weekday we want
  431. // is the Jan 2nd, 1601.
  432. //
  433. TimeFields->Weekday = (CSHORT)((Days + WEEKDAY_OF_1601) % 7);
  434. //
  435. // Calculate the number of whole years contained in the elapsed days
  436. // For example if Days = 500 then Years = 1
  437. //
  438. Years = ElapsedDaysToYears( Days );
  439. //
  440. // And subtract the number of whole years from our elapsed days
  441. // For example if Days = 500, Years = 1, and the new days is equal
  442. // to 500 - 365 (normal year).
  443. //
  444. Days = Days - ElapsedYearsToDays( Years );
  445. //
  446. // Now test whether the year we are working on (i.e., The year
  447. // after the total number of elapsed years) is a leap year
  448. // or not.
  449. //
  450. if (IsLeapYear( Years + 1 )) {
  451. //
  452. // The current year is a leap year, so figure out what month
  453. // it is, and then subtract the number of days preceding the
  454. // month from the days to figure out what day of the month it is
  455. //
  456. Month = LeapYearDayToMonth[Days];
  457. Days = Days - LeapYearDaysPrecedingMonth[Month];
  458. } else {
  459. //
  460. // The current year is a normal year, so figure out the month
  461. // and days as described above for the leap year case
  462. //
  463. Month = NormalYearDayToMonth[Days];
  464. Days = Days - NormalYearDaysPrecedingMonth[Month];
  465. }
  466. //
  467. // Now we need to compute the elapsed hour, minute, second, milliseconds
  468. // from the millisecond variable. This variable currently contains
  469. // the number of milliseconds in our input time variable that did not
  470. // fit into a whole day. To compute the hour, minute, second part
  471. // we will actually do the arithmetic backwards computing milliseconds
  472. // seconds, minutes, and then hours. We start by computing the
  473. // number of whole seconds left in the day, and then computing
  474. // the millisecond remainder.
  475. //
  476. Seconds = Milliseconds / 1000;
  477. Milliseconds = Milliseconds % 1000;
  478. //
  479. // Now we compute the number of whole minutes left in the day
  480. // and the number of remainder seconds
  481. //
  482. Minutes = Seconds / 60;
  483. Seconds = Seconds % 60;
  484. //
  485. // Now compute the number of whole hours left in the day
  486. // and the number of remainder minutes
  487. //
  488. Hours = Minutes / 60;
  489. Minutes = Minutes % 60;
  490. //
  491. // As our final step we put everything into the time fields
  492. // output variable
  493. //
  494. TimeFields->Year = (CSHORT)(Years + 1601);
  495. TimeFields->Month = (CSHORT)(Month + 1);
  496. TimeFields->Day = (CSHORT)(Days + 1);
  497. TimeFields->Hour = (CSHORT)Hours;
  498. TimeFields->Minute = (CSHORT)Minutes;
  499. TimeFields->Second = (CSHORT)Seconds;
  500. TimeFields->Milliseconds = (CSHORT)Milliseconds;
  501. //
  502. // and return to our caller
  503. //
  504. return;
  505. }
  506. BOOLEAN
  507. RtlCutoverTimeToSystemTime(
  508. PTIME_FIELDS CutoverTime,
  509. PLARGE_INTEGER SystemTime,
  510. PLARGE_INTEGER CurrentSystemTime,
  511. BOOLEAN ThisYear
  512. )
  513. {
  514. TIME_FIELDS CurrentTimeFields;
  515. //
  516. // Get the current system time
  517. //
  518. RtlTimeToTimeFields(CurrentSystemTime,&CurrentTimeFields);
  519. //
  520. // check for absolute time field. If the year is specified,
  521. // the the time is an abosulte time
  522. //
  523. if ( CutoverTime->Year ) {
  524. //
  525. // Convert this to a time value and make sure it
  526. // is greater than the current system time
  527. //
  528. if ( !RtlTimeFieldsToTime(CutoverTime,SystemTime) ) {
  529. return FALSE;
  530. }
  531. if (SystemTime->QuadPart < CurrentSystemTime->QuadPart) {
  532. return FALSE;
  533. }
  534. return TRUE;
  535. }
  536. else {
  537. TIME_FIELDS WorkingTimeField;
  538. TIME_FIELDS ScratchTimeField;
  539. LARGE_INTEGER ScratchTime;
  540. CSHORT BestWeekdayDate;
  541. CSHORT WorkingWeekdayNumber;
  542. CSHORT TargetWeekdayNumber;
  543. CSHORT TargetYear;
  544. CSHORT TargetMonth;
  545. CSHORT TargetWeekday; // range [0..6] == [Sunday..Saturday]
  546. BOOLEAN MonthMatches;
  547. //
  548. // The time is an day in the month style time
  549. //
  550. // the convention is the Day is 1-5 specifying 1st, 2nd... Last
  551. // day within the month. The day is WeekDay.
  552. //
  553. //
  554. // Compute the target month and year
  555. //
  556. TargetWeekdayNumber = CutoverTime->Day;
  557. if ( TargetWeekdayNumber > 5 || TargetWeekdayNumber == 0 ) {
  558. return FALSE;
  559. }
  560. TargetWeekday = CutoverTime->Weekday;
  561. TargetMonth = CutoverTime->Month;
  562. MonthMatches = FALSE;
  563. if ( !ThisYear ) {
  564. if ( TargetMonth < CurrentTimeFields.Month ) {
  565. TargetYear = CurrentTimeFields.Year + 1;
  566. }
  567. else if ( TargetMonth > CurrentTimeFields.Month ) {
  568. TargetYear = CurrentTimeFields.Year;
  569. }
  570. else {
  571. TargetYear = CurrentTimeFields.Year;
  572. MonthMatches = TRUE;
  573. }
  574. }
  575. else {
  576. TargetYear = CurrentTimeFields.Year;
  577. }
  578. try_next_year:
  579. BestWeekdayDate = 0;
  580. WorkingTimeField.Year = TargetYear;
  581. WorkingTimeField.Month = TargetMonth;
  582. WorkingTimeField.Day = 1;
  583. WorkingTimeField.Hour = CutoverTime->Hour;
  584. WorkingTimeField.Minute = CutoverTime->Minute;
  585. WorkingTimeField.Second = CutoverTime->Second;
  586. WorkingTimeField.Milliseconds = CutoverTime->Milliseconds;
  587. WorkingTimeField.Weekday = 0;
  588. //
  589. // Convert to time and then back to time fields so we can determine
  590. // the weekday of day 1 on the month
  591. //
  592. if ( !RtlTimeFieldsToTime(&WorkingTimeField,&ScratchTime) ) {
  593. return FALSE;
  594. }
  595. RtlTimeToTimeFields(&ScratchTime,&ScratchTimeField);
  596. //
  597. // Compute bias to target weekday
  598. //
  599. if ( ScratchTimeField.Weekday > TargetWeekday ) {
  600. WorkingTimeField.Day += (7-(ScratchTimeField.Weekday - TargetWeekday));
  601. }
  602. else if ( ScratchTimeField.Weekday < TargetWeekday ) {
  603. WorkingTimeField.Day += (TargetWeekday - ScratchTimeField.Weekday);
  604. }
  605. //
  606. // We are now at the first weekday that matches our target weekday
  607. //
  608. BestWeekdayDate = WorkingTimeField.Day;
  609. WorkingWeekdayNumber = 1;
  610. //
  611. // Keep going one week at a time until we either pass the
  612. // target weekday, or we match exactly
  613. //
  614. while ( WorkingWeekdayNumber < TargetWeekdayNumber ) {
  615. WorkingTimeField.Day += 7;
  616. if ( !RtlTimeFieldsToTime(&WorkingTimeField,&ScratchTime) ) {
  617. break;
  618. }
  619. RtlTimeToTimeFields(&ScratchTime,&ScratchTimeField);
  620. WorkingWeekdayNumber++;
  621. BestWeekdayDate = ScratchTimeField.Day;
  622. }
  623. WorkingTimeField.Day = BestWeekdayDate;
  624. //
  625. // If the months match, and the date is less than the current
  626. // date, then be have to go to next year.
  627. //
  628. if ( !RtlTimeFieldsToTime(&WorkingTimeField,&ScratchTime) ) {
  629. return FALSE;
  630. }
  631. if ( MonthMatches ) {
  632. if ( WorkingTimeField.Day < CurrentTimeFields.Day ) {
  633. MonthMatches = FALSE;
  634. TargetYear++;
  635. goto try_next_year;
  636. }
  637. if ( WorkingTimeField.Day == CurrentTimeFields.Day ) {
  638. if (ScratchTime.QuadPart < CurrentSystemTime->QuadPart) {
  639. MonthMatches = FALSE;
  640. TargetYear++;
  641. goto try_next_year;
  642. }
  643. }
  644. }
  645. *SystemTime = ScratchTime;
  646. return TRUE;
  647. }
  648. }
  649. BOOLEAN
  650. RtlTimeFieldsToTime (
  651. IN PTIME_FIELDS TimeFields,
  652. OUT PLARGE_INTEGER Time
  653. )
  654. /*++
  655. Routine Description:
  656. This routine converts an input Time Field variable to a 64-bit NT time
  657. value. It ignores the WeekDay of the time field.
  658. Arguments:
  659. TimeFields - Supplies the time field record to use
  660. Time - Receives the NT Time corresponding to TimeFields
  661. Return Value:
  662. BOOLEAN - TRUE if the Time Fields is well formed and within the
  663. range of time expressible by LARGE_INTEGER and FALSE otherwise.
  664. --*/
  665. {
  666. ULONG Year;
  667. ULONG Month;
  668. ULONG Day;
  669. ULONG Hour;
  670. ULONG Minute;
  671. ULONG Second;
  672. ULONG Milliseconds;
  673. ULONG ElapsedDays;
  674. ULONG ElapsedMilliseconds;
  675. //
  676. // Load the time field elements into local variables. This should
  677. // ensure that the compiler will only load the input elements
  678. // once, even if there are alias problems. It will also make
  679. // everything (except the year) zero based. We cannot zero base the
  680. // year because then we can't recognize cases where we're given a year
  681. // before 1601.
  682. //
  683. Year = TimeFields->Year;
  684. Month = TimeFields->Month - 1;
  685. Day = TimeFields->Day - 1;
  686. Hour = TimeFields->Hour;
  687. Minute = TimeFields->Minute;
  688. Second = TimeFields->Second;
  689. Milliseconds = TimeFields->Milliseconds;
  690. //
  691. // Check that the time field input variable contains
  692. // proper values.
  693. //
  694. //
  695. // Year 30827 check: Time (in 100ns units) is stored in a
  696. // 64-bit integer, rooted at 1/1/1601.
  697. //
  698. // 2^63 / (10^7 * 86400) = 10675199 days
  699. // 10675199 / 146097 = 73 400-year chunks, 10118 days
  700. // 10118 / 1461 = 6 4-year chunks, 1352 days
  701. // 1352 / 365 = 3 years, some residual days
  702. // 1600 + 73*400 + 6*4 + 3 = 30827 is last year fully
  703. // supported.
  704. //
  705. // I'm guessing it's undesirable to support part of the
  706. // year 30828.
  707. //
  708. if ((TimeFields->Month < 1) ||
  709. (TimeFields->Day < 1) ||
  710. (Year < 1601) ||
  711. (Year > 30827) ||
  712. (Month > 11) ||
  713. ((CSHORT)Day >= MaxDaysInMonth(Year, Month)) ||
  714. (Hour > 23) ||
  715. (Minute > 59) ||
  716. (Second > 59) ||
  717. (Milliseconds > 999)) {
  718. return FALSE;
  719. }
  720. //
  721. // Compute the total number of elapsed days represented by the
  722. // input time field variable
  723. //
  724. ElapsedDays = ElapsedYearsToDays( Year - 1601 );
  725. if (IsLeapYear( Year - 1600 )) {
  726. ElapsedDays += LeapYearDaysPrecedingMonth[ Month ];
  727. } else {
  728. ElapsedDays += NormalYearDaysPrecedingMonth[ Month ];
  729. }
  730. ElapsedDays += Day;
  731. //
  732. // Now compute the total number of milliseconds in the fractional
  733. // part of the day
  734. //
  735. ElapsedMilliseconds = (((Hour*60) + Minute)*60 + Second)*1000 + Milliseconds;
  736. //
  737. // Given the elapsed days and milliseconds we can now build
  738. // the output time variable
  739. //
  740. DaysAndFractionToTime( ElapsedDays, ElapsedMilliseconds, Time );
  741. //
  742. // And return to our caller
  743. //
  744. return TRUE;
  745. }
  746. VOID
  747. RtlTimeToElapsedTimeFields (
  748. IN PLARGE_INTEGER Time,
  749. OUT PTIME_FIELDS TimeFields
  750. )
  751. /*++
  752. Routine Description:
  753. This routine converts an input 64-bit LARGE_INTEGER variable to its corresponding
  754. time field record. The input time is the elapsed time (difference
  755. between to times). It will tell the caller the number of days, hour,
  756. minute, second, and milliseconds that the elapsed time represents.
  757. Arguments:
  758. Time - Supplies the time value to interpret
  759. TimeFields - Receives a value corresponding to Time
  760. Return Value:
  761. None
  762. --*/
  763. {
  764. ULONG Days;
  765. ULONG Hours;
  766. ULONG Minutes;
  767. ULONG Seconds;
  768. ULONG Milliseconds;
  769. //
  770. // First divide the input time 64 bit time variable into
  771. // the number of whole days and part days (in milliseconds)
  772. //
  773. TimeToDaysAndFraction( Time, &Days, &Milliseconds );
  774. //
  775. // Now we need to compute the elapsed hour, minute, second, milliseconds
  776. // from the millisecond variable. This variable currently contains
  777. // the number of milliseconds in our input time variable that did not
  778. // fit into a whole day. To compute the hour, minute, second part
  779. // we will actually do the arithmetic backwards computing milliseconds
  780. // seconds, minutes, and then hours. We start by computing the
  781. // number of whole seconds left in the day, and then computing
  782. // the millisecond remainder.
  783. //
  784. Seconds = Milliseconds / 1000;
  785. Milliseconds = Milliseconds % 1000;
  786. //
  787. // Now we compute the number of whole minutes left in the day
  788. // and the number of remainder seconds
  789. //
  790. Minutes = Seconds / 60;
  791. Seconds = Seconds % 60;
  792. //
  793. // Now compute the number of whole hours left in the day
  794. // and the number of remainder minutes
  795. //
  796. Hours = Minutes / 60;
  797. Minutes = Minutes % 60;
  798. //
  799. // As our final step we put everything into the time fields
  800. // output variable
  801. //
  802. TimeFields->Year = 0;
  803. TimeFields->Month = 0;
  804. TimeFields->Day = (CSHORT)Days;
  805. TimeFields->Hour = (CSHORT)Hours;
  806. TimeFields->Minute = (CSHORT)Minutes;
  807. TimeFields->Second = (CSHORT)Seconds;
  808. TimeFields->Milliseconds = (CSHORT)Milliseconds;
  809. //
  810. // and return to our caller
  811. //
  812. return;
  813. }
  814. BOOLEAN
  815. RtlTimeToSecondsSince1980 (
  816. IN PLARGE_INTEGER Time,
  817. OUT PULONG ElapsedSeconds
  818. )
  819. /*++
  820. Routine Description:
  821. This routine converts an input 64-bit NT Time variable to the
  822. number of seconds since the start of 1980. The NT time must be
  823. within the range 1980 to around 2115.
  824. Arguments:
  825. Time - Supplies the Time to convert from
  826. ElapsedSeconds - Receives the number of seconds since the start of 1980
  827. denoted by Time
  828. Return Value:
  829. BOOLEAN - TRUE if the input Time is within a range expressible by
  830. ElapsedSeconds and FALSE otherwise
  831. --*/
  832. {
  833. LARGE_INTEGER Seconds;
  834. //
  835. // First convert time to seconds since 1601
  836. //
  837. Seconds = Convert100nsToSeconds( *(PLARGE_INTEGER)Time );
  838. //
  839. // Then subtract the number of seconds from 1601 to 1980.
  840. //
  841. Seconds.QuadPart = Seconds.QuadPart - SecondsToStartOf1980.QuadPart;
  842. //
  843. // If the results is negative then the date was before 1980 or if
  844. // the results is greater than a ulong then its too far in the
  845. // future so we return FALSE
  846. //
  847. if (Seconds.HighPart != 0) {
  848. return FALSE;
  849. }
  850. //
  851. // Otherwise we have the answer
  852. //
  853. *ElapsedSeconds = Seconds.LowPart;
  854. //
  855. // And return to our caller
  856. //
  857. return TRUE;
  858. }
  859. VOID
  860. RtlSecondsSince1980ToTime (
  861. IN ULONG ElapsedSeconds,
  862. OUT PLARGE_INTEGER Time
  863. )
  864. /*++
  865. Routine Description:
  866. This routine converts the seconds since the start of 1980 to an
  867. NT Time value.
  868. Arguments:
  869. ElapsedSeconds - Supplies the number of seconds from the start of 1980
  870. to convert from
  871. Time - Receives the converted Time value
  872. Return Value:
  873. None
  874. --*/
  875. {
  876. LARGE_INTEGER Seconds;
  877. //
  878. // Move elapsed seconds to a large integer
  879. //
  880. Seconds.LowPart = ElapsedSeconds;
  881. Seconds.HighPart = 0;
  882. //
  883. // convert number of seconds from 1980 to number of seconds from 1601
  884. //
  885. Seconds.QuadPart = Seconds.QuadPart + SecondsToStartOf1980.QuadPart;
  886. //
  887. // Convert seconds to 100ns resolution
  888. //
  889. *(PLARGE_INTEGER)Time = ConvertSecondsTo100ns( Seconds );
  890. //
  891. // and return to our caller
  892. //
  893. return;
  894. }
  895. BOOLEAN
  896. RtlTimeToSecondsSince1970 (
  897. IN PLARGE_INTEGER Time,
  898. OUT PULONG ElapsedSeconds
  899. )
  900. /*++
  901. Routine Description:
  902. This routine converts an input 64-bit NT Time variable to the
  903. number of seconds since the start of 1970. The NT time must be
  904. within the range 1970 to around 2105.
  905. Arguments:
  906. Time - Supplies the Time to convert from
  907. ElapsedSeconds - Receives the number of seconds since the start of 1970
  908. denoted by Time
  909. Return Value:
  910. BOOLEAN - TRUE if the input time is within the range expressible by
  911. ElapsedSeconds and FALSE otherwise
  912. --*/
  913. {
  914. LARGE_INTEGER Seconds;
  915. //
  916. // First convert time to seconds since 1601
  917. //
  918. Seconds = Convert100nsToSeconds( *(PLARGE_INTEGER)Time );
  919. //
  920. // Then subtract the number of seconds from 1601 to 1970.
  921. //
  922. Seconds.QuadPart = Seconds.QuadPart - SecondsToStartOf1970.QuadPart;
  923. //
  924. // If the results is negative then the date was before 1970 or if
  925. // the results is greater than a ulong then its too far in the
  926. // future so we return FALSE
  927. //
  928. if (Seconds.HighPart != 0) {
  929. return FALSE;
  930. }
  931. //
  932. // Otherwise we have the answer
  933. //
  934. *ElapsedSeconds = Seconds.LowPart;
  935. //
  936. // And return to our caller
  937. //
  938. return TRUE;
  939. }
  940. VOID
  941. RtlSecondsSince1970ToTime (
  942. IN ULONG ElapsedSeconds,
  943. OUT PLARGE_INTEGER Time
  944. )
  945. /*++
  946. Routine Description:
  947. This routine converts the seconds since the start of 1970 to an
  948. NT Time value
  949. Arguments:
  950. ElapsedSeconds - Supplies the number of seconds from the start of 1970
  951. to convert from
  952. Time - Receives the converted Time value
  953. Return Value:
  954. None
  955. --*/
  956. {
  957. LARGE_INTEGER Seconds;
  958. //
  959. // Move elapsed seconds to a large integer
  960. //
  961. Seconds.LowPart = ElapsedSeconds;
  962. Seconds.HighPart = 0;
  963. //
  964. // Convert number of seconds from 1970 to number of seconds from 1601
  965. //
  966. Seconds.QuadPart = Seconds.QuadPart + SecondsToStartOf1970.QuadPart;
  967. //
  968. // Convert seconds to 100ns resolution
  969. //
  970. *(PLARGE_INTEGER)Time = ConvertSecondsTo100ns( Seconds );
  971. //
  972. // return to our caller
  973. //
  974. return;
  975. }
  976. NTSTATUS
  977. RtlSystemTimeToLocalTime (
  978. IN PLARGE_INTEGER SystemTime,
  979. OUT PLARGE_INTEGER LocalTime
  980. )
  981. {
  982. NTSTATUS Status;
  983. SYSTEM_TIMEOFDAY_INFORMATION TimeOfDay;
  984. Status = ZwQuerySystemInformation(
  985. SystemTimeOfDayInformation,
  986. &TimeOfDay,
  987. sizeof(TimeOfDay),
  988. NULL
  989. );
  990. if ( !NT_SUCCESS(Status) ) {
  991. return Status;
  992. }
  993. //
  994. // LocalTime = SystemTime - TimeZoneBias
  995. //
  996. LocalTime->QuadPart = SystemTime->QuadPart - TimeOfDay.TimeZoneBias.QuadPart;
  997. return STATUS_SUCCESS;
  998. }
  999. NTSTATUS
  1000. RtlLocalTimeToSystemTime (
  1001. IN PLARGE_INTEGER LocalTime,
  1002. OUT PLARGE_INTEGER SystemTime
  1003. )
  1004. {
  1005. NTSTATUS Status;
  1006. SYSTEM_TIMEOFDAY_INFORMATION TimeOfDay;
  1007. Status = ZwQuerySystemInformation(
  1008. SystemTimeOfDayInformation,
  1009. &TimeOfDay,
  1010. sizeof(TimeOfDay),
  1011. NULL
  1012. );
  1013. if ( !NT_SUCCESS(Status) ) {
  1014. return Status;
  1015. }
  1016. //
  1017. // SystemTime = LocalTime + TimeZoneBias
  1018. //
  1019. SystemTime->QuadPart = LocalTime->QuadPart + TimeOfDay.TimeZoneBias.QuadPart;
  1020. return STATUS_SUCCESS;
  1021. }