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.

880 lines
25 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. efitimefunc.cxx
  5. --*/
  6. #include<pch.cxx>
  7. static
  8. VOID
  9. DaysAndFractionToTime (
  10. IN ULONG ElapsedDays,
  11. IN ULONG Milliseconds,
  12. OUT PLARGE_INTEGER Time
  13. );
  14. static
  15. VOID
  16. TimeToDaysAndFraction (
  17. IN PLARGE_INTEGER Time,
  18. OUT PULONG ElapsedDays,
  19. OUT PULONG Milliseconds
  20. );
  21. //
  22. // The following two tables map a day offset within a year to the month
  23. // containing the day. Both tables are zero based. For example, day
  24. // offset of 0 to 30 map to 0 (which is Jan).
  25. //
  26. CONST UCHAR LeapYearDayToMonth[366] = {
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. 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
  38. 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
  39. CONST UCHAR NormalYearDayToMonth[365] = {
  40. 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
  41. 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
  42. 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
  43. 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
  44. 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
  45. 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
  46. 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
  47. 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
  48. 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
  49. 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
  50. 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
  51. 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
  52. //
  53. // The following two tables map a month index to the number of days preceding
  54. // the month in the year. Both tables are zero based. For example, 1 (Feb)
  55. // has 31 days preceding it. To help calculate the maximum number of days
  56. // in a month each table has 13 entries, so the number of days in a month
  57. // of index i is the table entry of i+1 minus the table entry of i.
  58. //
  59. CONST CSHORT LeapYearDaysPrecedingMonth[13] = {
  60. 0, // January
  61. 31, // February
  62. 31+29, // March
  63. 31+29+31, // April
  64. 31+29+31+30, // May
  65. 31+29+31+30+31, // June
  66. 31+29+31+30+31+30, // July
  67. 31+29+31+30+31+30+31, // August
  68. 31+29+31+30+31+30+31+31, // September
  69. 31+29+31+30+31+30+31+31+30, // October
  70. 31+29+31+30+31+30+31+31+30+31, // November
  71. 31+29+31+30+31+30+31+31+30+31+30, // December
  72. 31+29+31+30+31+30+31+31+30+31+30+31};
  73. CONST CSHORT NormalYearDaysPrecedingMonth[13] = {
  74. 0, // January
  75. 31, // February
  76. 31+28, // March
  77. 31+28+31, // April
  78. 31+28+31+30, // May
  79. 31+28+31+30+31, // June
  80. 31+28+31+30+31+30, // July
  81. 31+28+31+30+31+30+31, // August
  82. 31+28+31+30+31+30+31+31, // September
  83. 31+28+31+30+31+30+31+31+30, // October
  84. 31+28+31+30+31+30+31+31+30+31, // November
  85. 31+28+31+30+31+30+31+31+30+31+30, // December
  86. 31+28+31+30+31+30+31+31+30+31+30+31};
  87. //
  88. // The following definitions and declarations are some important constants
  89. // used in the time conversion routines
  90. //
  91. //
  92. // This is the week day that January 1st, 1601 fell on (a Monday)
  93. //
  94. #define WEEKDAY_OF_1601 1
  95. #define RtlExtendedMagicDivide( A, B, C ) \
  96. (LARGE_INTEGER)(A.QuadPart)/(B.QuadPart)
  97. //
  98. // These are known constants used to convert 1970 and 1980 times to 1601
  99. // times. They are the number of seconds from the 1601 base to the start
  100. // of 1970 and the start of 1980. The number of seconds from 1601 to
  101. // 1970 is 369 years worth, or (369 * 365) + 89 leap days = 134774 days, or
  102. // 134774 * 864000 seconds, which is equal to the large integer defined
  103. // below. The number of seconds from 1601 to 1980 is 379 years worth, or etc.
  104. //
  105. const LARGE_INTEGER SecondsToStartOf1970 = {0xb6109100, 0x00000002};
  106. const LARGE_INTEGER SecondsToStartOf1980 = {0xc8df3700, 0x00000002};
  107. //
  108. // These are the magic numbers needed to do our extended division. The
  109. // only numbers we ever need to divide by are
  110. //
  111. // 10,000 = convert 100ns tics to millisecond tics
  112. //
  113. // 10,000,000 = convert 100ns tics to one second tics
  114. //
  115. // 86,400,000 = convert Millisecond tics to one day tics
  116. //
  117. const LARGE_INTEGER Magic10000 = {0xe219652c, 0xd1b71758};
  118. #define SHIFT10000 13
  119. const LARGE_INTEGER Magic10000000 = {0xe57a42bd, 0xd6bf94d5};
  120. #define SHIFT10000000 23
  121. const LARGE_INTEGER Magic86400000 = {0xfa67b90e, 0xc6d750eb};
  122. #define SHIFT86400000 26
  123. //
  124. // To make the code more readable we'll also define some macros to
  125. // do the actual division for use
  126. //
  127. LARGE_INTEGER Convert100nsToMilliseconds(LARGE_INTEGER a );
  128. LARGE_INTEGER ConvertMillisecondsTo100ns(LARGE_INTEGER a);
  129. LARGE_INTEGER Convert100nsToSeconds(LARGE_INTEGER a );
  130. LARGE_INTEGER ConvertSecondsTo100ns(LARGE_INTEGER a);
  131. LARGE_INTEGER ConvertMillisecondsToDays(LARGE_INTEGER a);
  132. ULONGLONG ConvertDaysToMilliseconds(ULONG a );
  133. //
  134. // ULONG
  135. // ElapsedYearsToDays (
  136. // IN ULONG ElapsedYears
  137. // );
  138. //
  139. // The number of days contained in elapsed years is simply the number
  140. // of years times 365 (because every year has at least 365 days) plus
  141. // the number of leap years there are (i.e., the number of 366 days years)
  142. //
  143. #define ElapsedYearsToDays(YEARS) ( \
  144. ((YEARS) * 365) + NumberOfLeapYears(YEARS) \
  145. )
  146. ULONG
  147. ElapsedDaysToYears (
  148. IN ULONG ElapsedDays
  149. );
  150. //
  151. // BOOLEAN
  152. // IsLeapYear (
  153. // IN ULONG ElapsedYears
  154. // );
  155. //
  156. // If it is an even 400 or a non century leapyear then the
  157. // answer is true otherwise it's false
  158. //
  159. #define IsLeapYear(YEARS) ( \
  160. (((YEARS) % 400 == 0) || \
  161. ((YEARS) % 100 != 0) && ((YEARS) % 4 == 0)) ? \
  162. TRUE \
  163. : \
  164. FALSE \
  165. )
  166. //
  167. // ULONG
  168. // MaxDaysInMonth (
  169. // IN ULONG Year,
  170. // IN ULONG Month
  171. // );
  172. //
  173. // The maximum number of days in a month depend on the year and month.
  174. // It is the difference between the days to the month and the days
  175. // to the following month
  176. //
  177. #define MaxDaysInMonth(YEAR,MONTH) ( \
  178. IsLeapYear(YEAR) ? \
  179. LeapYearDaysPrecedingMonth[(MONTH) + 1] - \
  180. LeapYearDaysPrecedingMonth[(MONTH)] \
  181. : \
  182. NormalYearDaysPrecedingMonth[(MONTH) + 1] - \
  183. NormalYearDaysPrecedingMonth[(MONTH)] \
  184. )
  185. //
  186. // ULONG
  187. // NumberOfLeapYears (
  188. // IN ULONG ElapsedYears
  189. // );
  190. //
  191. // The number of leap years is simply the number of years divided by 4
  192. // minus years divided by 100 plus years divided by 400. This says
  193. // that every four years is a leap year except centuries, and the
  194. // exception to the exception is the quadricenturies
  195. //
  196. #define NumberOfLeapYears(YEARS) ( \
  197. ((YEARS) / 4) - ((YEARS) / 100) + ((YEARS) / 400) \
  198. )
  199. LARGE_INTEGER Convert100nsToMilliseconds(LARGE_INTEGER a )
  200. {
  201. LARGE_INTEGER result;
  202. result.QuadPart = ((LONGLONG) (a.QuadPart)) / Magic10000.QuadPart;
  203. return (result);
  204. }
  205. LARGE_INTEGER ConvertMillisecondsTo100ns(LARGE_INTEGER a)
  206. {
  207. LARGE_INTEGER result;
  208. result.QuadPart = ((LONGLONG) a.QuadPart) * 10000;
  209. return (result);
  210. }
  211. LARGE_INTEGER Convert100nsToSeconds(LARGE_INTEGER a )
  212. {
  213. LARGE_INTEGER result;
  214. result.QuadPart = ((LONGLONG) (a.QuadPart)) / Magic10000000.QuadPart;
  215. return (result);
  216. }
  217. LARGE_INTEGER ConvertSecondsTo100ns(LARGE_INTEGER a)
  218. {
  219. LARGE_INTEGER result;
  220. result.QuadPart = ((LONGLONG) a.QuadPart) * 10000000;
  221. return (result);
  222. }
  223. LARGE_INTEGER ConvertMillisecondsToDays(LARGE_INTEGER a)
  224. {
  225. LARGE_INTEGER result;
  226. result.QuadPart = ((LONGLONG)(a.QuadPart)) / Magic86400000.QuadPart;
  227. return (result);
  228. }
  229. ULONGLONG ConvertDaysToMilliseconds(ULONG a )
  230. {
  231. return ((LONGLONG) a) * 86400000;
  232. }
  233. //
  234. // ULONG
  235. // ElapsedYearsToDays (
  236. // IN ULONG ElapsedYears
  237. // );
  238. //
  239. // The number of days contained in elapsed years is simply the number
  240. // of years times 365 (because every year has at least 365 days) plus
  241. // the number of leap years there are (i.e., the number of 366 days years)
  242. //
  243. #define ElapsedYearsToDays(YEARS) ( \
  244. ((YEARS) * 365) + NumberOfLeapYears(YEARS) \
  245. )
  246. ULONG
  247. ElapsedDaysToYears (
  248. IN ULONG ElapsedDays
  249. )
  250. /*++
  251. Routine Description:
  252. This routine computes the number of total years contained in the indicated
  253. number of elapsed days. The computation is to first compute the number of
  254. 400 years and subtract that it, then do the 100 years and subtract that out,
  255. then do the number of 4 years and subtract that out. Then what we have left
  256. is the number of days with in a normalized 4 year block. Normalized being that
  257. the first three years are not leap years.
  258. Arguments:
  259. ElapsedDays - Supplies the number of days to use
  260. Return Value:
  261. ULONG - Returns the number of whole years contained within the input number
  262. of days.
  263. --*/
  264. {
  265. ULONG NumberOf400s;
  266. ULONG NumberOf100s;
  267. ULONG NumberOf4s;
  268. ULONG Years;
  269. //
  270. // A 400 year time block is 365*400 + 400/4 - 400/100 + 400/400 = 146097 days
  271. // long. So we simply compute the number of whole 400 year block and the
  272. // the number days contained in those whole blocks, and subtract if from the
  273. // elapsed day total
  274. //
  275. NumberOf400s = ElapsedDays / 146097;
  276. ElapsedDays -= NumberOf400s * 146097;
  277. //
  278. // A 100 year time block is 365*100 + 100/4 - 100/100 = 36524 days long.
  279. // The computation for the number of 100 year blocks is biased by 3/4 days per
  280. // 100 years to account for the extra leap day thrown in on the last year
  281. // of each 400 year block.
  282. //
  283. NumberOf100s = (ElapsedDays * 100 + 75) / 3652425;
  284. ElapsedDays -= NumberOf100s * 36524;
  285. //
  286. // A 4 year time block is 365*4 + 4/4 = 1461 days long.
  287. //
  288. NumberOf4s = ElapsedDays / 1461;
  289. ElapsedDays -= NumberOf4s * 1461;
  290. //
  291. // Now the number of whole years is the number of 400 year blocks times 400,
  292. // 100 year blocks time 100, 4 year blocks times 4, and the number of elapsed
  293. // whole years, taking into account the 3/4 day per year needed to handle the
  294. // leap year.
  295. //
  296. Years = (NumberOf400s * 400) +
  297. (NumberOf100s * 100) +
  298. (NumberOf4s * 4) +
  299. (ElapsedDays * 100 + 75) / 36525;
  300. return Years;
  301. }
  302. //
  303. // BOOLEAN
  304. // IsLeapYear (
  305. // IN ULONG ElapsedYears
  306. // );
  307. //
  308. // If it is an even 400 or a non century leapyear then the
  309. // answer is true otherwise it's false
  310. //
  311. #define IsLeapYear(YEARS) ( \
  312. (((YEARS) % 400 == 0) || \
  313. ((YEARS) % 100 != 0) && ((YEARS) % 4 == 0)) ? \
  314. TRUE \
  315. : \
  316. FALSE \
  317. )
  318. //
  319. // ULONG
  320. // MaxDaysInMonth (
  321. // IN ULONG Year,
  322. // IN ULONG Month
  323. // );
  324. //
  325. // The maximum number of days in a month depend on the year and month.
  326. // It is the difference between the days to the month and the days
  327. // to the following month
  328. //
  329. #define MaxDaysInMonth(YEAR,MONTH) ( \
  330. IsLeapYear(YEAR) ? \
  331. LeapYearDaysPrecedingMonth[(MONTH) + 1] - \
  332. LeapYearDaysPrecedingMonth[(MONTH)] \
  333. : \
  334. NormalYearDaysPrecedingMonth[(MONTH) + 1] - \
  335. NormalYearDaysPrecedingMonth[(MONTH)] \
  336. )
  337. //
  338. // ULONG
  339. // NumberOfLeapYears (
  340. // IN ULONG ElapsedYears
  341. // );
  342. //
  343. // The number of leap years is simply the number of years divided by 4
  344. // minus years divided by 100 plus years divided by 400. This says
  345. // that every four years is a leap year except centuries, and the
  346. // exception to the exception is the quadricenturies
  347. //
  348. #define NumberOfLeapYears(YEARS) ( \
  349. ((YEARS) / 4) - ((YEARS) / 100) + ((YEARS) / 400) \
  350. )
  351. //static
  352. VOID
  353. DaysAndFractionToTime (
  354. IN ULONG ElapsedDays,
  355. IN ULONG Milliseconds,
  356. OUT PLARGE_INTEGER Time
  357. )
  358. /*++
  359. Routine Description:
  360. This routine converts an input elapsed day count and partial time
  361. in milliseconds to a 64-bit time value.
  362. Arguments:
  363. ElapsedDays - Supplies the number of elapsed days
  364. Milliseconds - Supplies the number of milliseconds in the partial day
  365. Time - Receives the output time to value
  366. Return Value:
  367. None
  368. --*/
  369. {
  370. LARGE_INTEGER Temp;
  371. LARGE_INTEGER Temp2;
  372. //
  373. // Calculate the exact number of milliseconds in the elapsed days.
  374. //
  375. Temp.QuadPart = ConvertDaysToMilliseconds( ElapsedDays );
  376. //
  377. // Convert milliseconds to a large integer
  378. //
  379. Temp2.LowPart = Milliseconds;
  380. Temp2.HighPart = 0;
  381. //
  382. // add milliseconds to the whole day milliseconds
  383. //
  384. Temp.QuadPart = Temp.QuadPart + Temp2.QuadPart;
  385. //
  386. // Finally convert the milliseconds to 100ns resolution
  387. //
  388. *(PLARGE_INTEGER)Time = ConvertMillisecondsTo100ns( Temp );
  389. //
  390. // and return to our caller
  391. //
  392. return;
  393. }
  394. BOOLEAN
  395. RtlTimeFieldsToTime (
  396. IN PTIME_FIELDS TimeFields,
  397. OUT PLARGE_INTEGER Time
  398. )
  399. /*++
  400. Routine Description:
  401. This routine converts an input Time Field variable to a 64-bit NT time
  402. value. It ignores the WeekDay of the time field.
  403. Arguments:
  404. TimeFields - Supplies the time field record to use
  405. Time - Receives the NT Time corresponding to TimeFields
  406. Return Value:
  407. BOOLEAN - TRUE if the Time Fields is well formed and within the
  408. range of time expressible by LARGE_INTEGER and FALSE otherwise.
  409. --*/
  410. {
  411. ULONG Year;
  412. ULONG Month;
  413. ULONG Day;
  414. ULONG Hour;
  415. ULONG Minute;
  416. ULONG Second;
  417. ULONG Milliseconds;
  418. ULONG ElapsedDays;
  419. ULONG ElapsedMilliseconds;
  420. //
  421. // Load the time field elements into local variables. This should
  422. // ensure that the compiler will only load the input elements
  423. // once, even if there are alias problems. It will also make
  424. // everything (except the year) zero based. We cannot zero base the
  425. // year because then we can't recognize cases where we're given a year
  426. // before 1601.
  427. //
  428. Year = TimeFields->Year;
  429. Month = TimeFields->Month - 1;
  430. Day = TimeFields->Day - 1;
  431. Hour = TimeFields->Hour;
  432. Minute = TimeFields->Minute;
  433. Second = TimeFields->Second;
  434. Milliseconds = TimeFields->Milliseconds;
  435. //
  436. // Check that the time field input variable contains
  437. // proper values.
  438. //
  439. if ((TimeFields->Month < 1) ||
  440. (TimeFields->Day < 1) ||
  441. (Year < 1601) ||
  442. (Month > 11) ||
  443. ((CSHORT)Day >= MaxDaysInMonth(Year, Month)) ||
  444. (Hour > 23) ||
  445. (Minute > 59) ||
  446. (Second > 59) ||
  447. (Milliseconds > 999)) {
  448. return FALSE;
  449. }
  450. //
  451. // Compute the total number of elapsed days represented by the
  452. // input time field variable
  453. //
  454. ElapsedDays = ElapsedYearsToDays( Year - 1601 );
  455. if (IsLeapYear( Year - 1600 )) {
  456. ElapsedDays += LeapYearDaysPrecedingMonth[ Month ];
  457. } else {
  458. ElapsedDays += NormalYearDaysPrecedingMonth[ Month ];
  459. }
  460. ElapsedDays += Day;
  461. //
  462. // Now compute the total number of milliseconds in the fractional
  463. // part of the day
  464. //
  465. ElapsedMilliseconds = (((Hour*60) + Minute)*60 + Second)*1000 + Milliseconds;
  466. //
  467. // Given the elapsed days and milliseconds we can now build
  468. // the output time variable
  469. //
  470. DaysAndFractionToTime( ElapsedDays, ElapsedMilliseconds, Time );
  471. //
  472. // And return to our caller
  473. //
  474. return TRUE;
  475. }
  476. static
  477. VOID
  478. TimeToDaysAndFraction (
  479. IN PLARGE_INTEGER Time,
  480. OUT PULONG ElapsedDays,
  481. OUT PULONG Milliseconds
  482. )
  483. /*++
  484. Routine Description:
  485. This routine converts an input 64-bit time value to the number
  486. of total elapsed days and the number of milliseconds in the
  487. partial day.
  488. Arguments:
  489. Time - Supplies the input time to convert from
  490. ElapsedDays - Receives the number of elapsed days
  491. Milliseconds - Receives the number of milliseconds in the partial day
  492. Return Value:
  493. None
  494. --*/
  495. {
  496. LARGE_INTEGER TotalMilliseconds;
  497. LARGE_INTEGER Temp;
  498. //
  499. // Convert the input time to total milliseconds
  500. //
  501. TotalMilliseconds = Convert100nsToMilliseconds( *(PLARGE_INTEGER)Time );
  502. //
  503. // Convert milliseconds to total days
  504. //
  505. Temp = ConvertMillisecondsToDays( TotalMilliseconds );
  506. //
  507. // Set the elapsed days from temp, we've divided it enough so that
  508. // the high part must be zero.
  509. //
  510. *ElapsedDays = Temp.LowPart;
  511. //
  512. // Calculate the exact number of milliseconds in the elapsed days
  513. // and subtract that from the total milliseconds to figure out
  514. // the number of milliseconds left in the partial day
  515. //
  516. Temp.QuadPart = ConvertDaysToMilliseconds( *ElapsedDays );
  517. Temp.QuadPart = TotalMilliseconds.QuadPart - Temp.QuadPart;
  518. //
  519. // Set the fraction part from temp, the total number of milliseconds in
  520. // a day guarantees that the high part must be zero.
  521. //
  522. *Milliseconds = Temp.LowPart;
  523. //
  524. // And return to our caller
  525. //
  526. return;
  527. }
  528. VOID
  529. RtlTimeToTimeFields (
  530. IN PLARGE_INTEGER Time,
  531. OUT PTIME_FIELDS TimeFields
  532. )
  533. /*++
  534. Routine Description:
  535. This routine converts an input 64-bit LARGE_INTEGER variable to its corresponding
  536. time field record. It will tell the caller the year, month, day, hour,
  537. minute, second, millisecond, and weekday corresponding to the input time
  538. variable.
  539. Arguments:
  540. Time - Supplies the time value to interpret
  541. TimeFields - Receives a value corresponding to Time
  542. Return Value:
  543. None
  544. --*/
  545. {
  546. ULONG Years;
  547. ULONG Month;
  548. ULONG Days;
  549. ULONG Hours;
  550. ULONG Minutes;
  551. ULONG Seconds;
  552. ULONG Milliseconds;
  553. //
  554. // First divide the input time 64 bit time variable into
  555. // the number of whole days and part days (in milliseconds)
  556. //
  557. TimeToDaysAndFraction( Time, &Days, &Milliseconds );
  558. //
  559. // Compute which weekday it is and save it away now in the output
  560. // variable. We add the weekday of the base day to bias our computation
  561. // which means that if one day has elapsed then we the weekday we want
  562. // is the Jan 2nd, 1601.
  563. //
  564. TimeFields->Weekday = (CSHORT)((Days + WEEKDAY_OF_1601) % 7);
  565. //
  566. // Calculate the number of whole years contained in the elapsed days
  567. // For example if Days = 500 then Years = 1
  568. //
  569. Years = ElapsedDaysToYears( Days );
  570. //
  571. // And subtract the number of whole years from our elapsed days
  572. // For example if Days = 500, Years = 1, and the new days is equal
  573. // to 500 - 365 (normal year).
  574. //
  575. Days = Days - ElapsedYearsToDays( Years );
  576. //
  577. // Now test whether the year we are working on (i.e., The year
  578. // after the total number of elapsed years) is a leap year
  579. // or not.
  580. //
  581. if (IsLeapYear( Years + 1 )) {
  582. //
  583. // The current year is a leap year, so figure out what month
  584. // it is, and then subtract the number of days preceding the
  585. // month from the days to figure out what day of the month it is
  586. //
  587. Month = LeapYearDayToMonth[Days];
  588. Days = Days - LeapYearDaysPrecedingMonth[Month];
  589. } else {
  590. //
  591. // The current year is a normal year, so figure out the month
  592. // and days as described above for the leap year case
  593. //
  594. Month = NormalYearDayToMonth[Days];
  595. Days = Days - NormalYearDaysPrecedingMonth[Month];
  596. }
  597. //
  598. // Now we need to compute the elapsed hour, minute, second, milliseconds
  599. // from the millisecond variable. This variable currently contains
  600. // the number of milliseconds in our input time variable that did not
  601. // fit into a whole day. To compute the hour, minute, second part
  602. // we will actually do the arithmetic backwards computing milliseconds
  603. // seconds, minutes, and then hours. We start by computing the
  604. // number of whole seconds left in the day, and then computing
  605. // the millisecond remainder.
  606. //
  607. Seconds = Milliseconds / 1000;
  608. Milliseconds = Milliseconds % 1000;
  609. //
  610. // Now we compute the number of whole minutes left in the day
  611. // and the number of remainder seconds
  612. //
  613. Minutes = Seconds / 60;
  614. Seconds = Seconds % 60;
  615. //
  616. // Now compute the number of whole hours left in the day
  617. // and the number of remainder minutes
  618. //
  619. Hours = Minutes / 60;
  620. Minutes = Minutes % 60;
  621. //
  622. // As our final step we put everything into the time fields
  623. // output variable
  624. //
  625. TimeFields->Year = (CSHORT)(Years + 1601);
  626. TimeFields->Month = (CSHORT)(Month + 1);
  627. TimeFields->Day = (CSHORT)(Days + 1);
  628. TimeFields->Hour = (CSHORT)Hours;
  629. TimeFields->Minute = (CSHORT)Minutes;
  630. TimeFields->Second = (CSHORT)Seconds;
  631. TimeFields->Milliseconds = (CSHORT)Milliseconds;
  632. //
  633. // and return to our caller
  634. //
  635. return;
  636. }
  637. NTSTATUS
  638. RtlSystemTimeToLocalTime (
  639. IN PLARGE_INTEGER SystemTime,
  640. OUT PLARGE_INTEGER LocalTime
  641. )
  642. {
  643. // BUGBUG this is stubbed out for the time being.
  644. LocalTime->QuadPart = SystemTime->QuadPart;
  645. return STATUS_SUCCESS;
  646. }
  647. NTSTATUS
  648. EfiQuerySystemTime(
  649. OUT PLARGE_INTEGER SystemTime
  650. )
  651. {
  652. EFI_TIME time;
  653. TIME_FIELDS nttime;
  654. if(SystemTime == NULL) {
  655. return STATUS_INVALID_PARAMETER;
  656. }
  657. if(RT->GetTime(&time,NULL) != EFI_SUCCESS) {
  658. return STATUS_UNSUCCESSFUL;
  659. }
  660. nttime.Year = time.Year;
  661. nttime.Month = time.Month;
  662. nttime.Day = time.Day;
  663. nttime.Hour = time.Hour;
  664. nttime.Minute = time.Minute;
  665. nttime.Second = time.Second;
  666. nttime.Milliseconds = (CSHORT)time.Nanosecond / (1000*1000);
  667. nttime.Weekday = 0;
  668. RtlTimeFieldsToTime(&nttime,SystemTime );
  669. return STATUS_SUCCESS;
  670. }