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.

1962 lines
59 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. datetime.c
  5. Abstract:
  6. This module implements Win32 time of day functions
  7. Author:
  8. Mark Lucovsky (markl) 08-Oct-1990
  9. Revision History:
  10. --*/
  11. #include "basedll.h"
  12. #define IsActiveConsoleSession() (USER_SHARED_DATA->ActiveConsoleId == NtCurrentPeb()->SessionId)
  13. ULONG
  14. CalcClientTimeZoneIdAndBias(
  15. IN CONST TIME_ZONE_INFORMATION *ptzi,
  16. OUT KSYSTEM_TIME *pBias);
  17. VOID
  18. WINAPI
  19. GetLocalTime(
  20. LPSYSTEMTIME lpLocalTime
  21. )
  22. /*++
  23. Routine Description:
  24. The current local system date and time can be returned using
  25. GetLocalTime.
  26. Arguments:
  27. lpLocalTime - Returns the current system date and time:
  28. SYSTEMTIME Structure:
  29. WORD wYear - Returns the current year.
  30. WORD wMonth - Returns the current month with January equal to 1.
  31. WORD wDayOfWeek - Returns the current day of the week where
  32. 0=Sunday, 1=Monday...
  33. WORD wDay - Returns the current day of the month.
  34. WORD wHour - Returns the current hour.
  35. WORD wMinute - Returns the current minute within the hour.
  36. WORD wSecond - Returns the current second within the minute.
  37. WORD wMilliseconds - Returns the current millisecond within the
  38. second.
  39. Return Value:
  40. None.
  41. --*/
  42. {
  43. LARGE_INTEGER LocalTime;
  44. LARGE_INTEGER SystemTime;
  45. LARGE_INTEGER Bias;
  46. TIME_FIELDS TimeFields;
  47. volatile KSYSTEM_TIME *pRealBias;
  48. if(!IsActiveConsoleSession() &&
  49. BaseStaticServerData->TermsrvClientTimeZoneId!=TIME_ZONE_ID_INVALID) {
  50. pRealBias=&(BaseStaticServerData->ktTermsrvClientBias);
  51. } else {
  52. pRealBias=&(USER_SHARED_DATA->TimeZoneBias);
  53. }
  54. //
  55. // Read system time from shared region.
  56. //
  57. do {
  58. SystemTime.HighPart = USER_SHARED_DATA->SystemTime.High1Time;
  59. SystemTime.LowPart = USER_SHARED_DATA->SystemTime.LowPart;
  60. } while (SystemTime.HighPart != USER_SHARED_DATA->SystemTime.High2Time);
  61. //
  62. // Read time zone bias from shared region.
  63. // If it's terminal server session use client bias.
  64. do {
  65. Bias.HighPart = pRealBias->High1Time;
  66. Bias.LowPart = pRealBias->LowPart;
  67. } while (Bias.HighPart != pRealBias->High2Time);
  68. LocalTime.QuadPart = SystemTime.QuadPart - Bias.QuadPart;
  69. RtlTimeToTimeFields(&LocalTime,&TimeFields);
  70. lpLocalTime->wYear = TimeFields.Year ;
  71. lpLocalTime->wMonth = TimeFields.Month ;
  72. lpLocalTime->wDayOfWeek = TimeFields.Weekday ;
  73. lpLocalTime->wDay = TimeFields.Day ;
  74. lpLocalTime->wHour = TimeFields.Hour ;
  75. lpLocalTime->wMinute = TimeFields.Minute ;
  76. lpLocalTime->wSecond = TimeFields.Second ;
  77. lpLocalTime->wMilliseconds = TimeFields.Milliseconds;
  78. }
  79. VOID
  80. WINAPI
  81. GetSystemTime(
  82. LPSYSTEMTIME lpSystemTime
  83. )
  84. /*++
  85. Routine Description:
  86. The current system date and time (UTC based) can be returned using
  87. GetSystemTime.
  88. Arguments:
  89. lpSystemTime - Returns the current system date and time:
  90. SYSTEMTIME Structure:
  91. WORD wYear - Returns the current year.
  92. WORD wMonth - Returns the current month with January equal to 1.
  93. WORD wDayOfWeek - Returns the current day of the week where
  94. 0=Sunday, 1=Monday...
  95. WORD wDay - Returns the current day of the month.
  96. WORD wHour - Returns the current hour.
  97. WORD wMinute - Returns the current minute within the hour.
  98. WORD wSecond - Returns the current second within the minute.
  99. WORD wMilliseconds - Returns the current millisecond within the
  100. second.
  101. Return Value:
  102. None.
  103. --*/
  104. {
  105. LARGE_INTEGER SystemTime;
  106. TIME_FIELDS TimeFields;
  107. //
  108. // Read system time from shared region.
  109. //
  110. do {
  111. SystemTime.HighPart = USER_SHARED_DATA->SystemTime.High1Time;
  112. SystemTime.LowPart = USER_SHARED_DATA->SystemTime.LowPart;
  113. } while (SystemTime.HighPart != USER_SHARED_DATA->SystemTime.High2Time);
  114. RtlTimeToTimeFields(&SystemTime,&TimeFields);
  115. lpSystemTime->wYear = TimeFields.Year ;
  116. lpSystemTime->wMonth = TimeFields.Month ;
  117. lpSystemTime->wDayOfWeek = TimeFields.Weekday ;
  118. lpSystemTime->wDay = TimeFields.Day ;
  119. lpSystemTime->wHour = TimeFields.Hour ;
  120. lpSystemTime->wMinute = TimeFields.Minute ;
  121. lpSystemTime->wSecond = TimeFields.Second ;
  122. lpSystemTime->wMilliseconds = TimeFields.Milliseconds;
  123. }
  124. VOID
  125. WINAPI
  126. GetSystemTimeAsFileTime(
  127. LPFILETIME lpSystemTimeAsFileTime
  128. )
  129. /*++
  130. Routine Description:
  131. The current system date and time (UTC based) can be returned using
  132. GetSystemTimeAsFileTime.
  133. Arguments:
  134. lpSystemTimeAsFileTime - Returns the current system date and time formatted as
  135. a FILETIME structure
  136. Return Value:
  137. None.
  138. --*/
  139. {
  140. LARGE_INTEGER SystemTime;
  141. //
  142. // Read system time from shared region.
  143. //
  144. do {
  145. SystemTime.HighPart = USER_SHARED_DATA->SystemTime.High1Time;
  146. SystemTime.LowPart = USER_SHARED_DATA->SystemTime.LowPart;
  147. } while (SystemTime.HighPart != USER_SHARED_DATA->SystemTime.High2Time);
  148. lpSystemTimeAsFileTime->dwLowDateTime = SystemTime.LowPart;
  149. lpSystemTimeAsFileTime->dwHighDateTime = SystemTime.HighPart;
  150. }
  151. BOOL
  152. WINAPI
  153. SetSystemTime(
  154. CONST SYSTEMTIME *lpSystemTime
  155. )
  156. /*++
  157. Routine Description:
  158. The current UTC based system date and time can be set using
  159. SetSystemTime.
  160. Arguments:
  161. lpSystemTime - Supplies the date and time to set. The wDayOfWeek field
  162. is ignored.
  163. Return Value:
  164. TRUE - The current system date and time was set.
  165. FALSE/NULL - The operation failed. Extended error status is available
  166. using GetLastError.
  167. --*/
  168. {
  169. LARGE_INTEGER SystemTime;
  170. TIME_FIELDS TimeFields;
  171. BOOLEAN ReturnValue;
  172. PVOID State;
  173. NTSTATUS Status;
  174. ReturnValue = TRUE;
  175. TimeFields.Year = lpSystemTime->wYear ;
  176. TimeFields.Month = lpSystemTime->wMonth ;
  177. TimeFields.Day = lpSystemTime->wDay ;
  178. TimeFields.Hour = lpSystemTime->wHour ;
  179. TimeFields.Minute = lpSystemTime->wMinute ;
  180. TimeFields.Second = lpSystemTime->wSecond ;
  181. TimeFields.Milliseconds = lpSystemTime->wMilliseconds;
  182. if ( !RtlTimeFieldsToTime(&TimeFields,&SystemTime) ) {
  183. Status = STATUS_INVALID_PARAMETER;
  184. ReturnValue = FALSE;
  185. }
  186. else {
  187. Status = BasepAcquirePrivilegeEx( SE_SYSTEMTIME_PRIVILEGE, &State );
  188. if ( NT_SUCCESS(Status) ) {
  189. Status = NtSetSystemTime(&SystemTime,NULL);
  190. BasepReleasePrivilege( State );
  191. }
  192. if ( !NT_SUCCESS(Status) ) {
  193. ReturnValue = FALSE;
  194. }
  195. }
  196. if ( !ReturnValue ) {
  197. BaseSetLastNTError(Status);
  198. }
  199. return ReturnValue;
  200. }
  201. BOOL
  202. WINAPI
  203. SetLocalTime(
  204. CONST SYSTEMTIME *lpLocalTime
  205. )
  206. /*++
  207. Routine Description:
  208. The current local system date and time can be set using
  209. SetLocalTime.
  210. Arguments:
  211. lpSystemTime - Supplies the date and time to set. The wDayOfWeek field
  212. is ignored.
  213. Return Value:
  214. TRUE - The current system date and time was set.
  215. FALSE/NULL - The operation failed. Extended error status is available
  216. using GetLastError.
  217. --*/
  218. {
  219. LARGE_INTEGER SystemTime;
  220. LARGE_INTEGER LocalTime;
  221. TIME_FIELDS TimeFields;
  222. BOOLEAN ReturnValue;
  223. PVOID State;
  224. NTSTATUS Status;
  225. LARGE_INTEGER Bias;
  226. volatile KSYSTEM_TIME *pRealBias;
  227. //
  228. // Read time zone bias from shared region.
  229. // If it's terminal server session use client bias.
  230. if(!IsActiveConsoleSession() &&
  231. BaseStaticServerData->TermsrvClientTimeZoneId!=TIME_ZONE_ID_INVALID) {
  232. pRealBias=&(BaseStaticServerData->ktTermsrvClientBias);
  233. } else {
  234. pRealBias=&(USER_SHARED_DATA->TimeZoneBias);
  235. }
  236. do {
  237. Bias.HighPart = pRealBias->High1Time;
  238. Bias.LowPart = pRealBias->LowPart;
  239. } while (Bias.HighPart != pRealBias->High2Time);
  240. ReturnValue = TRUE;
  241. TimeFields.Year = lpLocalTime->wYear ;
  242. TimeFields.Month = lpLocalTime->wMonth ;
  243. TimeFields.Day = lpLocalTime->wDay ;
  244. TimeFields.Hour = lpLocalTime->wHour ;
  245. TimeFields.Minute = lpLocalTime->wMinute ;
  246. TimeFields.Second = lpLocalTime->wSecond ;
  247. TimeFields.Milliseconds = lpLocalTime->wMilliseconds;
  248. if ( !RtlTimeFieldsToTime(&TimeFields,&LocalTime) ) {
  249. Status = STATUS_INVALID_PARAMETER;
  250. ReturnValue = FALSE;
  251. }
  252. else {
  253. SystemTime.QuadPart = LocalTime.QuadPart + Bias.QuadPart;
  254. Status = BasepAcquirePrivilegeEx( SE_SYSTEMTIME_PRIVILEGE, &State );
  255. if ( NT_SUCCESS(Status) ) {
  256. Status = NtSetSystemTime(&SystemTime,NULL);
  257. BasepReleasePrivilege( State );
  258. if ( !NT_SUCCESS(Status) ) {
  259. ReturnValue = FALSE;
  260. }
  261. }
  262. else {
  263. ReturnValue = FALSE;
  264. }
  265. }
  266. if ( !ReturnValue ) {
  267. BaseSetLastNTError(Status);
  268. }
  269. return ReturnValue;
  270. }
  271. DWORD
  272. GetTickCount(
  273. VOID
  274. )
  275. /*++
  276. Routine Description:
  277. Win32 systems implement a free-running millisecond counter. The
  278. value of this counter can be read using GetTickCount.
  279. Arguments:
  280. None.
  281. Return Value:
  282. This function returns the number of milliseconds that have elapsed
  283. since the system was started. If the system has been running for
  284. a long time, it is possible that the count will repeat. The value of
  285. the counter is accurate within 55 milliseconds.
  286. --*/
  287. {
  288. return (DWORD)NtGetTickCount();
  289. }
  290. BOOL
  291. APIENTRY
  292. FileTimeToSystemTime(
  293. CONST FILETIME *lpFileTime,
  294. LPSYSTEMTIME lpSystemTime
  295. )
  296. /*++
  297. Routine Description:
  298. This functions converts a 64-bit file time value to a time in system
  299. time format.
  300. Arguments:
  301. lpFileTime - Supplies the 64-bit file time to convert to the system
  302. date and time format.
  303. lpSystemTime - Returns the converted value of the 64-bit file time.
  304. Return Value:
  305. TRUE - The 64-bit file time was successfully converted.
  306. FALSE - The operation failed. Extended error status is available
  307. using GetLastError.
  308. --*/
  309. {
  310. LARGE_INTEGER FileTime;
  311. TIME_FIELDS TimeFields;
  312. FileTime.LowPart = lpFileTime->dwLowDateTime;
  313. FileTime.HighPart = lpFileTime->dwHighDateTime;
  314. if ( FileTime.QuadPart < 0 ) {
  315. SetLastError(ERROR_INVALID_PARAMETER);
  316. return FALSE;
  317. }
  318. RtlTimeToTimeFields(&FileTime, &TimeFields);
  319. lpSystemTime->wYear = TimeFields.Year ;
  320. lpSystemTime->wMonth = TimeFields.Month ;
  321. lpSystemTime->wDay = TimeFields.Day ;
  322. lpSystemTime->wDayOfWeek = TimeFields.Weekday ;
  323. lpSystemTime->wHour = TimeFields.Hour ;
  324. lpSystemTime->wMinute = TimeFields.Minute ;
  325. lpSystemTime->wSecond = TimeFields.Second ;
  326. lpSystemTime->wMilliseconds = TimeFields.Milliseconds;
  327. return TRUE;
  328. }
  329. BOOL
  330. APIENTRY
  331. SystemTimeToFileTime(
  332. CONST SYSTEMTIME *lpSystemTime,
  333. LPFILETIME lpFileTime
  334. )
  335. /*++
  336. Routine Description:
  337. This functions converts a system time value into a 64-bit file time.
  338. Arguments:
  339. lpSystemTime - Supplies the time that is to be converted into
  340. the 64-bit file time format. The wDayOfWeek field is ignored.
  341. lpFileTime - Returns the 64-bit file time representation of
  342. lpSystemTime.
  343. Return Value:
  344. TRUE - The time was successfully converted.
  345. FALSE - The operation failed. Extended error status is available
  346. using GetLastError.
  347. --*/
  348. {
  349. TIME_FIELDS TimeFields;
  350. LARGE_INTEGER FileTime;
  351. TimeFields.Year = lpSystemTime->wYear ;
  352. TimeFields.Month = lpSystemTime->wMonth ;
  353. TimeFields.Day = lpSystemTime->wDay ;
  354. TimeFields.Hour = lpSystemTime->wHour ;
  355. TimeFields.Minute = lpSystemTime->wMinute ;
  356. TimeFields.Second = lpSystemTime->wSecond ;
  357. TimeFields.Milliseconds = lpSystemTime->wMilliseconds;
  358. if ( !RtlTimeFieldsToTime(&TimeFields,&FileTime)) {
  359. BaseSetLastNTError(STATUS_INVALID_PARAMETER);
  360. return FALSE;
  361. }
  362. else {
  363. lpFileTime->dwLowDateTime = FileTime.LowPart;
  364. lpFileTime->dwHighDateTime = FileTime.HighPart;
  365. return TRUE;
  366. }
  367. }
  368. BOOL
  369. WINAPI
  370. FileTimeToLocalFileTime(
  371. CONST FILETIME *lpFileTime,
  372. LPFILETIME lpLocalFileTime
  373. )
  374. /*++
  375. Routine Description:
  376. This functions converts a UTC based file time to a local file time.
  377. Arguments:
  378. lpFileTime - Supplies the UTC based file time that is to be
  379. converted into a local file time
  380. lpLocalFileTime - Returns the 64-bit local file time representation of
  381. lpFileTime.
  382. Return Value:
  383. TRUE - The time was successfully converted.
  384. FALSE - The operation failed. Extended error status is available
  385. using GetLastError.
  386. --*/
  387. {
  388. LARGE_INTEGER FileTime;
  389. LARGE_INTEGER LocalFileTime;
  390. LARGE_INTEGER Bias;
  391. volatile KSYSTEM_TIME *pRealBias;
  392. //
  393. // Read time zone bias from shared region.
  394. // If it's terminal server session use client bias.
  395. if(!IsActiveConsoleSession() &&
  396. BaseStaticServerData->TermsrvClientTimeZoneId!=TIME_ZONE_ID_INVALID) {
  397. pRealBias=&(BaseStaticServerData->ktTermsrvClientBias);
  398. } else {
  399. pRealBias=&(USER_SHARED_DATA->TimeZoneBias);
  400. }
  401. do {
  402. Bias.HighPart = pRealBias->High1Time;
  403. Bias.LowPart = pRealBias->LowPart;
  404. } while (Bias.HighPart != pRealBias->High2Time);
  405. FileTime.LowPart = lpFileTime->dwLowDateTime;
  406. FileTime.HighPart = lpFileTime->dwHighDateTime;
  407. LocalFileTime.QuadPart = FileTime.QuadPart - Bias.QuadPart;
  408. lpLocalFileTime->dwLowDateTime = LocalFileTime.LowPart;
  409. lpLocalFileTime->dwHighDateTime = LocalFileTime.HighPart;
  410. return TRUE;
  411. }
  412. BOOL
  413. WINAPI
  414. LocalFileTimeToFileTime(
  415. CONST FILETIME *lpLocalFileTime,
  416. LPFILETIME lpFileTime
  417. )
  418. /*++
  419. Routine Description:
  420. This functions converts a local file time to a UTC based file time.
  421. Arguments:
  422. lpLocalFileTime - Supplies the local file time that is to be
  423. converted into a UTC based file time
  424. lpFileTime - Returns the 64-bit UTC based file time representation of
  425. lpLocalFileTime.
  426. Return Value:
  427. TRUE - The time was successfully converted.
  428. FALSE - The operation failed. Extended error status is available
  429. using GetLastError.
  430. --*/
  431. {
  432. LARGE_INTEGER FileTime;
  433. LARGE_INTEGER LocalFileTime;
  434. LARGE_INTEGER Bias;
  435. volatile KSYSTEM_TIME *pRealBias;
  436. //
  437. // Read time zone bias from shared region.
  438. // If it's terminal server session use client bias.
  439. if(!IsActiveConsoleSession() &&
  440. BaseStaticServerData->TermsrvClientTimeZoneId!=TIME_ZONE_ID_INVALID) {
  441. pRealBias=&(BaseStaticServerData->ktTermsrvClientBias);
  442. } else {
  443. pRealBias=&(USER_SHARED_DATA->TimeZoneBias);
  444. }
  445. do {
  446. Bias.HighPart = pRealBias->High1Time;
  447. Bias.LowPart = pRealBias->LowPart;
  448. } while (Bias.HighPart != pRealBias->High2Time);
  449. LocalFileTime.LowPart = lpLocalFileTime->dwLowDateTime;
  450. LocalFileTime.HighPart = lpLocalFileTime->dwHighDateTime;
  451. FileTime.QuadPart = LocalFileTime.QuadPart + Bias.QuadPart;
  452. lpFileTime->dwLowDateTime = FileTime.LowPart;
  453. lpFileTime->dwHighDateTime = FileTime.HighPart;
  454. return TRUE;
  455. }
  456. #define AlmostTwoSeconds (2*1000*1000*10 - 1)
  457. BOOL
  458. APIENTRY
  459. FileTimeToDosDateTime(
  460. CONST FILETIME *lpFileTime,
  461. LPWORD lpFatDate,
  462. LPWORD lpFatTime
  463. )
  464. /*++
  465. Routine Description:
  466. This function converts a 64-bit file time into DOS date and time value
  467. which is represented as two 16-bit unsigned integers.
  468. Since the DOS date format can only represent dates between 1/1/80 and
  469. 12/31/2107, this conversion can fail if the input file time is outside
  470. of this range.
  471. Arguments:
  472. lpFileTime - Supplies the 64-bit file time to convert to DOS date and
  473. time format.
  474. lpFatDate - Returns the 16-bit DOS representation of date.
  475. lpFatTime - Returns the 16-bit DOS representation of time.
  476. Return Value:
  477. TRUE - The file time was successfully converted.
  478. FALSE - The operation failed. Extended error status is available
  479. using GetLastError.
  480. --*/
  481. {
  482. TIME_FIELDS TimeFields;
  483. LARGE_INTEGER FileTime;
  484. FileTime.LowPart = lpFileTime->dwLowDateTime;
  485. FileTime.HighPart = lpFileTime->dwHighDateTime;
  486. FileTime.QuadPart = FileTime.QuadPart + (LONGLONG)AlmostTwoSeconds;
  487. if ( FileTime.QuadPart < 0 ) {
  488. SetLastError(ERROR_INVALID_PARAMETER);
  489. return FALSE;
  490. }
  491. RtlTimeToTimeFields(&FileTime, &TimeFields);
  492. if (TimeFields.Year < 1980 || TimeFields.Year > 2107) {
  493. BaseSetLastNTError(STATUS_INVALID_PARAMETER);
  494. return FALSE;
  495. }
  496. *lpFatDate = (WORD)( ((USHORT)(TimeFields.Year-(CSHORT)1980) << 9) |
  497. ((USHORT)TimeFields.Month << 5) |
  498. (USHORT)TimeFields.Day
  499. );
  500. *lpFatTime = (WORD)( ((USHORT)TimeFields.Hour << 11) |
  501. ((USHORT)TimeFields.Minute << 5) |
  502. ((USHORT)TimeFields.Second >> 1)
  503. );
  504. return TRUE;
  505. }
  506. BOOL
  507. APIENTRY
  508. DosDateTimeToFileTime(
  509. WORD wFatDate,
  510. WORD wFatTime,
  511. LPFILETIME lpFileTime
  512. )
  513. /*++
  514. Routine Description:
  515. This function converts a DOS date and time value, which is
  516. represented as two 16-bit unsigned integers, into a 64-bit file
  517. time.
  518. Arguments:
  519. lpFatDate - Supplies the 16-bit DOS representation of date.
  520. lpFatTime - Supplies the 16-bit DOS representation of time.
  521. lpFileTime - Returns the 64-bit file time converted from the DOS
  522. date and time format.
  523. Return Value:
  524. TRUE - The Dos date and time were successfully converted.
  525. FALSE - The operation failed. Extended error status is available
  526. using GetLastError.
  527. --*/
  528. {
  529. TIME_FIELDS TimeFields;
  530. LARGE_INTEGER FileTime;
  531. TimeFields.Year = (CSHORT)((wFatDate & 0xFE00) >> 9)+(CSHORT)1980;
  532. TimeFields.Month = (CSHORT)((wFatDate & 0x01E0) >> 5);
  533. TimeFields.Day = (CSHORT)((wFatDate & 0x001F) >> 0);
  534. TimeFields.Hour = (CSHORT)((wFatTime & 0xF800) >> 11);
  535. TimeFields.Minute = (CSHORT)((wFatTime & 0x07E0) >> 5);
  536. TimeFields.Second = (CSHORT)((wFatTime & 0x001F) << 1);
  537. TimeFields.Milliseconds = 0;
  538. if (RtlTimeFieldsToTime(&TimeFields,&FileTime)) {
  539. lpFileTime->dwLowDateTime = FileTime.LowPart;
  540. lpFileTime->dwHighDateTime = FileTime.HighPart;
  541. return TRUE;
  542. }
  543. else {
  544. BaseSetLastNTError(STATUS_INVALID_PARAMETER);
  545. return FALSE;
  546. }
  547. }
  548. LONG
  549. APIENTRY
  550. CompareFileTime(
  551. CONST FILETIME *lpFileTime1,
  552. CONST FILETIME *lpFileTime2
  553. )
  554. /*++
  555. Routine Description:
  556. This function compares two 64-bit file times.
  557. Arguments:
  558. lpFileTime1 - pointer to a 64-bit file time.
  559. lpFileTime2 - pointer to a 64-bit file time.
  560. Return Value:
  561. -1 - *lpFileTime1 < *lpFileTime2
  562. 0 - *lpFileTime1 == *lpFileTime2
  563. +1 - *lpFileTime1 > *lpFileTime2
  564. --*/
  565. {
  566. ULARGE_INTEGER FileTime1;
  567. ULARGE_INTEGER FileTime2;
  568. FileTime1.LowPart = lpFileTime1->dwLowDateTime;
  569. FileTime1.HighPart = lpFileTime1->dwHighDateTime;
  570. FileTime2.LowPart = lpFileTime2->dwLowDateTime;
  571. FileTime2.HighPart = lpFileTime2->dwHighDateTime;
  572. if (FileTime1.QuadPart < FileTime2.QuadPart) {
  573. return( -1 );
  574. }
  575. else
  576. if (FileTime1.QuadPart > FileTime2.QuadPart) {
  577. return( 1 );
  578. }
  579. else {
  580. return( 0 );
  581. }
  582. }
  583. DWORD
  584. WINAPI
  585. GetTimeZoneInformation(
  586. LPTIME_ZONE_INFORMATION lpTimeZoneInformation
  587. )
  588. /*++
  589. Routine Description:
  590. This function allows an application to get the current timezone
  591. parameters These parameters control the Universal time to Local time
  592. translations.
  593. All UTC time to Local time translations are based on the following
  594. formula:
  595. UTC = LocalTime + Bias
  596. The return value of this function is the systems best guess of
  597. the current time zone parameters. This is one of:
  598. - Unknown
  599. - Standard Time
  600. - Daylight Savings Time
  601. If SetTimeZoneInformation was called without the transition date
  602. information, Unknown is returned, but the currect bias is used for
  603. local time translation. Otherwise, the system will correctly pick
  604. either daylight savings time or standard time.
  605. The information returned by this API is identical to the information
  606. stored in the last successful call to SetTimeZoneInformation. The
  607. exception is the Bias field returns the current Bias value in
  608. Arguments:
  609. lpTimeZoneInformation - Supplies the address of the time zone
  610. information structure.
  611. Return Value:
  612. TIME_ZONE_ID_UNKNOWN - The system can not determine the current
  613. timezone. This is usually due to a previous call to
  614. SetTimeZoneInformation where only the Bias was supplied and no
  615. transition dates were supplied.
  616. TIME_ZONE_ID_STANDARD - The system is operating in the range covered
  617. by StandardDate.
  618. TIME_ZONE_ID_DAYLIGHT - The system is operating in the range covered
  619. by DaylightDate.
  620. 0xffffffff - The operation failed. Extended error status is
  621. available using GetLastError.
  622. --*/
  623. {
  624. RTL_TIME_ZONE_INFORMATION tzi;
  625. NTSTATUS Status;
  626. //
  627. // get the timezone data from the system
  628. // If it's terminal server session use client time zone
  629. if(!IsActiveConsoleSession() &&
  630. BaseStaticServerData->TermsrvClientTimeZoneId!=TIME_ZONE_ID_INVALID) {
  631. *lpTimeZoneInformation = BaseStaticServerData->tziTermsrvClientTimeZone;
  632. return BaseStaticServerData->TermsrvClientTimeZoneId;
  633. } else {
  634. Status = NtQuerySystemInformation(
  635. SystemCurrentTimeZoneInformation,
  636. &tzi,
  637. sizeof(tzi),
  638. NULL
  639. );
  640. if ( !NT_SUCCESS(Status) ) {
  641. BaseSetLastNTError(Status);
  642. return 0xffffffff;
  643. }
  644. lpTimeZoneInformation->Bias = tzi.Bias;
  645. lpTimeZoneInformation->StandardBias = tzi.StandardBias;
  646. lpTimeZoneInformation->DaylightBias = tzi.DaylightBias;
  647. RtlMoveMemory(&lpTimeZoneInformation->StandardName,&tzi.StandardName,sizeof(tzi.StandardName));
  648. RtlMoveMemory(&lpTimeZoneInformation->DaylightName,&tzi.DaylightName,sizeof(tzi.DaylightName));
  649. lpTimeZoneInformation->StandardDate.wYear = tzi.StandardStart.Year ;
  650. lpTimeZoneInformation->StandardDate.wMonth = tzi.StandardStart.Month ;
  651. lpTimeZoneInformation->StandardDate.wDayOfWeek = tzi.StandardStart.Weekday ;
  652. lpTimeZoneInformation->StandardDate.wDay = tzi.StandardStart.Day ;
  653. lpTimeZoneInformation->StandardDate.wHour = tzi.StandardStart.Hour ;
  654. lpTimeZoneInformation->StandardDate.wMinute = tzi.StandardStart.Minute ;
  655. lpTimeZoneInformation->StandardDate.wSecond = tzi.StandardStart.Second ;
  656. lpTimeZoneInformation->StandardDate.wMilliseconds = tzi.StandardStart.Milliseconds;
  657. lpTimeZoneInformation->DaylightDate.wYear = tzi.DaylightStart.Year ;
  658. lpTimeZoneInformation->DaylightDate.wMonth = tzi.DaylightStart.Month ;
  659. lpTimeZoneInformation->DaylightDate.wDayOfWeek = tzi.DaylightStart.Weekday ;
  660. lpTimeZoneInformation->DaylightDate.wDay = tzi.DaylightStart.Day ;
  661. lpTimeZoneInformation->DaylightDate.wHour = tzi.DaylightStart.Hour ;
  662. lpTimeZoneInformation->DaylightDate.wMinute = tzi.DaylightStart.Minute ;
  663. lpTimeZoneInformation->DaylightDate.wSecond = tzi.DaylightStart.Second ;
  664. lpTimeZoneInformation->DaylightDate.wMilliseconds = tzi.DaylightStart.Milliseconds;
  665. return USER_SHARED_DATA->TimeZoneId;
  666. }
  667. }
  668. BOOL
  669. WINAPI
  670. SetTimeZoneInformation(
  671. CONST TIME_ZONE_INFORMATION *lpTimeZoneInformation
  672. )
  673. /*++
  674. Routine Description:
  675. This function allows an application to set timezone parameters into
  676. their system. These parameters control the Universal time to Local
  677. time translations.
  678. All UTC time to Local time translations are based on the following
  679. formula:
  680. UTC = LocalTime + Bias
  681. This API allows the caller to program the current time zone bias,
  682. and optionally set up the system to automatically sense daylight
  683. savings time and standard time transitions.
  684. The timezone bias information is controlled by the
  685. TIME_ZONE_INFORMATION structure.
  686. Bias - Supplies the current bias in minutes for local time
  687. translation on this machine where LocalTime + Bias = UTC. This
  688. is a required filed of this structure.
  689. StandardName - Supplies an optional abbreviation string associated
  690. with standard time on this system. This string is uniterpreted
  691. and is supplied and used only by callers of this API and of
  692. GetTimeZoneInformation.
  693. StandardDate - Supplies an optional date and time (UTC) that
  694. describes the transition into standard time. A value of 0 in
  695. the wMonth field tells the system that StandardDate is not
  696. specified. If this field is specified, then DaylightDate must
  697. also be specified. Additionally, local time translations done
  698. during the StandardTime range will be done relative to the
  699. supplied StandardBias value (added to Bias).
  700. This field supports two date formats. Absolute form specifies and
  701. exact date and time when standard time begins. In this form, the
  702. wYear, wMonth, wDay, wHour, wMinute, wSecond, and wMilliseconds
  703. of the SYSTEMTIME structure are used to specify an exact date.
  704. Day-in-month time is specified by setting wYear to 0, setting
  705. wDayOfWeek to an appropriate weekday, and using wDay in the
  706. range of 1-5 to select the correct day in the month. Using this
  707. notation, the first sunday in april may be specified as can be
  708. the last thursday in october (5 is equal to "the last").
  709. StandardBias - Supplies an optional bias value to be used during
  710. local time translations that occur during Standard Time. This
  711. field is ignored if StandardDate is not supplied.
  712. This bias value
  713. is added to the Bias field to form the Bias used during standard
  714. time. In most time zones, the value of this field is zero.
  715. DaylightName - Supplies an optional abbreviation string associated
  716. with daylight savings time on this system. This string is
  717. uniterpreted and is supplied and used only by callers of this
  718. API and of GetTimeZoneInformation.
  719. DaylightDate - Supplies an optional date and time (UTC) that
  720. describes the transition into daylight savings time. A value of
  721. 0 in the wMonth field tells the system that DaylightDate is not
  722. specified. If this field is specified, then StandardDate must
  723. also be specified. Additionally, local time translations done
  724. during the DaylightTime range will be done relative to the
  725. supplied DaylightBias value (added to Bias). The same dat formats
  726. supported by StandardDate are supported ib DaylightDate.
  727. DaylightBias - Supplies an optional bias value to be used during
  728. local time translations that occur during Daylight Savings Time.
  729. This field is ignored if DaylightDate is not supplied. This
  730. bias value is added to the Bias field to form the Bias used
  731. during daylight time. In most time zones, the value of this
  732. field is -60.
  733. Arguments:
  734. lpTimeZoneInformation - Supplies the address of the time zone
  735. information structure.
  736. Return Value:
  737. TRUE - The operation was successful.
  738. FALSE - The operation failed. Extended error status is available
  739. using GetLastError.
  740. --*/
  741. {
  742. RTL_TIME_ZONE_INFORMATION tzi;
  743. NTSTATUS Status;
  744. if(!IsActiveConsoleSession()) {
  745. return SetClientTimeZoneInformation(lpTimeZoneInformation);
  746. } else {
  747. tzi.Bias = lpTimeZoneInformation->Bias;
  748. tzi.StandardBias = lpTimeZoneInformation->StandardBias;
  749. tzi.DaylightBias = lpTimeZoneInformation->DaylightBias;
  750. RtlMoveMemory(&tzi.StandardName,&lpTimeZoneInformation->StandardName,sizeof(tzi.StandardName));
  751. RtlMoveMemory(&tzi.DaylightName,&lpTimeZoneInformation->DaylightName,sizeof(tzi.DaylightName));
  752. tzi.StandardStart.Year = lpTimeZoneInformation->StandardDate.wYear ;
  753. tzi.StandardStart.Month = lpTimeZoneInformation->StandardDate.wMonth ;
  754. tzi.StandardStart.Weekday = lpTimeZoneInformation->StandardDate.wDayOfWeek ;
  755. tzi.StandardStart.Day = lpTimeZoneInformation->StandardDate.wDay ;
  756. tzi.StandardStart.Hour = lpTimeZoneInformation->StandardDate.wHour ;
  757. tzi.StandardStart.Minute = lpTimeZoneInformation->StandardDate.wMinute ;
  758. tzi.StandardStart.Second = lpTimeZoneInformation->StandardDate.wSecond ;
  759. tzi.StandardStart.Milliseconds = lpTimeZoneInformation->StandardDate.wMilliseconds;
  760. tzi.DaylightStart.Year = lpTimeZoneInformation->DaylightDate.wYear ;
  761. tzi.DaylightStart.Month = lpTimeZoneInformation->DaylightDate.wMonth ;
  762. tzi.DaylightStart.Weekday = lpTimeZoneInformation->DaylightDate.wDayOfWeek ;
  763. tzi.DaylightStart.Day = lpTimeZoneInformation->DaylightDate.wDay ;
  764. tzi.DaylightStart.Hour = lpTimeZoneInformation->DaylightDate.wHour ;
  765. tzi.DaylightStart.Minute = lpTimeZoneInformation->DaylightDate.wMinute ;
  766. tzi.DaylightStart.Second = lpTimeZoneInformation->DaylightDate.wSecond ;
  767. tzi.DaylightStart.Milliseconds = lpTimeZoneInformation->DaylightDate.wMilliseconds;
  768. Status = RtlSetTimeZoneInformation( &tzi );
  769. if (!NT_SUCCESS( Status )) {
  770. BaseSetLastNTError(Status);
  771. return FALSE;
  772. }
  773. //
  774. // Refresh the system's concept of time
  775. //
  776. NtSetSystemTime(NULL,NULL);
  777. return TRUE;
  778. }
  779. }
  780. BOOL
  781. WINAPI
  782. GetSystemTimeAdjustment(
  783. PDWORD lpTimeAdjustment,
  784. PDWORD lpTimeIncrement,
  785. PBOOL lpTimeAdjustmentDisabled
  786. )
  787. /*++
  788. Routine Description:
  789. This function is used to support algorithms that want to synchronize
  790. the time of day (reported via GetSystemTime and GetLocalTime) with
  791. another time source using a programmed clock adjustment over a
  792. period of time.
  793. To facilitate this, the system computes the time of day by adding a
  794. value to a time of day counter at a periodic interval. This API
  795. allows the caller to obtain the periodic interval (clock interrupt
  796. rate), and the amount added to the time of day with each interrupt.
  797. A boolean value is also returned which indicates whether or not this
  798. time adjustment algorithm is even being used. A value of TRUE
  799. indicates that adjustment is not being used. If this is the case,
  800. the system may attempt to keep the time of day clock in sync using
  801. its own internal mechanisms. This may cause time of day to
  802. periodicly "jump" to the "correct time".
  803. Arguments:
  804. lpTimeAdjustment - Returns the number of 100ns units added to the
  805. time of day counter at each clock interrupt.
  806. lpTimeIncrement - Returns the clock interrupt rate in 100ns units.
  807. lpTimeAdjustmentDisabled - Returns an indicator which specifies
  808. whether or not time adjustment is inabled. A value of TRUE
  809. indicates that periodic adjustment is disabled
  810. (*lpTimeAdjustment == *lpTimeIncrement), AND that the system is
  811. free to serialize time of day using any mechanism it wants.
  812. This may cause periodic time jumps as the system serializes time
  813. of day to the "correct time". A value of false indicates that
  814. programmed time adjustment is being used to serialize the time
  815. of day, and that the system will not interfere with this scheme
  816. and will not attempt to synchronize time of day on its own.
  817. Return Value:
  818. TRUE - The operation was successful.
  819. FALSE - The operation failed. Use GetLastError to obtain detailed
  820. error information.
  821. --*/
  822. {
  823. NTSTATUS Status;
  824. SYSTEM_QUERY_TIME_ADJUST_INFORMATION TimeAdjust;
  825. BOOL b;
  826. Status = NtQuerySystemInformation(
  827. SystemTimeAdjustmentInformation,
  828. &TimeAdjust,
  829. sizeof(TimeAdjust),
  830. NULL
  831. );
  832. if ( !NT_SUCCESS(Status) ) {
  833. BaseSetLastNTError(Status);
  834. b = FALSE;
  835. }
  836. else {
  837. *lpTimeAdjustment = TimeAdjust.TimeAdjustment;
  838. *lpTimeIncrement = TimeAdjust.TimeIncrement;
  839. *lpTimeAdjustmentDisabled = TimeAdjust.Enable;
  840. b = TRUE;
  841. }
  842. return b;
  843. }
  844. BOOL
  845. WINAPI
  846. SetSystemTimeAdjustment(
  847. DWORD dwTimeAdjustment,
  848. BOOL bTimeAdjustmentDisabled
  849. )
  850. /*++
  851. Routine Description:
  852. This function is used to tell the system the parameters it should
  853. use to periodicaly synchronize time of day with some other source.
  854. This API supports two modes of operation.
  855. In the first mode, bTimeAdjustmentDisabled is set to FALSE. At each
  856. clock interrupt, the value of dwTimeAdjustment is added to the time
  857. of day. The clock interrupt rate may be obtained using
  858. GetSystemTimeAdjustment, and looking at the returned value of
  859. lpTimeIncrement.
  860. In the second mode, bTimeAdjustmentDisabled is set to TRUE. At each
  861. clock interrupt, the clock interrupt rate is added to the time of
  862. day. The system may also periodically refresh the time of day using
  863. other internal algorithms. These may produce "jumps" in time.
  864. The application must have system-time privilege (the
  865. SE_SYSTEMTIME_NAME privilege) for this function to succeed. This
  866. privilege is disabled by default. Use the AdjustTokenPrivileges
  867. function to enable the privilege and again to disable it after the
  868. time adjustment has been set.
  869. Arguments:
  870. dwTimeAdjustment - Supplies the value (in 100ns units) that is to be
  871. added to the time of day at each clock interrupt.
  872. bTimeAdjustmentDisabled - Supplies a flag which specifies the time
  873. adjustment mode that the system is to use. A value of TRUE
  874. indicates the the system should synchronize time of day using
  875. its own internal mechanisms. When this is the case, the value
  876. of dwTimeAdjustment is ignored. A value of FALSE indicates that
  877. the application is in control, and that the value specified by
  878. dwTimeAdjustment is to be added to the time of day at each clock
  879. interrupt.
  880. Return Value:
  881. TRUE - The operation was successful.
  882. FALSE - The operation failed. Use GetLastError to obtain detailed
  883. error information.
  884. --*/
  885. {
  886. NTSTATUS Status;
  887. SYSTEM_SET_TIME_ADJUST_INFORMATION TimeAdjust;
  888. BOOL b;
  889. b = TRUE;
  890. TimeAdjust.TimeAdjustment = dwTimeAdjustment;
  891. TimeAdjust.Enable = (BOOLEAN)bTimeAdjustmentDisabled;
  892. Status = NtSetSystemInformation(
  893. SystemTimeAdjustmentInformation,
  894. &TimeAdjust,
  895. sizeof(TimeAdjust)
  896. );
  897. if ( !NT_SUCCESS(Status) ) {
  898. BaseSetLastNTError(Status);
  899. b = FALSE;
  900. }
  901. return b;
  902. }
  903. BOOL
  904. WINAPI
  905. SystemTimeToTzSpecificLocalTime(
  906. LPTIME_ZONE_INFORMATION lpTimeZoneInformation,
  907. LPSYSTEMTIME lpUniversalTime,
  908. LPSYSTEMTIME lpLocalTime
  909. )
  910. {
  911. TIME_ZONE_INFORMATION TziData;
  912. LPTIME_ZONE_INFORMATION Tzi;
  913. RTL_TIME_ZONE_INFORMATION tzi;
  914. LARGE_INTEGER TimeZoneBias;
  915. LARGE_INTEGER NewTimeZoneBias;
  916. LARGE_INTEGER LocalCustomBias;
  917. LARGE_INTEGER StandardTime;
  918. LARGE_INTEGER DaylightTime;
  919. LARGE_INTEGER UtcStandardTime;
  920. LARGE_INTEGER UtcDaylightTime;
  921. LARGE_INTEGER CurrentUniversalTime;
  922. LARGE_INTEGER ComputedLocalTime;
  923. ULONG CurrentTimeZoneId = 0xffffffff;
  924. //
  925. // Get the timezone information into a useful format
  926. //
  927. if ( !ARGUMENT_PRESENT(lpTimeZoneInformation) ) {
  928. //
  929. // Convert universal time to local time using current timezone info
  930. //
  931. if (GetTimeZoneInformation(&TziData) == TIME_ZONE_ID_INVALID) {
  932. return FALSE;
  933. }
  934. Tzi = &TziData;
  935. }
  936. else {
  937. Tzi = lpTimeZoneInformation;
  938. }
  939. tzi.Bias = Tzi->Bias;
  940. tzi.StandardBias = Tzi->StandardBias;
  941. tzi.DaylightBias = Tzi->DaylightBias;
  942. RtlMoveMemory(&tzi.StandardName,&Tzi->StandardName,sizeof(tzi.StandardName));
  943. RtlMoveMemory(&tzi.DaylightName,&Tzi->DaylightName,sizeof(tzi.DaylightName));
  944. tzi.StandardStart.Year = Tzi->StandardDate.wYear ;
  945. tzi.StandardStart.Month = Tzi->StandardDate.wMonth ;
  946. tzi.StandardStart.Weekday = Tzi->StandardDate.wDayOfWeek ;
  947. tzi.StandardStart.Day = Tzi->StandardDate.wDay ;
  948. tzi.StandardStart.Hour = Tzi->StandardDate.wHour ;
  949. tzi.StandardStart.Minute = Tzi->StandardDate.wMinute ;
  950. tzi.StandardStart.Second = Tzi->StandardDate.wSecond ;
  951. tzi.StandardStart.Milliseconds = Tzi->StandardDate.wMilliseconds;
  952. tzi.DaylightStart.Year = Tzi->DaylightDate.wYear ;
  953. tzi.DaylightStart.Month = Tzi->DaylightDate.wMonth ;
  954. tzi.DaylightStart.Weekday = Tzi->DaylightDate.wDayOfWeek ;
  955. tzi.DaylightStart.Day = Tzi->DaylightDate.wDay ;
  956. tzi.DaylightStart.Hour = Tzi->DaylightDate.wHour ;
  957. tzi.DaylightStart.Minute = Tzi->DaylightDate.wMinute ;
  958. tzi.DaylightStart.Second = Tzi->DaylightDate.wSecond ;
  959. tzi.DaylightStart.Milliseconds = Tzi->DaylightDate.wMilliseconds;
  960. //
  961. // convert the input universal time to NT style time
  962. //
  963. if ( !SystemTimeToFileTime(lpUniversalTime,(LPFILETIME)&CurrentUniversalTime) ) {
  964. return FALSE;
  965. }
  966. //
  967. // Get the new timezone bias
  968. //
  969. NewTimeZoneBias.QuadPart = Int32x32To64(tzi.Bias*60, 10000000);
  970. //
  971. // Now see if we have stored cutover times
  972. //
  973. if ( tzi.StandardStart.Month && tzi.DaylightStart.Month ) {
  974. //
  975. // We have timezone cutover information. Compute the
  976. // cutover dates and compute what our current bias
  977. // is
  978. //
  979. if ( !RtlCutoverTimeToSystemTime(
  980. &tzi.StandardStart,
  981. &StandardTime,
  982. &CurrentUniversalTime,
  983. TRUE
  984. ) ) {
  985. BaseSetLastNTError(STATUS_INVALID_PARAMETER);
  986. return FALSE;
  987. }
  988. if ( !RtlCutoverTimeToSystemTime(
  989. &tzi.DaylightStart,
  990. &DaylightTime,
  991. &CurrentUniversalTime,
  992. TRUE
  993. ) ) {
  994. BaseSetLastNTError(STATUS_INVALID_PARAMETER);
  995. return FALSE;
  996. }
  997. //
  998. // Convert standard time and daylight time to utc
  999. //
  1000. LocalCustomBias.QuadPart = Int32x32To64(tzi.StandardBias*60, 10000000);
  1001. TimeZoneBias.QuadPart = NewTimeZoneBias.QuadPart + LocalCustomBias.QuadPart;
  1002. UtcDaylightTime.QuadPart = DaylightTime.QuadPart + TimeZoneBias.QuadPart;
  1003. LocalCustomBias.QuadPart = Int32x32To64(tzi.DaylightBias*60, 10000000);
  1004. TimeZoneBias.QuadPart = NewTimeZoneBias.QuadPart + LocalCustomBias.QuadPart;
  1005. UtcStandardTime.QuadPart = StandardTime.QuadPart + TimeZoneBias.QuadPart;
  1006. //
  1007. // If daylight < standard, then time >= daylight and
  1008. // less than standard is daylight
  1009. //
  1010. if ( UtcDaylightTime.QuadPart < UtcStandardTime.QuadPart ) {
  1011. //
  1012. // If today is >= DaylightTime and < StandardTime, then
  1013. // We are in daylight savings time
  1014. //
  1015. if ( (CurrentUniversalTime.QuadPart >= UtcDaylightTime.QuadPart) &&
  1016. (CurrentUniversalTime.QuadPart < UtcStandardTime.QuadPart) ) {
  1017. CurrentTimeZoneId = TIME_ZONE_ID_DAYLIGHT;
  1018. }
  1019. else {
  1020. CurrentTimeZoneId = TIME_ZONE_ID_STANDARD;
  1021. }
  1022. }
  1023. else {
  1024. //
  1025. // If today is >= StandardTime and < DaylightTime, then
  1026. // We are in standard time
  1027. //
  1028. if ( (CurrentUniversalTime.QuadPart >= UtcStandardTime.QuadPart ) &&
  1029. (CurrentUniversalTime.QuadPart < UtcDaylightTime.QuadPart ) ) {
  1030. CurrentTimeZoneId = TIME_ZONE_ID_STANDARD;
  1031. }
  1032. else {
  1033. CurrentTimeZoneId = TIME_ZONE_ID_DAYLIGHT;
  1034. }
  1035. }
  1036. //
  1037. // At this point, we know our current timezone and the
  1038. // Universal time of the next cutover.
  1039. //
  1040. LocalCustomBias.QuadPart = Int32x32To64(
  1041. CurrentTimeZoneId == TIME_ZONE_ID_DAYLIGHT ?
  1042. tzi.DaylightBias*60 :
  1043. tzi.StandardBias*60, // Bias in seconds
  1044. 10000000
  1045. );
  1046. TimeZoneBias.QuadPart = NewTimeZoneBias.QuadPart + LocalCustomBias.QuadPart;
  1047. }
  1048. else {
  1049. TimeZoneBias = NewTimeZoneBias;
  1050. }
  1051. ComputedLocalTime.QuadPart = CurrentUniversalTime.QuadPart - TimeZoneBias.QuadPart;
  1052. if ( !FileTimeToSystemTime((LPFILETIME)&ComputedLocalTime,lpLocalTime) ) {
  1053. return FALSE;
  1054. }
  1055. return TRUE;
  1056. }
  1057. BOOL
  1058. WINAPI
  1059. TzSpecificLocalTimeToSystemTime(
  1060. LPTIME_ZONE_INFORMATION lpTimeZoneInformation,
  1061. LPSYSTEMTIME lpLocalTime,
  1062. LPSYSTEMTIME lpUniversalTime
  1063. )
  1064. {
  1065. TIME_ZONE_INFORMATION TziData;
  1066. LPTIME_ZONE_INFORMATION Tzi;
  1067. RTL_TIME_ZONE_INFORMATION tzi;
  1068. LARGE_INTEGER TimeZoneBias;
  1069. LARGE_INTEGER NewTimeZoneBias;
  1070. LARGE_INTEGER LocalCustomBias;
  1071. LARGE_INTEGER StandardTime;
  1072. LARGE_INTEGER DaylightTime;
  1073. LARGE_INTEGER CurrentLocalTime;
  1074. LARGE_INTEGER ComputedUniversalTime;
  1075. ULONG CurrentTimeZoneId = 0xffffffff;
  1076. //
  1077. // Get the timezone information into a useful format
  1078. //
  1079. if ( !ARGUMENT_PRESENT(lpTimeZoneInformation) ) {
  1080. //
  1081. // Convert universal time to local time using current timezone info
  1082. //
  1083. if (GetTimeZoneInformation(&TziData) == TIME_ZONE_ID_INVALID) {
  1084. return FALSE;
  1085. }
  1086. Tzi = &TziData;
  1087. }
  1088. else {
  1089. Tzi = lpTimeZoneInformation;
  1090. }
  1091. tzi.Bias = Tzi->Bias;
  1092. tzi.StandardBias = Tzi->StandardBias;
  1093. tzi.DaylightBias = Tzi->DaylightBias;
  1094. RtlMoveMemory(&tzi.StandardName,&Tzi->StandardName,sizeof(tzi.StandardName));
  1095. RtlMoveMemory(&tzi.DaylightName,&Tzi->DaylightName,sizeof(tzi.DaylightName));
  1096. tzi.StandardStart.Year = Tzi->StandardDate.wYear ;
  1097. tzi.StandardStart.Month = Tzi->StandardDate.wMonth ;
  1098. tzi.StandardStart.Weekday = Tzi->StandardDate.wDayOfWeek ;
  1099. tzi.StandardStart.Day = Tzi->StandardDate.wDay ;
  1100. tzi.StandardStart.Hour = Tzi->StandardDate.wHour ;
  1101. tzi.StandardStart.Minute = Tzi->StandardDate.wMinute ;
  1102. tzi.StandardStart.Second = Tzi->StandardDate.wSecond ;
  1103. tzi.StandardStart.Milliseconds = Tzi->StandardDate.wMilliseconds;
  1104. tzi.DaylightStart.Year = Tzi->DaylightDate.wYear ;
  1105. tzi.DaylightStart.Month = Tzi->DaylightDate.wMonth ;
  1106. tzi.DaylightStart.Weekday = Tzi->DaylightDate.wDayOfWeek ;
  1107. tzi.DaylightStart.Day = Tzi->DaylightDate.wDay ;
  1108. tzi.DaylightStart.Hour = Tzi->DaylightDate.wHour ;
  1109. tzi.DaylightStart.Minute = Tzi->DaylightDate.wMinute ;
  1110. tzi.DaylightStart.Second = Tzi->DaylightDate.wSecond ;
  1111. tzi.DaylightStart.Milliseconds = Tzi->DaylightDate.wMilliseconds;
  1112. //
  1113. // convert the input local time to NT style time
  1114. //
  1115. if ( !SystemTimeToFileTime(lpLocalTime,(LPFILETIME)&CurrentLocalTime) ) {
  1116. return FALSE;
  1117. }
  1118. //
  1119. // Get the new timezone bias
  1120. //
  1121. NewTimeZoneBias.QuadPart = Int32x32To64(tzi.Bias*60, 10000000);
  1122. //
  1123. // Now see if we have stored cutover times
  1124. //
  1125. if ( tzi.StandardStart.Month && tzi.DaylightStart.Month ) {
  1126. //
  1127. // We have timezone cutover information. Compute the
  1128. // cutover dates and compute what our current bias
  1129. // is
  1130. //
  1131. if ( !RtlCutoverTimeToSystemTime(
  1132. &tzi.StandardStart,
  1133. &StandardTime,
  1134. &CurrentLocalTime,
  1135. TRUE
  1136. ) ) {
  1137. BaseSetLastNTError(STATUS_INVALID_PARAMETER);
  1138. return FALSE;
  1139. }
  1140. if ( !RtlCutoverTimeToSystemTime(
  1141. &tzi.DaylightStart,
  1142. &DaylightTime,
  1143. &CurrentLocalTime,
  1144. TRUE
  1145. ) ) {
  1146. BaseSetLastNTError(STATUS_INVALID_PARAMETER);
  1147. return FALSE;
  1148. }
  1149. //
  1150. // If daylight < standard, then time >= daylight and
  1151. // less than standard is daylight
  1152. //
  1153. if ( DaylightTime.QuadPart < StandardTime.QuadPart ) {
  1154. //
  1155. // If today is >= DaylightTime and < StandardTime, then
  1156. // We are in daylight savings time
  1157. //
  1158. if ( (CurrentLocalTime.QuadPart >= DaylightTime.QuadPart) &&
  1159. (CurrentLocalTime.QuadPart < StandardTime.QuadPart) ) {
  1160. CurrentTimeZoneId = TIME_ZONE_ID_DAYLIGHT;
  1161. }
  1162. else {
  1163. CurrentTimeZoneId = TIME_ZONE_ID_STANDARD;
  1164. }
  1165. }
  1166. else {
  1167. //
  1168. // If today is >= StandardTime and < DaylightTime, then
  1169. // We are in standard time
  1170. //
  1171. if ( (CurrentLocalTime.QuadPart >= StandardTime.QuadPart ) &&
  1172. (CurrentLocalTime.QuadPart < DaylightTime.QuadPart ) ) {
  1173. CurrentTimeZoneId = TIME_ZONE_ID_STANDARD;
  1174. }
  1175. else {
  1176. CurrentTimeZoneId = TIME_ZONE_ID_DAYLIGHT;
  1177. }
  1178. }
  1179. //
  1180. // At this point, we know our current timezone and the
  1181. // local time of the next cutover.
  1182. //
  1183. LocalCustomBias.QuadPart = Int32x32To64(
  1184. CurrentTimeZoneId == TIME_ZONE_ID_DAYLIGHT ?
  1185. tzi.DaylightBias*60 :
  1186. tzi.StandardBias*60, // Bias in seconds
  1187. 10000000
  1188. );
  1189. TimeZoneBias.QuadPart = NewTimeZoneBias.QuadPart + LocalCustomBias.QuadPart;
  1190. }
  1191. else {
  1192. TimeZoneBias = NewTimeZoneBias;
  1193. }
  1194. ComputedUniversalTime.QuadPart = CurrentLocalTime.QuadPart + TimeZoneBias.QuadPart;
  1195. if ( !FileTimeToSystemTime((LPFILETIME)&ComputedUniversalTime,lpUniversalTime) ) {
  1196. return FALSE;
  1197. }
  1198. return TRUE;
  1199. }
  1200. BOOL
  1201. WINAPI
  1202. SetClientTimeZoneInformation(
  1203. IN CONST TIME_ZONE_INFORMATION *ptzi
  1204. )
  1205. /*++
  1206. Routine Description:
  1207. Sets information in global structures used
  1208. to calculate local time in TS session.
  1209. Arguments:
  1210. IN CONST TIME_ZONE_INFORMATION *ptzi
  1211. Return Value:
  1212. TRUE - The operation was successful.
  1213. FALSE - The operation failed. Use GetLastError to obtain detailed
  1214. error information.
  1215. Client time zone information may become invalid during this call
  1216. In this case we will use time zone information from server
  1217. --*/
  1218. {
  1219. NTSTATUS Status;
  1220. BASE_API_MSG m;
  1221. PBASE_SET_TERMSRVCLIENTTIMEZONE c = &m.u.SetTermsrvClientTimeZone;
  1222. c->fFirstChunk=TRUE; //this meanes that this is only first portion of data
  1223. //we have to cut it ito two pieces because of
  1224. //message size restrictions (100 bytes)
  1225. c->Bias=ptzi->Bias;
  1226. RtlMoveMemory(&c->Name,&ptzi->StandardName,sizeof(ptzi->StandardName));
  1227. c->Date=ptzi->StandardDate;
  1228. c->Bias1=ptzi->StandardBias;
  1229. #if defined(BUILD_WOW6432)
  1230. Status = CsrBasepSetClientTimeZoneInformation(c);
  1231. #else
  1232. Status = CsrClientCallServer((PCSR_API_MSG)&m, NULL,
  1233. CSR_MAKE_API_NUMBER(BASESRV_SERVERDLL_INDEX,
  1234. BasepSetTermsrvClientTimeZone),
  1235. sizeof( *c ));
  1236. #endif
  1237. if ( !NT_SUCCESS( Status ) ) {
  1238. SetLastError( RtlNtStatusToDosError( Status ) );
  1239. return( FALSE );
  1240. }
  1241. c->fFirstChunk=FALSE; //this is a second and last portion of data
  1242. RtlMoveMemory(&c->Name,&ptzi->DaylightName,sizeof(ptzi->DaylightName));
  1243. c->Date=ptzi->DaylightDate;
  1244. c->Bias1=ptzi->DaylightBias;
  1245. c->TimeZoneId=CalcClientTimeZoneIdAndBias(ptzi,&c->RealBias);
  1246. #if defined(BUILD_WOW6432)
  1247. Status = CsrBasepSetClientTimeZoneInformation(c);
  1248. #else
  1249. Status = CsrClientCallServer((PCSR_API_MSG)&m, NULL,
  1250. CSR_MAKE_API_NUMBER(BASESRV_SERVERDLL_INDEX,
  1251. BasepSetTermsrvClientTimeZone),
  1252. sizeof( *c ));
  1253. #endif
  1254. if ( !NT_SUCCESS( Status ) ) {
  1255. SetLastError( RtlNtStatusToDosError( Status ) );
  1256. return( FALSE );
  1257. }
  1258. return( TRUE );
  1259. }
  1260. ULONG
  1261. CalcClientTimeZoneIdAndBias(
  1262. IN CONST TIME_ZONE_INFORMATION *ptzi,
  1263. OUT KSYSTEM_TIME *pBias)
  1264. /*++
  1265. Routine Description:
  1266. Calculates current bias and time zone ID.
  1267. Arguments:
  1268. IN CONST TIME_ZONE_INFORMATION *ptzi - time zone for which to calculate bias
  1269. OUT KSYSTEM_TIME *pBias - current bias
  1270. Return Value:
  1271. TIME_ZONE_ID_UNKNOWN - daylight saving time is not used in the
  1272. current time zone.
  1273. TIME_ZONE_ID_STANDARD - The system is operating in the range covered
  1274. by StandardDate.
  1275. TIME_ZONE_ID_DAYLIGHT - The system is operating in the range covered
  1276. by DaylightDate.
  1277. TIME_ZONE_ID_INVALID - The operation failed. Extended error status is
  1278. available using GetLastError.
  1279. --*/
  1280. {
  1281. LARGE_INTEGER TimeZoneBias;
  1282. LARGE_INTEGER NewTimeZoneBias;
  1283. LARGE_INTEGER LocalCustomBias;
  1284. LARGE_INTEGER StandardTime;
  1285. LARGE_INTEGER DaylightTime;
  1286. LARGE_INTEGER UtcStandardTime;
  1287. LARGE_INTEGER UtcDaylightTime;
  1288. SYSTEMTIME CurrentSystemTime;
  1289. LARGE_INTEGER CurrentUniversalTime;
  1290. ULONG CurrentTimeZoneId = 0xffffffff;
  1291. TIME_FIELDS StandardStart,DaylightStart;
  1292. NewTimeZoneBias.QuadPart = Int32x32To64(ptzi->Bias*60, 10000000);
  1293. //
  1294. // Now see if we have stored cutover times
  1295. //
  1296. if ( ptzi->StandardDate.wMonth && ptzi->DaylightDate.wMonth ) {
  1297. GetSystemTime(&CurrentSystemTime);
  1298. SystemTimeToFileTime(&CurrentSystemTime,(LPFILETIME)&CurrentUniversalTime);
  1299. StandardStart.Year = ptzi->StandardDate.wYear ;
  1300. StandardStart.Month = ptzi->StandardDate.wMonth ;
  1301. StandardStart.Weekday = ptzi->StandardDate.wDayOfWeek ;
  1302. StandardStart.Day = ptzi->StandardDate.wDay ;
  1303. StandardStart.Hour = ptzi->StandardDate.wHour ;
  1304. StandardStart.Minute = ptzi->StandardDate.wMinute ;
  1305. StandardStart.Second = ptzi->StandardDate.wSecond ;
  1306. StandardStart.Milliseconds = ptzi->StandardDate.wMilliseconds;
  1307. DaylightStart.Year = ptzi->DaylightDate.wYear ;
  1308. DaylightStart.Month = ptzi->DaylightDate.wMonth ;
  1309. DaylightStart.Weekday = ptzi->DaylightDate.wDayOfWeek ;
  1310. DaylightStart.Day = ptzi->DaylightDate.wDay ;
  1311. DaylightStart.Hour = ptzi->DaylightDate.wHour ;
  1312. DaylightStart.Minute = ptzi->DaylightDate.wMinute ;
  1313. DaylightStart.Second = ptzi->DaylightDate.wSecond ;
  1314. DaylightStart.Milliseconds = ptzi->DaylightDate.wMilliseconds;
  1315. //
  1316. // We have timezone cutover information. Compute the
  1317. // cutover dates and compute what our current bias
  1318. // is
  1319. //
  1320. if((!RtlCutoverTimeToSystemTime(&StandardStart,&StandardTime,
  1321. &CurrentUniversalTime,TRUE)) ||
  1322. (!RtlCutoverTimeToSystemTime(&DaylightStart,&DaylightTime,
  1323. &CurrentUniversalTime,TRUE))) {
  1324. BaseSetLastNTError(STATUS_INVALID_PARAMETER);
  1325. return TIME_ZONE_ID_INVALID;
  1326. }
  1327. //
  1328. // Convert standard time and daylight time to utc
  1329. //
  1330. LocalCustomBias.QuadPart = Int32x32To64(ptzi->StandardBias*60, 10000000);
  1331. TimeZoneBias.QuadPart = NewTimeZoneBias.QuadPart + LocalCustomBias.QuadPart;
  1332. UtcDaylightTime.QuadPart = DaylightTime.QuadPart + TimeZoneBias.QuadPart;
  1333. LocalCustomBias.QuadPart = Int32x32To64(ptzi->DaylightBias*60, 10000000);
  1334. TimeZoneBias.QuadPart = NewTimeZoneBias.QuadPart + LocalCustomBias.QuadPart;
  1335. UtcStandardTime.QuadPart = StandardTime.QuadPart + TimeZoneBias.QuadPart;
  1336. //
  1337. // If daylight < standard, then time >= daylight and
  1338. // less than standard is daylight
  1339. //
  1340. if ( UtcDaylightTime.QuadPart < UtcStandardTime.QuadPart ) {
  1341. //
  1342. // If today is >= DaylightTime and < StandardTime, then
  1343. // We are in daylight savings time
  1344. //
  1345. if ( (CurrentUniversalTime.QuadPart >= UtcDaylightTime.QuadPart) &&
  1346. (CurrentUniversalTime.QuadPart < UtcStandardTime.QuadPart) ) {
  1347. CurrentTimeZoneId = TIME_ZONE_ID_DAYLIGHT;
  1348. } else {
  1349. CurrentTimeZoneId = TIME_ZONE_ID_STANDARD;
  1350. }
  1351. } else {
  1352. //
  1353. // If today is >= StandardTime and < DaylightTime, then
  1354. // We are in standard time
  1355. //
  1356. if ( (CurrentUniversalTime.QuadPart >= UtcStandardTime.QuadPart ) &&
  1357. (CurrentUniversalTime.QuadPart < UtcDaylightTime.QuadPart ) ) {
  1358. CurrentTimeZoneId = TIME_ZONE_ID_STANDARD;
  1359. } else {
  1360. CurrentTimeZoneId = TIME_ZONE_ID_DAYLIGHT;
  1361. }
  1362. }
  1363. //
  1364. // At this point, we know our current timezone and the
  1365. // Universal time of the next cutover.
  1366. //
  1367. LocalCustomBias.QuadPart = Int32x32To64(
  1368. CurrentTimeZoneId == TIME_ZONE_ID_DAYLIGHT ?
  1369. ptzi->DaylightBias*60 :
  1370. ptzi->StandardBias*60, // Bias in seconds
  1371. 10000000
  1372. );
  1373. TimeZoneBias.QuadPart = NewTimeZoneBias.QuadPart + LocalCustomBias.QuadPart;
  1374. } else {
  1375. TimeZoneBias = NewTimeZoneBias;
  1376. CurrentTimeZoneId=TIME_ZONE_ID_UNKNOWN;
  1377. }
  1378. pBias->LowPart=(ULONG)(TimeZoneBias.LowPart);
  1379. pBias->High1Time=pBias->High2Time=(LONG)(TimeZoneBias.HighPart);
  1380. return CurrentTimeZoneId;
  1381. }
  1382. /*
  1383. //These 2 functions will be needed for new timedate.cpl
  1384. DWORD
  1385. WINAPI
  1386. GetServerTimeZoneInformation(
  1387. LPTIME_ZONE_INFORMATION lpTimeZoneInformation
  1388. )
  1389. {
  1390. RTL_TIME_ZONE_INFORMATION tzi;
  1391. NTSTATUS Status;
  1392. //
  1393. // get the timezone data from the system
  1394. //
  1395. Status = NtQuerySystemInformation(
  1396. SystemCurrentTimeZoneInformation,
  1397. &tzi,
  1398. sizeof(tzi),
  1399. NULL
  1400. );
  1401. if ( !NT_SUCCESS(Status) ) {
  1402. BaseSetLastNTError(Status);
  1403. return 0xffffffff;
  1404. }
  1405. lpTimeZoneInformation->Bias = tzi.Bias;
  1406. lpTimeZoneInformation->StandardBias = tzi.StandardBias;
  1407. lpTimeZoneInformation->DaylightBias = tzi.DaylightBias;
  1408. RtlMoveMemory(&lpTimeZoneInformation->StandardName,&tzi.StandardName,sizeof(tzi.StandardName));
  1409. RtlMoveMemory(&lpTimeZoneInformation->DaylightName,&tzi.DaylightName,sizeof(tzi.DaylightName));
  1410. lpTimeZoneInformation->StandardDate.wYear = tzi.StandardStart.Year ;
  1411. lpTimeZoneInformation->StandardDate.wMonth = tzi.StandardStart.Month ;
  1412. lpTimeZoneInformation->StandardDate.wDayOfWeek = tzi.StandardStart.Weekday ;
  1413. lpTimeZoneInformation->StandardDate.wDay = tzi.StandardStart.Day ;
  1414. lpTimeZoneInformation->StandardDate.wHour = tzi.StandardStart.Hour ;
  1415. lpTimeZoneInformation->StandardDate.wMinute = tzi.StandardStart.Minute ;
  1416. lpTimeZoneInformation->StandardDate.wSecond = tzi.StandardStart.Second ;
  1417. lpTimeZoneInformation->StandardDate.wMilliseconds = tzi.StandardStart.Milliseconds;
  1418. lpTimeZoneInformation->DaylightDate.wYear = tzi.DaylightStart.Year ;
  1419. lpTimeZoneInformation->DaylightDate.wMonth = tzi.DaylightStart.Month ;
  1420. lpTimeZoneInformation->DaylightDate.wDayOfWeek = tzi.DaylightStart.Weekday ;
  1421. lpTimeZoneInformation->DaylightDate.wDay = tzi.DaylightStart.Day ;
  1422. lpTimeZoneInformation->DaylightDate.wHour = tzi.DaylightStart.Hour ;
  1423. lpTimeZoneInformation->DaylightDate.wMinute = tzi.DaylightStart.Minute ;
  1424. lpTimeZoneInformation->DaylightDate.wSecond = tzi.DaylightStart.Second ;
  1425. lpTimeZoneInformation->DaylightDate.wMilliseconds = tzi.DaylightStart.Milliseconds;
  1426. return USER_SHARED_DATA->TimeZoneId;
  1427. }
  1428. BOOL
  1429. WINAPI
  1430. SetServerTimeZoneInformation(
  1431. CONST TIME_ZONE_INFORMATION *lpTimeZoneInformation
  1432. )
  1433. {
  1434. RTL_TIME_ZONE_INFORMATION tzi;
  1435. NTSTATUS Status;
  1436. tzi.Bias = lpTimeZoneInformation->Bias;
  1437. tzi.StandardBias = lpTimeZoneInformation->StandardBias;
  1438. tzi.DaylightBias = lpTimeZoneInformation->DaylightBias;
  1439. RtlMoveMemory(&tzi.StandardName,&lpTimeZoneInformation->StandardName,sizeof(tzi.StandardName));
  1440. RtlMoveMemory(&tzi.DaylightName,&lpTimeZoneInformation->DaylightName,sizeof(tzi.DaylightName));
  1441. tzi.StandardStart.Year = lpTimeZoneInformation->StandardDate.wYear ;
  1442. tzi.StandardStart.Month = lpTimeZoneInformation->StandardDate.wMonth ;
  1443. tzi.StandardStart.Weekday = lpTimeZoneInformation->StandardDate.wDayOfWeek ;
  1444. tzi.StandardStart.Day = lpTimeZoneInformation->StandardDate.wDay ;
  1445. tzi.StandardStart.Hour = lpTimeZoneInformation->StandardDate.wHour ;
  1446. tzi.StandardStart.Minute = lpTimeZoneInformation->StandardDate.wMinute ;
  1447. tzi.StandardStart.Second = lpTimeZoneInformation->StandardDate.wSecond ;
  1448. tzi.StandardStart.Milliseconds = lpTimeZoneInformation->StandardDate.wMilliseconds;
  1449. tzi.DaylightStart.Year = lpTimeZoneInformation->DaylightDate.wYear ;
  1450. tzi.DaylightStart.Month = lpTimeZoneInformation->DaylightDate.wMonth ;
  1451. tzi.DaylightStart.Weekday = lpTimeZoneInformation->DaylightDate.wDayOfWeek ;
  1452. tzi.DaylightStart.Day = lpTimeZoneInformation->DaylightDate.wDay ;
  1453. tzi.DaylightStart.Hour = lpTimeZoneInformation->DaylightDate.wHour ;
  1454. tzi.DaylightStart.Minute = lpTimeZoneInformation->DaylightDate.wMinute ;
  1455. tzi.DaylightStart.Second = lpTimeZoneInformation->DaylightDate.wSecond ;
  1456. tzi.DaylightStart.Milliseconds = lpTimeZoneInformation->DaylightDate.wMilliseconds;
  1457. Status = RtlSetTimeZoneInformation( &tzi );
  1458. if (!NT_SUCCESS( Status )) {
  1459. BaseSetLastNTError(Status);
  1460. return FALSE;
  1461. }
  1462. //
  1463. // Refresh the system's concept of time
  1464. //
  1465. NtSetSystemTime(NULL,NULL);
  1466. return TRUE;
  1467. }
  1468. */