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.

340 lines
7.6 KiB

  1. <%
  2. ' Use these constants as parameters to the UIDateFormat constructor to
  3. ' determine the format used in getDate()
  4. Const DATEFORMAT_LONG = 0
  5. Const DATEFORMAT_SHORT = 1
  6. %>
  7. <SCRIPT LANGUAGE="JavaScript">
  8. MONTHS = new Array()
  9. MONTHS[0] = "<%= L_JAN_TEXT %>";
  10. MONTHS[1] = "<%= L_FEB_TEXT %>";
  11. MONTHS[2] = "<%= L_MAR_TEXT %>";
  12. MONTHS[3] = "<%= L_APR_TEXT %>";
  13. MONTHS[4] = "<%= L_MAY_TEXT %>";
  14. MONTHS[5] = "<%= L_JUN_TEXT %>";
  15. MONTHS[6] = "<%= L_JUL_TEXT %>";
  16. MONTHS[7] = "<%= L_AUG_TEXT %>";
  17. MONTHS[8] = "<%= L_SEP_TEXT %>";
  18. MONTHS[9] = "<%= L_OCT_TEXT %>";
  19. MONTHS[10] = "<%= L_NOV_TEXT %>";
  20. MONTHS[11] = "<%= L_DEC_TEXT %>";
  21. DAYS = new Array()
  22. DAYS[0] = "<%= L_SUN_TEXT %>";
  23. DAYS[1] = "<%= L_MON_TEXT %>";
  24. DAYS[2] = "<%= L_TUE_TEXT %>";
  25. DAYS[3] = "<%= L_WED_TEXT %>";
  26. DAYS[4] = "<%= L_THU_TEXT %>";
  27. DAYS[5] = "<%= L_FRI_TEXT %>";
  28. DAYS[6] = "<%= L_SAT_TEXT %>";
  29. DAYTIME = new Array()
  30. DAYTIME[0] = "<%= L_MORNING_TEXT %>"
  31. DAYTIME[1] = "<%= L_AFTERNOON_TEXT %>"
  32. // UIDateFormat
  33. //
  34. // Date conversion class. To use: create a UIDateFormat object
  35. // with a flag indicating the type of date formatting desired
  36. // (DATEFORMAT_LONG or DATEFORMAT_SHORT) and use the getDate()
  37. // and getTime() methods to return a locale formatted date.
  38. function UIDateFormat( fDateFormat )
  39. {
  40. // Public interface - use these methods to display localized dates.
  41. // Both methods take a single JavaScript Date object as a parameter.
  42. this.getDate = uidGetDate;
  43. this.getTime = uidGetTime;
  44. // Implementation
  45. this.iDaytimeIndex = 0;
  46. this.strTimeFormat = "<%= L_TIME_FORMAT_TEXT %>";
  47. // strDate format can be changed to point to the long or short
  48. // date format as needed by the particular page.
  49. if( fDateFormat == <%= DATEFORMAT_LONG %> )
  50. {
  51. this.strDateFormat = "<%= L_DATE_LONGFORMAT_TEXT %>";
  52. }
  53. else
  54. {
  55. this.strDateFormat = "<%= L_DATE_SHORTFORMAT_TEXT %>";
  56. }
  57. this.strTimeTokens = "hHms";
  58. this.strDateTokens = "dMy";
  59. // helper methods
  60. this.uidTimeStringPartFromToken = uidTimeStringPartFromToken;
  61. this.uidDateStringPartFromToken = uidDateStringPartFromToken;
  62. }
  63. // getNeutralDateString
  64. //
  65. // Global utility function. Formats a date into a neutral format m/d/yyyy.
  66. // This shouldn't be displayed, but rather used to save the date
  67. // part in a format that doesn't lose any information.
  68. function getNeutralDateString( date )
  69. {
  70. var theYear = date.getYear();
  71. if( theYear < 1000 )
  72. {
  73. theYear += 1900;
  74. }
  75. var theMonth = date.getMonth() + 1;
  76. return "" + theMonth + "/" + date.getDate() + "/" + theYear;
  77. }
  78. // uidGetDate
  79. //
  80. // Returns the date part of the string. Called through UIDate.getDate().
  81. // Make a single pass through the date format string, extracting format
  82. // tokens. Uses uidDateStringPartFromToken() to interpret found tokens.
  83. //
  84. // date - the date value being translated
  85. function uidGetDate( date )
  86. {
  87. var strOut = "";
  88. var i = 0, j = 0;
  89. var nFormatStrLen = this.strDateFormat.length;
  90. var c;
  91. var nTokenType = -1;
  92. for( i = 0; i < nFormatStrLen; i++ )
  93. {
  94. c = this.strDateFormat.charAt(i);
  95. nTokenType = this.strDateTokens.indexOf( c );
  96. if( nTokenType != -1 )
  97. {
  98. // We found a token
  99. // Move to the next position after the current token.
  100. for( j = i + 1; j < nFormatStrLen; j++ )
  101. {
  102. if( c != this.strDateFormat.charAt(j) )
  103. {
  104. break;
  105. }
  106. }
  107. strOut += this.uidDateStringPartFromToken( date, nTokenType, j - i );
  108. i = j - 1;
  109. }
  110. else if( c == "'" )
  111. {
  112. // Interpret characters between single quotes as multichar literals
  113. // Skip over characters between the single quotes
  114. for( j = i + 1; j < nFormatStrLen; j++ )
  115. {
  116. if( c == this.strDateFormat.charAt(j) )
  117. {
  118. break;
  119. }
  120. }
  121. strOut += this.strDateFormat.substring( i + 1, j );
  122. i = j;
  123. }
  124. else
  125. {
  126. // literal character not quote delimited.
  127. strOut += c;
  128. }
  129. }
  130. return strOut;
  131. }
  132. // uidDateStringPartFromToken
  133. //
  134. // Returns a string representing the localized value of the date part
  135. // indicated by nTokenType.
  136. //
  137. // date - the javascript date value being translated
  138. // nTokenType - position in token string - "dMy"
  139. // nTokenChars - number of characters comprising the token
  140. function uidDateStringPartFromToken( date, nTokenType, nTokenChars )
  141. {
  142. var strOut = "";
  143. var datePart;
  144. if( nTokenType == 0 )
  145. {
  146. // Days
  147. if( nTokenChars > 2 )
  148. {
  149. // The day of the week
  150. strOut += DAYS[date.getDay()];
  151. }
  152. else
  153. {
  154. // The day of the month
  155. datePart = date.getDate();
  156. if( datePart < 10 && nTokenChars == 2 )
  157. {
  158. strOut += "0";
  159. }
  160. strOut += datePart;
  161. }
  162. }
  163. else if( nTokenType == 1 )
  164. {
  165. // Months
  166. datePart = date.getMonth();
  167. if( nTokenChars > 2 )
  168. {
  169. // Month as string
  170. strOut += MONTHS[datePart];
  171. }
  172. else
  173. {
  174. // Month as integer
  175. datePart++;
  176. if( datePart < 10 && nTokenChars == 2 )
  177. {
  178. strOut += "0";
  179. }
  180. strOut += datePart;
  181. }
  182. }
  183. else if( nTokenType == 2 )
  184. {
  185. // Year
  186. // Get the full year as a workaround for JavaScript bogusness.
  187. datePart = date.getYear();
  188. if( datePart < 1000 )
  189. {
  190. datePart += 1900;
  191. }
  192. strOut += datePart;
  193. if( nTokenChars < 4 )
  194. {
  195. // two digit year
  196. strOut = strOut.substring( 2, 4 );
  197. }
  198. }
  199. return strOut;
  200. }
  201. // uidGetTime
  202. // Converts the date into a time string. Call through UIDate.getTime( date )
  203. //
  204. // date - the javascript date value being translated
  205. function uidGetTime( date )
  206. {
  207. var strOut = "";
  208. var i = 0, j = 0;
  209. var nFormatStrLen = this.strTimeFormat.length;
  210. var c;
  211. var nTokenType = -1;
  212. var nDaytimeStart = -1;
  213. var nDaytimeLen = 0;
  214. for( i = 0; i < nFormatStrLen; i++ )
  215. {
  216. c = this.strTimeFormat.charAt(i);
  217. nTokenType = this.strTimeTokens.indexOf( c );
  218. if( nTokenType != -1 )
  219. {
  220. // We found a token
  221. j = i + 1;
  222. if( c == this.strTimeFormat.charAt(j) )
  223. {
  224. // double token
  225. j++;
  226. }
  227. strOut += this.uidTimeStringPartFromToken( date, nTokenType, j - i );
  228. // skip the token
  229. i = j - 1;
  230. }
  231. else if( c == 't' )
  232. {
  233. // This is the daytime designation. Since we may not yet know
  234. // if it's morning or afternoon, save the current position in
  235. // the string, and the number of t's. Just dump the t's back
  236. // into the string, we'll replace them later when we know
  237. // what time of the day it is.
  238. nDaytimeStart = strOut.length;
  239. nDaytimeLen = 0;
  240. // It would be nice to do-while, but 3.0 browsers choke on it
  241. while( c == this.strTimeFormat.charAt(i + nDaytimeLen) )
  242. {
  243. nDaytimeLen++;
  244. strOut += c;
  245. }
  246. // skip the token
  247. i += (nDaytimeLen - 1);
  248. }
  249. else
  250. {
  251. strOut += c;
  252. }
  253. }
  254. // Insert the daytime designation into the string, if we found one
  255. if( nDaytimeStart != -1 )
  256. {
  257. strOut = strOut.substring( 0, nDaytimeStart ) +
  258. DAYTIME[this.iDaytimeIndex] +
  259. strOut.substring( nDaytimeStart + nDaytimeLen );
  260. }
  261. return strOut;
  262. }
  263. // uidTimeStringPartFromToken
  264. //
  265. // Returns the time string value indicted by nTokenType
  266. //
  267. // date - the date value being translated
  268. // nTokenType - the position in the token string
  269. // nTokenChars - the number of consecutive tokens in the format string
  270. function uidTimeStringPartFromToken( date, nTokenType, nTokenChars )
  271. {
  272. var strOut = "";
  273. var timePart;
  274. if( nTokenType <= 1 )
  275. {
  276. // Use the hour indicator to determine whether it's AM/PM and
  277. // determine if we need to reparse
  278. timePart = date.getHours();
  279. if( timePart < 12 )
  280. {
  281. this.iDaytimeIndex = 0;
  282. }
  283. else
  284. {
  285. this.iDaytimeIndex = 1;
  286. }
  287. if( nTokenType == 0 )
  288. {
  289. // Using 12 hour clock
  290. if( timePart > 12 )
  291. {
  292. timePart -= 12;
  293. }
  294. }
  295. }
  296. else if( nTokenType == 2 )
  297. {
  298. timePart = date.getMinutes();
  299. }
  300. else if( nTokenType == 3 )
  301. {
  302. timePart = date.getSeconds();
  303. }
  304. if( nTokenChars > 1 && timePart < 10 )
  305. {
  306. strOut = "0";
  307. }
  308. strOut += timePart;
  309. return strOut;
  310. }
  311. </SCRIPT>