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.

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