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.

325 lines
11 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. ttime.c
  5. Abstract:
  6. Test program for the time conversion package
  7. Author:
  8. Gary Kimura [GaryKi] 27-Aug-1989
  9. Revision History:
  10. --*/
  11. #include <stdio.h>
  12. #include "nt.h"
  13. #include "ntrtl.h"
  14. VOID
  15. PrintTimeFields(
  16. IN PTIME_FIELDS TimeFields
  17. );
  18. LARGE_INTEGER Zero;
  19. LARGE_INTEGER OneSecond;
  20. LARGE_INTEGER OneMinute;
  21. LARGE_INTEGER OneHour;
  22. LARGE_INTEGER OneDay;
  23. LARGE_INTEGER OneWeek;
  24. LARGE_INTEGER OneNormalYear;
  25. LARGE_INTEGER OneLeapYear;
  26. LARGE_INTEGER OneCentury;
  27. LARGE_INTEGER TwoCenturies;
  28. LARGE_INTEGER ThreeCenturies;
  29. LARGE_INTEGER FourCenturies;
  30. LARGE_INTEGER Sum;
  31. TIME_FIELDS TimeFields;
  32. LARGE_INTEGER Time;
  33. LARGE_INTEGER StartOf1970;
  34. LARGE_INTEGER StartOf1980;
  35. int
  36. main(
  37. int argc,
  38. char *argv[]
  39. )
  40. {
  41. ULONG i;
  42. //
  43. // We're starting the test
  44. //
  45. DbgPrint("Start Time Test\n");
  46. //
  47. // Start by initializing some constants and making sure they
  48. // are correct
  49. //
  50. Zero.QuadPart = 0;
  51. OneSecond.QuadPart = 10000000;
  52. OneMinute.QuadPart = OneSecond.QuadPart * 60;
  53. OneHour.QuadPart = OneMinute.QuadPart * 60;
  54. OneDay.QuadPart = OneHour.QuadPart * 24;
  55. OneWeek.QuadPart = OneDay.QuadPart * 7;
  56. OneNormalYear.QuadPart = OneDay.QuadPart * 365;
  57. OneLeapYear.QuadPart = OneDay.QuadPart * 366;
  58. OneCentury.QuadPart = (OneNormalYear.QuadPart * 76) + (OneLeapYear.QuadPart * 24);
  59. TwoCenturies.QuadPart = OneCentury.QuadPart * 2;
  60. ThreeCenturies.QuadPart = OneCentury.QuadPart * 3;
  61. FourCenturies.QuadPart = (OneCentury.QuadPart * 4) + OneDay.QuadPart;
  62. Sum.QuadPart = Zero.QuadPart +
  63. OneSecond.QuadPart +
  64. OneMinute.QuadPart +
  65. OneHour.QuadPart +
  66. OneDay.QuadPart +
  67. OneWeek.QuadPart +
  68. OneNormalYear.QuadPart +
  69. ThreeCenturies.QuadPart;
  70. RtlTimeToTimeFields( (PLARGE_INTEGER)&Zero, &TimeFields );
  71. DbgPrint("StartOf1601 = "); PrintTimeFields( &TimeFields ); DbgPrint("\n");
  72. if (!RtlTimeFieldsToTime( &TimeFields, &Time )) {
  73. DbgPrint("****ERROR converting TimeFields back to Zero\n");
  74. }
  75. if ((Time.LowPart != Zero.LowPart) || (Time.HighPart != Zero.HighPart)) {
  76. DbgPrint("****ERROR Time != Zero\n");
  77. }
  78. RtlTimeToTimeFields( (PLARGE_INTEGER)&OneSecond, &TimeFields );
  79. DbgPrint(" + 1 Second = "); PrintTimeFields( &TimeFields ); DbgPrint("\n");
  80. if (!RtlTimeFieldsToTime( &TimeFields, &Time )) {
  81. DbgPrint("****ERROR converting TimeFields back to OneSecond\n");
  82. }
  83. if ((Time.LowPart != OneSecond.LowPart) || (Time.HighPart != OneSecond.HighPart)) {
  84. DbgPrint("****ERROR Time != OneSecond\n");
  85. }
  86. RtlTimeToTimeFields( (PLARGE_INTEGER)&OneMinute, &TimeFields );
  87. DbgPrint(" + 1 Minute = "); PrintTimeFields( &TimeFields ); DbgPrint("\n");
  88. if (!RtlTimeFieldsToTime( &TimeFields, &Time )) {
  89. DbgPrint("****ERROR converting TimeFields back to OneMinute\n");
  90. }
  91. if ((Time.LowPart != OneMinute.LowPart) || (Time.HighPart != OneMinute.HighPart)) {
  92. DbgPrint("****ERROR Time != OneMinute\n");
  93. }
  94. RtlTimeToTimeFields( (PLARGE_INTEGER)&OneHour, &TimeFields );
  95. DbgPrint(" + 1 Hour = "); PrintTimeFields( &TimeFields ); DbgPrint("\n");
  96. if (!RtlTimeFieldsToTime( &TimeFields, &Time )) {
  97. DbgPrint("****ERROR converting TimeFields back to OneHour\n");
  98. }
  99. if ((Time.LowPart != OneHour.LowPart) || (Time.HighPart != OneHour.HighPart)) {
  100. DbgPrint("****ERROR Time != OneHour\n");
  101. }
  102. RtlTimeToTimeFields( (PLARGE_INTEGER)&OneDay, &TimeFields );
  103. DbgPrint(" + 1 Day = "); PrintTimeFields( &TimeFields ); DbgPrint("\n");
  104. if (!RtlTimeFieldsToTime( &TimeFields, &Time )) {
  105. DbgPrint("****ERROR converting TimeFields back to OneDay\n");
  106. }
  107. if ((Time.LowPart != OneDay.LowPart) || (Time.HighPart != OneDay.HighPart)) {
  108. DbgPrint("****ERROR Time != OneDay\n");
  109. }
  110. RtlTimeToTimeFields( (PLARGE_INTEGER)&OneWeek, &TimeFields );
  111. DbgPrint(" + 1 Week = "); PrintTimeFields( &TimeFields ); DbgPrint("\n");
  112. if (!RtlTimeFieldsToTime( &TimeFields, &Time )) {
  113. DbgPrint("****ERROR converting TimeFields back to OneWeek\n");
  114. }
  115. if ((Time.LowPart != OneWeek.LowPart) || (Time.HighPart != OneWeek.HighPart)) {
  116. DbgPrint("****ERROR Time != OneWeek\n");
  117. }
  118. RtlTimeToTimeFields( (PLARGE_INTEGER)&OneNormalYear, &TimeFields );
  119. DbgPrint(" + 1 NormalYear = "); PrintTimeFields( &TimeFields ); DbgPrint("\n");
  120. if (!RtlTimeFieldsToTime( &TimeFields, &Time )) {
  121. DbgPrint("****ERROR converting TimeFields back to OneNormalYear\n");
  122. }
  123. if ((Time.LowPart != OneNormalYear.LowPart) || (Time.HighPart != OneNormalYear.HighPart)) {
  124. DbgPrint("****ERROR Time != OneNormalYear\n");
  125. }
  126. RtlTimeToTimeFields( (PLARGE_INTEGER)&OneLeapYear, &TimeFields );
  127. DbgPrint(" + 1 LeapYear = "); PrintTimeFields( &TimeFields ); DbgPrint("\n");
  128. if (!RtlTimeFieldsToTime( &TimeFields, &Time )) {
  129. DbgPrint("****ERROR converting TimeFields back to OneLeapYear\n");
  130. }
  131. if ((Time.LowPart != OneLeapYear.LowPart) || (Time.HighPart != OneLeapYear.HighPart)) {
  132. DbgPrint("****ERROR Time != OneLeapYear\n");
  133. }
  134. RtlTimeToTimeFields( (PLARGE_INTEGER)&OneCentury, &TimeFields );
  135. DbgPrint(" + 1 Century = "); PrintTimeFields( &TimeFields ); DbgPrint("\n");
  136. if (!RtlTimeFieldsToTime( &TimeFields, &Time )) {
  137. DbgPrint("****ERROR converting TimeFields back to OneCentury\n");
  138. }
  139. if ((Time.LowPart != OneCentury.LowPart) || (Time.HighPart != OneCentury.HighPart)) {
  140. DbgPrint("****ERROR Time != OneCentury\n");
  141. }
  142. RtlTimeToTimeFields( (PLARGE_INTEGER)&TwoCenturies, &TimeFields );
  143. DbgPrint(" + 2 Centuries = "); PrintTimeFields( &TimeFields ); DbgPrint("\n");
  144. if (!RtlTimeFieldsToTime( &TimeFields, &Time )) {
  145. DbgPrint("****ERROR converting TimeFields back to TwoCenturies\n");
  146. }
  147. if ((Time.LowPart != TwoCenturies.LowPart) || (Time.HighPart != TwoCenturies.HighPart)) {
  148. DbgPrint("****ERROR Time != TwoCenturies\n");
  149. }
  150. RtlTimeToTimeFields( (PLARGE_INTEGER)&ThreeCenturies, &TimeFields );
  151. DbgPrint(" + 3 Centuries = "); PrintTimeFields( &TimeFields ); DbgPrint("\n");
  152. if (!RtlTimeFieldsToTime( &TimeFields, &Time )) {
  153. DbgPrint("****ERROR converting TimeFields back to ThreeCenturies\n");
  154. }
  155. if ((Time.LowPart != ThreeCenturies.LowPart) || (Time.HighPart != ThreeCenturies.HighPart)) {
  156. DbgPrint("****ERROR Time != ThreeCenturies\n");
  157. }
  158. RtlTimeToTimeFields( (PLARGE_INTEGER)&FourCenturies, &TimeFields );
  159. DbgPrint(" + 4 Centuries = "); PrintTimeFields( &TimeFields ); DbgPrint("\n");
  160. if (!RtlTimeFieldsToTime( &TimeFields, &Time )) {
  161. DbgPrint("****ERROR converting TimeFields back to FourCenturies\n");
  162. }
  163. if ((Time.LowPart != FourCenturies.LowPart) || (Time.HighPart != FourCenturies.HighPart)) {
  164. DbgPrint("****ERROR Time != FourCenturies\n");
  165. }
  166. RtlTimeToTimeFields( (PLARGE_INTEGER)&Sum, &TimeFields );
  167. DbgPrint(" + sum = "); PrintTimeFields( &TimeFields ); DbgPrint("\n");
  168. if (!RtlTimeFieldsToTime( &TimeFields, &Time )) {
  169. DbgPrint("****ERROR converting TimeFields back to Sum\n");
  170. }
  171. if ((Time.LowPart != Sum.LowPart) || (Time.HighPart != Sum.HighPart)) {
  172. DbgPrint("****ERROR Time != Sum\n");
  173. }
  174. DbgPrint("\n");
  175. //
  176. // Setup and test the start 1970 time
  177. //
  178. RtlSecondsSince1970ToTime( 0, &StartOf1970 );
  179. RtlTimeToTimeFields( &StartOf1970, &TimeFields );
  180. DbgPrint(" Start of 1970 = "); PrintTimeFields( &TimeFields ); DbgPrint("\n");
  181. if (!RtlTimeFieldsToTime( &TimeFields, &Time )) {
  182. DbgPrint("****ERROR converting TimeFields back to start of 1970\n");
  183. }
  184. if ((Time.LowPart != StartOf1970.LowPart) || (Time.HighPart != StartOf1970.HighPart)) {
  185. DbgPrint("****ERROR Time != StartOf1970\n");
  186. }
  187. if (!RtlTimeToSecondsSince1970( &StartOf1970, &i )) {
  188. DbgPrint("****ERROR converting time to seconds since 1970\n");
  189. }
  190. if (i != 0) {
  191. DbgPrint("****ERROR seconds since 1970 != 0\n");
  192. }
  193. //
  194. // Setup and test the start 1980 time
  195. //
  196. RtlSecondsSince1980ToTime( 0, &StartOf1980 );
  197. RtlTimeToTimeFields( &StartOf1980, &TimeFields );
  198. DbgPrint(" Start of 1980 = "); PrintTimeFields( &TimeFields ); DbgPrint("\n");
  199. if (!RtlTimeFieldsToTime( &TimeFields, &Time )) {
  200. DbgPrint("****ERROR converting TimeFields back to start of 1980\n");
  201. }
  202. if ((Time.LowPart != StartOf1980.LowPart) || (Time.HighPart != StartOf1980.HighPart)) {
  203. DbgPrint("****ERROR Time != StartOf1980\n");
  204. }
  205. if (!RtlTimeToSecondsSince1980( &StartOf1980, &i )) {
  206. DbgPrint("****ERROR converting time to seconds since 1980\n");
  207. }
  208. if (i != 0) {
  209. DbgPrint("****ERROR seconds since 1980 != 0\n");
  210. }
  211. //
  212. // Lets try to print the Christmas when Santa arrives for 1901 to 2001
  213. // every 10 years
  214. //
  215. TimeFields.Month = 12;
  216. TimeFields.Day = 25;
  217. TimeFields.Hour = 3;
  218. TimeFields.Minute = 30;
  219. TimeFields.Second = 15;
  220. TimeFields.Milliseconds = 250;
  221. for (i = 1901; i < 2002; i += 10) {
  222. TimeFields.Year = i;
  223. if (!RtlTimeFieldsToTime( &TimeFields, &Time )) {
  224. DbgPrint("****ERROR converting TimeFields to Christmas %4d\n", TimeFields.Year);
  225. }
  226. RtlTimeToTimeFields( &Time, &TimeFields );
  227. DbgPrint(" Christmas %4d = ", i); PrintTimeFields( &TimeFields ); DbgPrint("\n");
  228. }
  229. //
  230. // Let's see how old I really am, when I turn 10, 20, 30, ...
  231. //
  232. TimeFields.Month = 12;
  233. TimeFields.Day = 5;
  234. TimeFields.Hour = 3;
  235. TimeFields.Minute = 14;
  236. TimeFields.Second = 0;
  237. TimeFields.Milliseconds = 0;
  238. for (i = 1956; i <= 1956+60; i += 10) {
  239. TimeFields.Year = i;
  240. if (!RtlTimeFieldsToTime( &TimeFields, &Time )) {
  241. DbgPrint("****ERROR converting TimeFields to DOB %4d\n", TimeFields.Year);
  242. }
  243. RtlTimeToTimeFields( &Time, &TimeFields );
  244. DbgPrint(" DOB + %4d = ", i-1956); PrintTimeFields( &TimeFields ); DbgPrint("\n");
  245. }
  246. DbgPrint("End Time Test\n");
  247. return TRUE;
  248. }
  249. VOID
  250. PrintTimeFields (
  251. IN PTIME_FIELDS TimeFields
  252. )
  253. {
  254. static PCHAR Months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  255. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  256. static PCHAR Days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  257. DbgPrint(" %d", TimeFields->Year);
  258. DbgPrint("-%s", Months[TimeFields->Month-1]);
  259. DbgPrint("-%d", TimeFields->Day);
  260. DbgPrint(" %2d", TimeFields->Hour);
  261. DbgPrint(":%2d", TimeFields->Minute);
  262. DbgPrint(":%2d", TimeFields->Second);
  263. DbgPrint(".%3d", TimeFields->Milliseconds);
  264. DbgPrint(" (%s)", Days[TimeFields->Weekday]);
  265. }