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.

361 lines
9.4 KiB

  1. /*
  2. * Utility program to query and set the current time zone settings
  3. * in the registry.
  4. *
  5. * tz [-b Bias] [-s Name Bias Date] [-d Name Bias Date]
  6. *
  7. */
  8. #include "tz.h"
  9. void
  10. Usage( void )
  11. {
  12. fprintf( stderr, "usage: TZ [-b Bias] [-s Name Bias Date] [-d Name Bias Date]\n" );
  13. fprintf( stderr, "Where...\n" );
  14. fprintf( stderr, " Default with no parameters is to display current time zone settings.\n" );
  15. exit( 1 );
  16. }
  17. char KeyValueBuffer[ 4096 ];
  18. char *Order[] = {
  19. "first",
  20. "second",
  21. "third",
  22. "fourth",
  23. "last"
  24. };
  25. char *DayOfWeek[] = {
  26. "Sunday",
  27. "Monday",
  28. "Tuesday",
  29. "Wednesday",
  30. "Thursday",
  31. "Friday",
  32. "Saturday"
  33. };
  34. char *Months[] = {
  35. "January",
  36. "February",
  37. "March",
  38. "April",
  39. "May",
  40. "June",
  41. "July",
  42. "August",
  43. "September",
  44. "October",
  45. "November",
  46. "December"
  47. };
  48. #define NUMBER_DATE_TIME_FIELDS 6
  49. BOOL
  50. ParseTimeZoneInfo(
  51. IN LPSTR NameIn,
  52. OUT PWSTR NameOut,
  53. IN LPSTR BiasIn,
  54. OUT PLONG BiasOut,
  55. IN LPSTR StartIn,
  56. OUT PTIME_FIELDS StartOut
  57. )
  58. {
  59. LPSTR s, s1;
  60. ULONG FieldIndexes[ NUMBER_DATE_TIME_FIELDS ] = {1, 2, 0, 3, 4, 7};
  61. //
  62. // Month/Day/Year HH:MM DayOfWeek
  63. //
  64. ULONG CurrentField = 0;
  65. PCSHORT Fields;
  66. LPSTR Field;
  67. ULONG FieldValue;
  68. MultiByteToWideChar( CP_ACP,
  69. 0,
  70. NameIn,
  71. strlen( NameIn ),
  72. NameOut,
  73. 32
  74. );
  75. *BiasOut = atoi( BiasIn );
  76. s = StartIn;
  77. RtlZeroMemory( StartOut, sizeof( *StartOut ) );
  78. Fields = &StartOut->Year;
  79. while (*s) {
  80. if (CurrentField >= 7) {
  81. return( FALSE );
  82. }
  83. while (*s == ' ') {
  84. s++;
  85. }
  86. Field = s;
  87. while (*s) {
  88. if (CurrentField == (NUMBER_DATE_TIME_FIELDS-1)) {
  89. }
  90. else
  91. if (*s < '0' || *s > '9') {
  92. break;
  93. }
  94. s++;
  95. }
  96. if (*s) {
  97. s++;
  98. }
  99. if (CurrentField == (NUMBER_DATE_TIME_FIELDS-1)) {
  100. if (strlen( Field ) < 3) {
  101. printf( "TZ: %s invalid day of week length\n", Field );
  102. return FALSE;
  103. }
  104. if (StartOut->Year != 0) {
  105. printf( "TZ: Year must be zero to specify day of week\n" );
  106. return FALSE;
  107. }
  108. if (!_strnicmp( Field, "SUN", 3 )) {
  109. FieldValue = 0;
  110. }
  111. else
  112. if (!_strnicmp( Field, "MON", 3 )) {
  113. FieldValue = 1;
  114. }
  115. else
  116. if (!_strnicmp( Field, "TUE", 3 )) {
  117. FieldValue = 2;
  118. }
  119. else
  120. if (!_strnicmp( Field, "WED", 3 )) {
  121. FieldValue = 3;
  122. }
  123. else
  124. if (!_strnicmp( Field, "THU", 3 )) {
  125. FieldValue = 4;
  126. }
  127. else
  128. if (!_strnicmp( Field, "FRI", 3 )) {
  129. FieldValue = 5;
  130. }
  131. else
  132. if (!_strnicmp( Field, "SAT", 3 )) {
  133. FieldValue = 6;
  134. }
  135. else {
  136. printf( "TZ: %s invalid day of week\n", Field );
  137. return FALSE;
  138. }
  139. }
  140. else {
  141. FieldValue = atoi( Field );
  142. }
  143. Fields[ FieldIndexes[ CurrentField++ ] ] = (CSHORT)FieldValue;
  144. }
  145. if (StartOut->Year == 0) {
  146. if (StartOut->Day > 5) {
  147. printf( "TZ: Day must be 0 - 5 if year is zero.\n" );
  148. return FALSE;
  149. }
  150. }
  151. else
  152. if (StartOut->Year < 100) {
  153. StartOut->Year += 1900;
  154. }
  155. return TRUE;
  156. }
  157. int
  158. __cdecl
  159. main(
  160. int argc,
  161. char *argv[]
  162. )
  163. {
  164. int i, j;
  165. LPSTR s, s1, s2, s3, AmPm;
  166. NTSTATUS Status;
  167. RTL_TIME_ZONE_INFORMATION tzi;
  168. BOOLEAN InfoModified = FALSE;
  169. ConvertAppToOem( argc, argv );
  170. if (argc < 1) {
  171. Usage();
  172. }
  173. Status = RtlQueryTimeZoneInformation( &tzi );
  174. if (!NT_SUCCESS( Status )) {
  175. fprintf( stderr, "TZ: Unable to query current information.\n" );
  176. }
  177. while (--argc) {
  178. s = *++argv;
  179. if (*s == '-' || *s == '/') {
  180. while (*++s) {
  181. switch( tolower( *s ) ) {
  182. case 'b':
  183. if (argc) {
  184. --argc;
  185. tzi.Bias = atoi( *++argv );
  186. InfoModified = TRUE;
  187. }
  188. else {
  189. Usage();
  190. }
  191. break;
  192. case 's':
  193. if (argc >= 3) {
  194. argc -= 3;
  195. s1 = *++argv;
  196. s2 = *++argv;
  197. s3 = *++argv;
  198. if (ParseTimeZoneInfo( s1, tzi.StandardName,
  199. s2, &tzi.StandardBias,
  200. s3, &tzi.StandardStart
  201. )
  202. ) {
  203. InfoModified = TRUE;
  204. }
  205. else {
  206. Usage();
  207. }
  208. }
  209. else {
  210. Usage();
  211. }
  212. break;
  213. case 'd':
  214. if (argc >= 3) {
  215. argc -= 3;
  216. s1 = *++argv;
  217. s2 = *++argv;
  218. s3 = *++argv;
  219. if (ParseTimeZoneInfo( s1, tzi.DaylightName,
  220. s2, &tzi.DaylightBias,
  221. s3, &tzi.DaylightStart
  222. )
  223. ) {
  224. InfoModified = TRUE;
  225. }
  226. else {
  227. Usage();
  228. }
  229. }
  230. else {
  231. Usage();
  232. }
  233. break;
  234. default: Usage();
  235. }
  236. }
  237. }
  238. else {
  239. Usage();
  240. }
  241. }
  242. if (InfoModified) {
  243. Status = RtlSetTimeZoneInformation( &tzi );
  244. if (!NT_SUCCESS( Status )) {
  245. fprintf( stderr, "TZ: Unable to set new information.\n" );
  246. }
  247. }
  248. printf( "Time Zone Information.\n" );
  249. printf( " Bias from UTC: %u:%02u\n", tzi.Bias / 60, tzi.Bias % 60 );
  250. printf( " Standard time:\n" );
  251. printf( " Name: %ws\n", &tzi.StandardName );
  252. printf( " Bias: %d:%02d\n", tzi.StandardBias / 60, abs( tzi.StandardBias % 60 ) );
  253. if (tzi.StandardStart.Month != 0) {
  254. if (tzi.StandardStart.Hour > 12) {
  255. AmPm = "pm";
  256. tzi.StandardStart.Hour -= 12;
  257. }
  258. else {
  259. AmPm = "am";
  260. }
  261. printf( " Start: %02u:%02u%s", tzi.StandardStart.Hour, tzi.StandardStart.Minute, AmPm );
  262. if (tzi.StandardStart.Year == 0) {
  263. printf( " %s %s of %s\n",
  264. Order[ tzi.StandardStart.Day - 1 ],
  265. DayOfWeek[ tzi.StandardStart.Weekday ],
  266. Months[ tzi.StandardStart.Month - 1 ]
  267. );
  268. }
  269. else {
  270. printf( "%s %02u, %u\n",
  271. Months[ tzi.StandardStart.Month - 1 ],
  272. tzi.StandardStart.Month, tzi.StandardStart.Day, tzi.StandardStart.Year
  273. );
  274. }
  275. }
  276. printf( " Daylight time:\n" );
  277. printf( " Name: %ws\n", &tzi.DaylightName );
  278. printf( " Bias: %d:%02d\n", tzi.DaylightBias / 60, abs( tzi.DaylightBias % 60 ) );
  279. if (tzi.DaylightStart.Month != 0) {
  280. if (tzi.DaylightStart.Hour > 12) {
  281. AmPm = "pm";
  282. tzi.DaylightStart.Hour -= 12;
  283. }
  284. else {
  285. AmPm = "am";
  286. }
  287. printf( " Start: %02u:%02u%s", tzi.DaylightStart.Hour, tzi.DaylightStart.Minute, AmPm );
  288. if (tzi.DaylightStart.Year == 0) {
  289. printf( " %s %s of %s\n",
  290. Order[ tzi.DaylightStart.Day - 1 ],
  291. DayOfWeek[ tzi.DaylightStart.Weekday ],
  292. Months[ tzi.DaylightStart.Month - 1 ]
  293. );
  294. }
  295. else {
  296. printf( "%s %02u, %u\n",
  297. Months[ tzi.DaylightStart.Month - 1 ],
  298. tzi.DaylightStart.Month, tzi.DaylightStart.Day, tzi.DaylightStart.Year
  299. );
  300. }
  301. }
  302. return( 0 );
  303. }
  304.