datetimeformat_test.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.i18n.DateTimeFormatTest');
  15. goog.setTestOnly('goog.i18n.DateTimeFormatTest');
  16. goog.require('goog.date.Date');
  17. goog.require('goog.date.DateTime');
  18. goog.require('goog.i18n.DateTimeFormat');
  19. goog.require('goog.i18n.DateTimePatterns');
  20. goog.require('goog.i18n.DateTimePatterns_ar');
  21. goog.require('goog.i18n.DateTimePatterns_de');
  22. goog.require('goog.i18n.DateTimePatterns_en');
  23. goog.require('goog.i18n.DateTimePatterns_fa');
  24. goog.require('goog.i18n.DateTimePatterns_fr');
  25. goog.require('goog.i18n.DateTimePatterns_ja');
  26. goog.require('goog.i18n.DateTimePatterns_sv');
  27. goog.require('goog.i18n.DateTimeSymbols');
  28. goog.require('goog.i18n.DateTimeSymbols_ar');
  29. goog.require('goog.i18n.DateTimeSymbols_ar_AE');
  30. goog.require('goog.i18n.DateTimeSymbols_ar_SA');
  31. goog.require('goog.i18n.DateTimeSymbols_bn_BD');
  32. goog.require('goog.i18n.DateTimeSymbols_de');
  33. goog.require('goog.i18n.DateTimeSymbols_en');
  34. goog.require('goog.i18n.DateTimeSymbols_en_GB');
  35. goog.require('goog.i18n.DateTimeSymbols_en_IE');
  36. goog.require('goog.i18n.DateTimeSymbols_en_IN');
  37. goog.require('goog.i18n.DateTimeSymbols_en_US');
  38. goog.require('goog.i18n.DateTimeSymbols_fa');
  39. goog.require('goog.i18n.DateTimeSymbols_fr');
  40. goog.require('goog.i18n.DateTimeSymbols_fr_DJ');
  41. goog.require('goog.i18n.DateTimeSymbols_he_IL');
  42. goog.require('goog.i18n.DateTimeSymbols_ja');
  43. goog.require('goog.i18n.DateTimeSymbols_ro_RO');
  44. goog.require('goog.i18n.DateTimeSymbols_sv');
  45. goog.require('goog.i18n.TimeZone');
  46. goog.require('goog.testing.jsunit');
  47. // Initial values
  48. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_en;
  49. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_en;
  50. function tearDown() {
  51. // We always revert to a known state
  52. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_en;
  53. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_en;
  54. goog.i18n.DateTimeFormat.setEnforceAsciiDigits(false);
  55. }
  56. // Helpers to make tests work regardless of the timeZone we're in.
  57. function timezoneString(date) {
  58. var timeZone = goog.i18n.TimeZone.createTimeZone(date.getTimezoneOffset());
  59. return timeZone.getShortName(date);
  60. }
  61. function timezoneId(date) {
  62. var timeZone = goog.i18n.TimeZone.createTimeZone(date.getTimezoneOffset());
  63. return timeZone.getTimeZoneId(date);
  64. }
  65. function timezoneStringRFC(date) {
  66. var timeZone = goog.i18n.TimeZone.createTimeZone(date.getTimezoneOffset());
  67. return timeZone.getRFCTimeZoneString(date);
  68. }
  69. // Where could such data be found
  70. // In js_i18n_data in http://go/i18n_dir, we have a bunch of files with names
  71. // like TimeZoneConstant__<locale>.js
  72. // We strongly discourage you to use them directly as those data can make
  73. // your client code bloated. You should try to provide this data from server
  74. // in a selective manner. In typical scenario, user's time zone is retrieved
  75. // and only data for that time zone should be provided.
  76. var americaLosAngelesData = {
  77. 'transitions': [
  78. 2770, 60, 7137, 0, 11506, 60, 16041, 0, 20410, 60, 24777, 0,
  79. 29146, 60, 33513, 0, 35194, 60, 42249, 0, 45106, 60, 50985, 0,
  80. 55354, 60, 59889, 0, 64090, 60, 68625, 0, 72994, 60, 77361, 0,
  81. 81730, 60, 86097, 0, 90466, 60, 94833, 0, 99202, 60, 103569, 0,
  82. 107938, 60, 112473, 0, 116674, 60, 121209, 0, 125578, 60, 129945, 0,
  83. 134314, 60, 138681, 0, 143050, 60, 147417, 0, 151282, 60, 156153, 0,
  84. 160018, 60, 165057, 0, 168754, 60, 173793, 0, 177490, 60, 182529, 0,
  85. 186394, 60, 191265, 0, 195130, 60, 200001, 0, 203866, 60, 208905, 0,
  86. 212602, 60, 217641, 0, 221338, 60, 226377, 0, 230242, 60, 235113, 0,
  87. 238978, 60, 243849, 0, 247714, 60, 252585, 0, 256450, 60, 261489, 0,
  88. 265186, 60, 270225, 0, 273922, 60, 278961, 0, 282826, 60, 287697, 0,
  89. 291562, 60, 296433, 0, 300298, 60, 305337, 0, 309034, 60, 314073, 0,
  90. 317770, 60, 322809, 0, 326002, 60, 331713, 0, 334738, 60, 340449, 0,
  91. 343474, 60, 349185, 0, 352378, 60, 358089, 0, 361114, 60, 366825, 0,
  92. 369850, 60, 375561, 0, 378586, 60, 384297, 0, 387322, 60, 393033, 0,
  93. 396058, 60, 401769, 0, 404962, 60, 410673, 0, 413698, 60, 419409, 0,
  94. 422434, 60, 428145, 0, 431170, 60, 436881, 0, 439906, 60, 445617, 0,
  95. 448810, 60, 454521, 0, 457546, 60, 463257, 0, 466282, 60, 471993, 0,
  96. 475018, 60, 480729, 0, 483754, 60, 489465, 0, 492490, 60, 498201, 0,
  97. 501394, 60, 507105, 0, 510130, 60, 515841, 0, 518866, 60, 524577, 0,
  98. 527602, 60, 533313, 0, 536338, 60, 542049, 0, 545242, 60, 550953, 0,
  99. 553978, 60, 559689, 0, 562714, 60, 568425, 0, 571450, 60, 577161, 0,
  100. 580186, 60, 585897, 0, 588922, 60, 594633, 0
  101. ],
  102. 'names': ['PST', 'Pacific Standard Time', 'PDT', 'Pacific Daylight Time'],
  103. 'names_ext': {
  104. STD_LONG_NAME_GMT: 'GMT-08:00',
  105. STD_GENERIC_LOCATION: 'Los Angeles Time',
  106. DST_LONG_NAME_GMT: 'GMT-07:00',
  107. DST_GENERIC_LOCATION: 'Los Angeles Time'
  108. },
  109. 'id': 'America/Los_Angeles',
  110. 'std_offset': -480
  111. };
  112. var europeBerlinData = {
  113. 'transitions': [
  114. 89953, 60, 94153, 0, 98521, 60, 102889, 0, 107257, 60, 111625, 0,
  115. 115993, 60, 120361, 0, 124729, 60, 129265, 0, 133633, 60, 138001, 0,
  116. 142369, 60, 146737, 0, 151105, 60, 155473, 0, 159841, 60, 164209, 0,
  117. 168577, 60, 172945, 0, 177313, 60, 181849, 0, 186217, 60, 190585, 0,
  118. 194953, 60, 199321, 0, 203689, 60, 208057, 0, 212425, 60, 216793, 0,
  119. 221161, 60, 225529, 0, 230065, 60, 235105, 0, 238801, 60, 243841, 0,
  120. 247537, 60, 252577, 0, 256273, 60, 261481, 0, 265009, 60, 270217, 0,
  121. 273745, 60, 278953, 0, 282649, 60, 287689, 0, 291385, 60, 296425, 0,
  122. 300121, 60, 305329, 0, 308857, 60, 314065, 0, 317593, 60, 322801, 0,
  123. 326329, 60, 331537, 0, 335233, 60, 340273, 0, 343969, 60, 349009, 0,
  124. 352705, 60, 357913, 0, 361441, 60, 366649, 0, 370177, 60, 375385, 0,
  125. 379081, 60, 384121, 0, 387817, 60, 392857, 0, 396553, 60, 401593, 0,
  126. 405289, 60, 410497, 0, 414025, 60, 419233, 0, 422761, 60, 427969, 0,
  127. 431665, 60, 436705, 0, 440401, 60, 445441, 0, 449137, 60, 454345, 0,
  128. 457873, 60, 463081, 0, 466609, 60, 471817, 0, 475513, 60, 480553, 0,
  129. 484249, 60, 489289, 0, 492985, 60, 498025, 0, 501721, 60, 506929, 0,
  130. 510457, 60, 515665, 0, 519193, 60, 524401, 0, 528097, 60, 533137, 0,
  131. 536833, 60, 541873, 0, 545569, 60, 550777, 0, 554305, 60, 559513, 0,
  132. 563041, 60, 568249, 0, 571777, 60, 576985, 0, 580681, 60, 585721, 0,
  133. 589417, 60, 594457, 0
  134. ],
  135. 'names': [
  136. 'MEZ', 'Mitteleurop\u00e4ische Zeit', 'MESZ',
  137. 'Mitteleurop\u00e4ische Sommerzeit'
  138. ],
  139. 'names_ext': {
  140. STD_LONG_NAME_GMT: 'GMT+01:00',
  141. STD_GENERIC_LOCATION: 'Deutschland Zeit',
  142. DST_LONG_NAME_GMT: 'GMT+02:00',
  143. DST_GENERIC_LOCATION: 'Deutschland Zeit'
  144. },
  145. 'id': 'Europe/Berlin',
  146. 'std_offset': 60
  147. };
  148. var date;
  149. function testHHmmss() {
  150. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  151. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  152. date = new Date(2006, 6, 27, 13, 10, 10, 250);
  153. var fmt = new goog.i18n.DateTimeFormat('HH:mm:ss');
  154. assertEquals('13:10:10', fmt.format(date));
  155. }
  156. function testhhmmssa() {
  157. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  158. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  159. date = new Date(2006, 6, 27, 13, 10, 10, 250);
  160. var fmt = new goog.i18n.DateTimeFormat('h:mm:ss a');
  161. assertEquals('1:10:10 nachm.', fmt.format(date));
  162. }
  163. function testEEEMMMddyy() {
  164. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  165. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  166. date = new Date(2006, 6, 27, 13, 10, 10, 250);
  167. var fmt = new goog.i18n.DateTimeFormat('EEE, MMM d, yy');
  168. assertEquals('Do., Juli 27, 06', fmt.format(date));
  169. }
  170. function testEEEEMMMddyy() {
  171. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  172. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  173. date = new Date(2006, 6, 27, 13, 10, 10, 250);
  174. var fmt = new goog.i18n.DateTimeFormat('EEEE,MMMM dd, yyyy');
  175. assertEquals('Donnerstag,Juli 27, 2006', fmt.format(date));
  176. }
  177. function testyyyyMMddG() {
  178. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  179. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  180. date = new Date(Date.UTC(2006, 6, 27, 13, 10, 10, 250));
  181. var timeZone =
  182. goog.i18n.TimeZone.createTimeZone(420, goog.i18n.DateTimeSymbols_de);
  183. var fmt = new goog.i18n.DateTimeFormat('yyyy.MM.dd G \'at\' HH:mm:ss vvvv');
  184. assertEquals(
  185. '2006.07.27 n. Chr. at 06:10:10 Etc/GMT+7', fmt.format(date, timeZone));
  186. }
  187. function testyyyyyMMMMM() {
  188. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  189. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  190. date = new Date(2006, 6, 27, 13, 10, 10, 250);
  191. var fmt = new goog.i18n.DateTimeFormat('yyyyy.MMMMM.dd GGG hh:mm aaa');
  192. assertEquals('02006.J.27 n. Chr. 01:10 nachm.', fmt.format(date));
  193. date = new Date(972, 11, 25, 13, 10, 10, 250);
  194. var fmt = new goog.i18n.DateTimeFormat('yyyyy.MMMMM.dd');
  195. assertEquals('00972.D.25', fmt.format(date));
  196. }
  197. function testQQQQyy() {
  198. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  199. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  200. date = new Date(2006, 0, 27, 13, 10, 10, 250);
  201. var fmt = new goog.i18n.DateTimeFormat('QQQQ yy');
  202. assertEquals('1. Quartal 06', fmt.format(date));
  203. date = new Date(2006, 1, 27, 13, 10, 10, 250);
  204. assertEquals('1. Quartal 06', fmt.format(date));
  205. date = new Date(2006, 2, 27, 13, 10, 10, 250);
  206. assertEquals('1. Quartal 06', fmt.format(date));
  207. date = new Date(2006, 3, 27, 13, 10, 10, 250);
  208. assertEquals('2. Quartal 06', fmt.format(date));
  209. date = new Date(2006, 4, 27, 13, 10, 10, 250);
  210. assertEquals('2. Quartal 06', fmt.format(date));
  211. date = new Date(2006, 5, 27, 13, 10, 10, 250);
  212. assertEquals('2. Quartal 06', fmt.format(date));
  213. date = new Date(2006, 6, 27, 13, 10, 10, 250);
  214. assertEquals('3. Quartal 06', fmt.format(date));
  215. date = new Date(2006, 7, 27, 13, 10, 10, 250);
  216. assertEquals('3. Quartal 06', fmt.format(date));
  217. date = new Date(2006, 8, 27, 13, 10, 10, 250);
  218. assertEquals('3. Quartal 06', fmt.format(date));
  219. date = new Date(2006, 9, 27, 13, 10, 10, 250);
  220. assertEquals('4. Quartal 06', fmt.format(date));
  221. date = new Date(2006, 10, 27, 13, 10, 10, 250);
  222. assertEquals('4. Quartal 06', fmt.format(date));
  223. date = new Date(2006, 11, 27, 13, 10, 10, 250);
  224. assertEquals('4. Quartal 06', fmt.format(date));
  225. }
  226. function testQQyyyy() {
  227. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  228. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  229. date = new Date(2006, 0, 27, 13, 10, 10, 250);
  230. var fmt = new goog.i18n.DateTimeFormat('QQ yyyy');
  231. assertEquals('Q1 2006', fmt.format(date));
  232. date = new Date(2006, 1, 27, 13, 10, 10, 250);
  233. assertEquals('Q1 2006', fmt.format(date));
  234. date = new Date(2006, 2, 27, 13, 10, 10, 250);
  235. assertEquals('Q1 2006', fmt.format(date));
  236. date = new Date(2006, 3, 27, 13, 10, 10, 250);
  237. assertEquals('Q2 2006', fmt.format(date));
  238. date = new Date(2006, 4, 27, 13, 10, 10, 250);
  239. assertEquals('Q2 2006', fmt.format(date));
  240. date = new Date(2006, 5, 27, 13, 10, 10, 250);
  241. assertEquals('Q2 2006', fmt.format(date));
  242. date = new Date(2006, 6, 27, 13, 10, 10, 250);
  243. assertEquals('Q3 2006', fmt.format(date));
  244. date = new Date(2006, 7, 27, 13, 10, 10, 250);
  245. assertEquals('Q3 2006', fmt.format(date));
  246. date = new Date(2006, 8, 27, 13, 10, 10, 250);
  247. assertEquals('Q3 2006', fmt.format(date));
  248. date = new Date(2006, 9, 27, 13, 10, 10, 250);
  249. assertEquals('Q4 2006', fmt.format(date));
  250. date = new Date(2006, 10, 27, 13, 10, 10, 250);
  251. assertEquals('Q4 2006', fmt.format(date));
  252. date = new Date(2006, 11, 27, 13, 10, 10, 250);
  253. assertEquals('Q4 2006', fmt.format(date));
  254. }
  255. function testMMddyyyyHHmmsszzz() {
  256. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  257. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  258. date = new Date(2006, 6, 27, 13, 10, 10, 250);
  259. var fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss zzz');
  260. assertEquals('07/27/2006 13:10:10 ' + timezoneString(date), fmt.format(date));
  261. }
  262. function testMMddyyyyHHmmssZ() {
  263. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  264. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  265. date = new Date(2006, 6, 27, 13, 10, 10, 250);
  266. var fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss Z');
  267. assertEquals(
  268. '07/27/2006 13:10:10 ' + timezoneStringRFC(date), fmt.format(date));
  269. }
  270. function testPatternMonthDayMedium() {
  271. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  272. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  273. date = new Date(2006, 6, 27, 13, 10, 10, 250);
  274. var fmt =
  275. new goog.i18n.DateTimeFormat(goog.i18n.DateTimePatterns.MONTH_DAY_MEDIUM);
  276. assertEquals('27. Juli', fmt.format(date));
  277. }
  278. function testPatternDayOfWeekMonthDayMedium() {
  279. date = new Date(2006, 6, 27, 13, 10, 10, 250);
  280. var fmt = new goog.i18n.DateTimeFormat(
  281. goog.i18n.DateTimePatterns.WEEKDAY_MONTH_DAY_MEDIUM);
  282. assertEquals('Thu, Jul 27', fmt.format(date));
  283. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  284. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  285. var fmt = new goog.i18n.DateTimeFormat(
  286. goog.i18n.DateTimePatterns.WEEKDAY_MONTH_DAY_MEDIUM);
  287. assertEquals('Do., 27. Juli', fmt.format(date));
  288. }
  289. function testPatternDayOfWeekMonthDayYearMedium() {
  290. date = new Date(2012, 5, 28, 13, 10, 10, 250);
  291. var fmt = new goog.i18n.DateTimeFormat(
  292. goog.i18n.DateTimePatterns.WEEKDAY_MONTH_DAY_YEAR_MEDIUM);
  293. assertEquals('Thu, Jun 28, 2012', fmt.format(date));
  294. var fmt = new goog.i18n.DateTimeFormat(
  295. goog.i18n.DateTimePatterns.MONTH_DAY_YEAR_MEDIUM);
  296. assertEquals('Jun 28, 2012', fmt.format(date));
  297. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_sv;
  298. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_sv;
  299. var fmt = new goog.i18n.DateTimeFormat(
  300. goog.i18n.DateTimePatterns.WEEKDAY_MONTH_DAY_YEAR_MEDIUM);
  301. assertEquals('tors 28 juni 2012', fmt.format(date));
  302. var fmt = new goog.i18n.DateTimeFormat(
  303. goog.i18n.DateTimePatterns.MONTH_DAY_YEAR_MEDIUM);
  304. assertEquals('28 juni 2012', fmt.format(date));
  305. }
  306. function testQuote() {
  307. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  308. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  309. date = new Date(2006, 6, 27, 13, 10, 10, 250);
  310. var fmt = new goog.i18n.DateTimeFormat('HH \'o\'\'clock\'');
  311. assertEquals('13 o\'clock', fmt.format(date));
  312. fmt = new goog.i18n.DateTimeFormat('HH \'oclock\'');
  313. assertEquals('13 oclock', fmt.format(date));
  314. fmt = new goog.i18n.DateTimeFormat('HH \'\'');
  315. assertEquals('13 \'', fmt.format(date));
  316. }
  317. function testFractionalSeconds() {
  318. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  319. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  320. date = new Date(2006, 6, 27, 13, 10, 10, 256);
  321. var fmt = new goog.i18n.DateTimeFormat('s:S');
  322. assertEquals('10:3', fmt.format(date));
  323. var fmt = new goog.i18n.DateTimeFormat('s:SS');
  324. assertEquals('10:26', fmt.format(date));
  325. var fmt = new goog.i18n.DateTimeFormat('s:SSS');
  326. assertEquals('10:256', fmt.format(date));
  327. var fmt = new goog.i18n.DateTimeFormat('s:SSSS');
  328. assertEquals('10:2560', fmt.format(date));
  329. var fmt = new goog.i18n.DateTimeFormat('s:SSSSS');
  330. assertEquals('10:25600', fmt.format(date));
  331. }
  332. function testPredefinedFormatter() {
  333. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  334. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  335. date = new Date(2006, 7, 4, 13, 49, 24, 0);
  336. var fmt =
  337. new goog.i18n.DateTimeFormat(goog.i18n.DateTimeFormat.Format.FULL_DATE);
  338. assertEquals('Freitag, 4. August 2006', fmt.format(date));
  339. fmt = new goog.i18n.DateTimeFormat(goog.i18n.DateTimeFormat.Format.LONG_DATE);
  340. assertEquals('4. August 2006', fmt.format(date));
  341. fmt =
  342. new goog.i18n.DateTimeFormat(goog.i18n.DateTimeFormat.Format.MEDIUM_DATE);
  343. assertEquals('04.08.2006', fmt.format(date));
  344. fmt =
  345. new goog.i18n.DateTimeFormat(goog.i18n.DateTimeFormat.Format.SHORT_DATE);
  346. assertEquals('04.08.06', fmt.format(date));
  347. fmt = new goog.i18n.DateTimeFormat(goog.i18n.DateTimeFormat.Format.FULL_TIME);
  348. assertEquals('13:49:24 ' + timezoneString(date), fmt.format(date));
  349. fmt = new goog.i18n.DateTimeFormat(goog.i18n.DateTimeFormat.Format.LONG_TIME);
  350. assertEquals('13:49:24 ' + timezoneString(date), fmt.format(date));
  351. fmt =
  352. new goog.i18n.DateTimeFormat(goog.i18n.DateTimeFormat.Format.MEDIUM_TIME);
  353. assertEquals('13:49:24', fmt.format(date));
  354. fmt =
  355. new goog.i18n.DateTimeFormat(goog.i18n.DateTimeFormat.Format.SHORT_TIME);
  356. assertEquals('13:49', fmt.format(date));
  357. fmt = new goog.i18n.DateTimeFormat(
  358. goog.i18n.DateTimeFormat.Format.FULL_DATETIME);
  359. assertEquals(
  360. 'Freitag, 4. August 2006 um 13:49:24 ' + timezoneString(date),
  361. fmt.format(date));
  362. fmt = new goog.i18n.DateTimeFormat(
  363. goog.i18n.DateTimeFormat.Format.LONG_DATETIME);
  364. assertEquals(
  365. '4. August 2006 um 13:49:24 ' + timezoneString(date), fmt.format(date));
  366. fmt = new goog.i18n.DateTimeFormat(
  367. goog.i18n.DateTimeFormat.Format.MEDIUM_DATETIME);
  368. assertEquals('04.08.2006, 13:49:24', fmt.format(date));
  369. fmt = new goog.i18n.DateTimeFormat(
  370. goog.i18n.DateTimeFormat.Format.SHORT_DATETIME);
  371. assertEquals('04.08.06, 13:49', fmt.format(date));
  372. }
  373. function testMMddyyyyHHmmssZSimpleTimeZone() {
  374. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  375. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  376. var date = new Date(Date.UTC(2006, 6, 27, 13, 10, 10));
  377. var timeZone = goog.i18n.TimeZone.createTimeZone(480);
  378. var fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss Z');
  379. assertEquals('07/27/2006 05:10:10 -0800', fmt.format(date, timeZone));
  380. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss ZZ');
  381. assertEquals('07/27/2006 05:10:10 -0800', fmt.format(date, timeZone));
  382. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss ZZZ');
  383. assertEquals('07/27/2006 05:10:10 -0800', fmt.format(date, timeZone));
  384. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss ZZZZ');
  385. assertEquals('07/27/2006 05:10:10 GMT-08:00', fmt.format(date, timeZone));
  386. }
  387. function testMMddyyyyHHmmssZCommonTimeZone() {
  388. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  389. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  390. var date = new Date(Date.UTC(2006, 6, 27, 13, 10, 10));
  391. var timeZone = goog.i18n.TimeZone.createTimeZone(americaLosAngelesData);
  392. var fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss Z');
  393. assertEquals('07/27/2006 06:10:10 -0700', fmt.format(date, timeZone));
  394. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss ZZ');
  395. assertEquals('07/27/2006 06:10:10 -0700', fmt.format(date, timeZone));
  396. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss ZZZ');
  397. assertEquals('07/27/2006 06:10:10 -0700', fmt.format(date, timeZone));
  398. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss ZZZZ');
  399. assertEquals('07/27/2006 06:10:10 GMT-07:00', fmt.format(date, timeZone));
  400. date = new Date(Date.UTC(2006, 1, 27, 13, 10, 10));
  401. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss Z');
  402. assertEquals('02/27/2006 05:10:10 -0800', fmt.format(date, timeZone));
  403. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss ZZ');
  404. assertEquals('02/27/2006 05:10:10 -0800', fmt.format(date, timeZone));
  405. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss ZZZ');
  406. assertEquals('02/27/2006 05:10:10 -0800', fmt.format(date, timeZone));
  407. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss ZZZZ');
  408. assertEquals('02/27/2006 05:10:10 GMT-08:00', fmt.format(date, timeZone));
  409. }
  410. function testMMddyyyyHHmmsszSimpleTimeZone() {
  411. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  412. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  413. var date = new Date(Date.UTC(2006, 6, 27, 13, 10, 10));
  414. var timeZone = goog.i18n.TimeZone.createTimeZone(420);
  415. var fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss z');
  416. assertEquals('07/27/2006 06:10:10 UTC-7', fmt.format(date, timeZone));
  417. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss zz');
  418. assertEquals('07/27/2006 06:10:10 UTC-7', fmt.format(date, timeZone));
  419. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss zzz');
  420. assertEquals('07/27/2006 06:10:10 UTC-7', fmt.format(date, timeZone));
  421. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss zzzz');
  422. assertEquals('07/27/2006 06:10:10 UTC-7', fmt.format(date, timeZone));
  423. }
  424. function testMMddyyyyHHmmsszCommonTimeZone() {
  425. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  426. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  427. var date = new Date(Date.UTC(2006, 6, 27, 13, 10, 10));
  428. var timeZone = goog.i18n.TimeZone.createTimeZone(americaLosAngelesData);
  429. var fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss z');
  430. assertEquals('07/27/2006 06:10:10 PDT', fmt.format(date, timeZone));
  431. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss zz');
  432. assertEquals('07/27/2006 06:10:10 PDT', fmt.format(date, timeZone));
  433. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss zzz');
  434. assertEquals('07/27/2006 06:10:10 PDT', fmt.format(date, timeZone));
  435. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss zzzz');
  436. assertEquals(
  437. '07/27/2006 06:10:10 Pacific Daylight Time', fmt.format(date, timeZone));
  438. date = new Date(Date.UTC(2006, 1, 27, 13, 10, 10));
  439. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss z');
  440. assertEquals('02/27/2006 05:10:10 PST', fmt.format(date, timeZone));
  441. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss zz');
  442. assertEquals('02/27/2006 05:10:10 PST', fmt.format(date, timeZone));
  443. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss zzz');
  444. assertEquals('02/27/2006 05:10:10 PST', fmt.format(date, timeZone));
  445. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss zzzz');
  446. assertEquals(
  447. '02/27/2006 05:10:10 Pacific Standard Time', fmt.format(date, timeZone));
  448. timeZone = goog.i18n.TimeZone.createTimeZone(europeBerlinData);
  449. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss z');
  450. assertEquals('02/27/2006 14:10:10 MEZ', fmt.format(date, timeZone));
  451. var fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss zzzz');
  452. assertEquals(
  453. '02/27/2006 14:10:10 Mitteleurop\u00e4ische Zeit',
  454. fmt.format(date, timeZone));
  455. }
  456. function testMMddyyyyHHmmssvCommonTimeZone() {
  457. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  458. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  459. var date = new Date(Date.UTC(2006, 6, 27, 13, 10, 10));
  460. var timeZone = goog.i18n.TimeZone.createTimeZone(americaLosAngelesData);
  461. var fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss v');
  462. assertEquals(
  463. '07/27/2006 06:10:10 America/Los_Angeles', fmt.format(date, timeZone));
  464. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss vv');
  465. assertEquals(
  466. '07/27/2006 06:10:10 America/Los_Angeles', fmt.format(date, timeZone));
  467. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss vvv');
  468. assertEquals(
  469. '07/27/2006 06:10:10 America/Los_Angeles', fmt.format(date, timeZone));
  470. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss vvvv');
  471. assertEquals(
  472. '07/27/2006 06:10:10 America/Los_Angeles', fmt.format(date, timeZone));
  473. }
  474. function testMMddyyyyHHmmssvSimpleTimeZone() {
  475. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  476. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  477. var date = new Date(Date.UTC(2006, 6, 27, 13, 10, 10));
  478. var timeZone = goog.i18n.TimeZone.createTimeZone(420);
  479. var fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss v');
  480. assertEquals('07/27/2006 06:10:10 Etc/GMT+7', fmt.format(date, timeZone));
  481. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss vv');
  482. assertEquals('07/27/2006 06:10:10 Etc/GMT+7', fmt.format(date, timeZone));
  483. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss vvv');
  484. assertEquals('07/27/2006 06:10:10 Etc/GMT+7', fmt.format(date, timeZone));
  485. var fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss vvvv');
  486. assertEquals('07/27/2006 06:10:10 Etc/GMT+7', fmt.format(date, timeZone));
  487. }
  488. function testMMddyyyyHHmmssVCommonTimeZone() {
  489. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  490. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  491. var date = new Date(Date.UTC(2006, 6, 27, 13, 10, 10));
  492. var timeZone = goog.i18n.TimeZone.createTimeZone(americaLosAngelesData);
  493. var fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss V');
  494. assertEquals(
  495. '07/27/2006 06:10:10 America/Los_Angeles', fmt.format(date, timeZone));
  496. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss VV');
  497. assertEquals(
  498. '07/27/2006 06:10:10 America/Los_Angeles', fmt.format(date, timeZone));
  499. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss VVV');
  500. assertEquals(
  501. '07/27/2006 06:10:10 Los Angeles Time', fmt.format(date, timeZone));
  502. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss VVVV');
  503. assertEquals(
  504. '07/27/2006 06:10:10 Los Angeles Time', fmt.format(date, timeZone));
  505. }
  506. function testMMddyyyyHHmmssVSimpleTimeZone() {
  507. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  508. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  509. var date = new Date(Date.UTC(2006, 6, 27, 13, 10, 10));
  510. var timeZone = goog.i18n.TimeZone.createTimeZone(420);
  511. var fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss V');
  512. assertEquals('07/27/2006 06:10:10 Etc/GMT+7', fmt.format(date, timeZone));
  513. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss VV');
  514. assertEquals('07/27/2006 06:10:10 Etc/GMT+7', fmt.format(date, timeZone));
  515. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss VVV');
  516. assertEquals('07/27/2006 06:10:10 GMT-07:00', fmt.format(date, timeZone));
  517. var fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss VVVV');
  518. assertEquals('07/27/2006 06:10:10 GMT-07:00', fmt.format(date, timeZone));
  519. }
  520. function test_yyyyMMddG() {
  521. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  522. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  523. var date = new Date(Date.UTC(2006, 6, 27, 20, 10, 10));
  524. var timeZone = goog.i18n.TimeZone.createTimeZone(420);
  525. var fmt = new goog.i18n.DateTimeFormat('yyyy.MM.dd G \'at\' HH:mm:ss vvvv');
  526. assertEquals(
  527. '2006.07.27 n. Chr. at 13:10:10 Etc/GMT+7', fmt.format(date, timeZone));
  528. timeZone = goog.i18n.TimeZone.createTimeZone(americaLosAngelesData);
  529. fmt = new goog.i18n.DateTimeFormat('yyyy.MM.dd G \'at\' HH:mm:ss vvvv');
  530. assertEquals(
  531. '2006.07.27 n. Chr. at 13:10:10 America/Los_Angeles',
  532. fmt.format(date, timeZone));
  533. }
  534. function test_daylightTimeTransition() {
  535. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  536. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  537. // US PST transition to PDT on 2006/4/2/ 2:00am, jump to 2006/4/2 3:00am,
  538. // That's UTC time 2006/4/2 10:00am
  539. var timeZone = goog.i18n.TimeZone.createTimeZone(americaLosAngelesData);
  540. var date = new Date(Date.UTC(2006, 4 - 1, 2, 9, 59, 0));
  541. var fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss z');
  542. assertEquals('04/02/2006 01:59:00 PST', fmt.format(date, timeZone));
  543. date = new Date(Date.UTC(2006, 4 - 1, 2, 10, 1, 0));
  544. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss z');
  545. assertEquals('04/02/2006 03:01:00 PDT', fmt.format(date, timeZone));
  546. date = new Date(Date.UTC(2006, 4 - 1, 2, 10, 0, 0));
  547. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss z');
  548. assertEquals('04/02/2006 03:00:00 PDT', fmt.format(date, timeZone));
  549. }
  550. function test_timeDisplayOnDaylighTimeTransition() {
  551. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  552. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  553. // US PST transition to PDT on 2006/4/2/ 2:00am, jump to 2006/4/2 3:00am,
  554. var date = new Date(Date.UTC(2006, 4 - 1, 2, 2, 30, 0));
  555. var timeZone = goog.i18n.TimeZone.createTimeZone(0);
  556. var fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss Z');
  557. assertEquals('04/02/2006 02:30:00 +0000', fmt.format(date, timeZone));
  558. // US PDT transition to PST on 2006/10/29/ 2:00am, jump back to PDT
  559. // 2006/4/2 1:00am,
  560. date = new Date(Date.UTC(2006, 10 - 1, 29, 1, 30, 0));
  561. fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss Z');
  562. assertEquals('10/29/2006 01:30:00 +0000', fmt.format(date, timeZone));
  563. }
  564. function testTimeDisplayOnDaylightTimeTransitionDayChange() {
  565. // NOTE: this test is a regression test only if the test browser has an OS
  566. // timezone of PST. While the test should still work in other timezones, it
  567. // does not serve as a regression test in them.
  568. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  569. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  570. // Time is 2015/11/01 3:00:01am after US PDT -> PST. 11:00:01am UTC.
  571. var date = new Date(Date.UTC(2015, 11 - 1, 1, 11, 0, 1));
  572. // Convert to GMT-12, across DST transition.
  573. // The date should also change, but does not change when subtracting 4 hours
  574. // from PST/PDT due to the extra hour from switching DST.
  575. var timeZone = goog.i18n.TimeZone.createTimeZone(12 * 60);
  576. var fmt = new goog.i18n.DateTimeFormat('yyyy/MM/dd HH:mm:ss Z');
  577. // Regression test: this once returned 2015/11/01 instead.
  578. assertEquals('2015/10/31 23:00:01 -1200', fmt.format(date, timeZone));
  579. }
  580. function test_nativeDigits_fa() {
  581. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_fa;
  582. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_fa;
  583. date = new Date(2006, 7 - 1, 27, 13, 10, 10, 250);
  584. var timeZone = goog.i18n.TimeZone.createTimeZone(420);
  585. var fmt = new goog.i18n.DateTimeFormat('y/MM/dd H:mm:ss٫SS');
  586. assertEquals('۲۰۰۶/۰۷/۲۷ ۱۳:۱۰:۱۰٫۲۵', fmt.format(date));
  587. // Make sure standardized timezone formats don't use native digits
  588. fmt = new goog.i18n.DateTimeFormat('Z');
  589. assertEquals('-0700', fmt.format(date, timeZone));
  590. }
  591. function test_nativeDigits_ar() {
  592. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_ar;
  593. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_ar;
  594. date = new Date(2006, 7 - 1, 27, 13, 10, 10, 250);
  595. var timeZone = goog.i18n.TimeZone.createTimeZone(420);
  596. var fmt = new goog.i18n.DateTimeFormat('y/MM/dd H:mm:ss٫SS');
  597. assertEquals('٢٠٠٦/٠٧/٢٧ ١٣:١٠:١٠٫٢٥', fmt.format(date));
  598. var fmt = new goog.i18n.DateTimeFormat(11);
  599. assertEquals('٢٧\u200f/٧\u200f/٢٠٠٦ ١:١٠ م', fmt.format(date));
  600. // Make sure standardized timezone formats don't use native digits
  601. fmt = new goog.i18n.DateTimeFormat('Z');
  602. assertEquals('-0700', fmt.format(date, timeZone));
  603. }
  604. function test_enforceAsciiDigits_ar() {
  605. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_ar;
  606. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_ar;
  607. goog.i18n.DateTimeFormat.setEnforceAsciiDigits(true);
  608. date = new Date(2006, 7 - 1, 27, 13, 10, 10, 250);
  609. var timeZone = goog.i18n.TimeZone.createTimeZone(420);
  610. var fmt = new goog.i18n.DateTimeFormat('y/MM/dd H:mm:ss٫SS');
  611. assertEquals('2006/07/27 13:10:10٫25', fmt.format(date));
  612. var fmt = new goog.i18n.DateTimeFormat(11);
  613. assertEquals('27/7/2006 1:10 م', fmt.format(date));
  614. // Make sure standardized timezone formats don't use native digits
  615. fmt = new goog.i18n.DateTimeFormat('Z');
  616. assertEquals('-0700', fmt.format(date, timeZone));
  617. }
  618. // Making sure that the date-time combination is not a simple concatenation
  619. function test_dateTimeConcatenation() {
  620. var date = new Date(Date.UTC(2006, 4 - 1, 2, 2, 30, 0));
  621. var timeZone = goog.i18n.TimeZone.createTimeZone(americaLosAngelesData);
  622. var fmt = new goog.i18n.DateTimeFormat(
  623. goog.i18n.DateTimeFormat.Format.FULL_DATETIME);
  624. // {1} 'at' {0}
  625. assertEquals(
  626. 'Saturday, April 1, 2006 at 6:30:00 PM Pacific Standard Time',
  627. fmt.format(date, timeZone));
  628. fmt = new goog.i18n.DateTimeFormat(
  629. goog.i18n.DateTimeFormat.Format.LONG_DATETIME);
  630. assertEquals('April 1, 2006 at 6:30:00 PM PST', fmt.format(date, timeZone));
  631. // {1}, {0}
  632. fmt = new goog.i18n.DateTimeFormat(
  633. goog.i18n.DateTimeFormat.Format.MEDIUM_DATETIME);
  634. assertEquals('Apr 1, 2006, 6:30:00 PM', fmt.format(date, timeZone));
  635. fmt = new goog.i18n.DateTimeFormat(
  636. goog.i18n.DateTimeFormat.Format.SHORT_DATETIME);
  637. assertEquals('4/1/06, 6:30 PM', fmt.format(date, timeZone));
  638. }
  639. function testNotUsingGlobalSymbols() {
  640. date = new Date(2013, 10, 15);
  641. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_fr;
  642. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_fr;
  643. var fmtFr =
  644. new goog.i18n.DateTimeFormat(goog.i18n.DateTimeFormat.Format.FULL_DATE);
  645. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  646. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  647. var fmtDe =
  648. new goog.i18n.DateTimeFormat(goog.i18n.DateTimeFormat.Format.FULL_DATE);
  649. // The two formatters should return different results (French & German)
  650. assertEquals('vendredi 15 novembre 2013', fmtFr.format(date));
  651. assertEquals('Freitag, 15. November 2013', fmtDe.format(date));
  652. }
  653. function testConstructorSymbols() {
  654. date = new Date(2013, 10, 15);
  655. var fmtFr = new goog.i18n.DateTimeFormat(
  656. goog.i18n.DateTimeFormat.Format.FULL_DATE, goog.i18n.DateTimeSymbols_fr);
  657. var fmtDe = new goog.i18n.DateTimeFormat(
  658. goog.i18n.DateTimeFormat.Format.FULL_DATE, goog.i18n.DateTimeSymbols_de);
  659. // The two formatters should return different results (French & German)
  660. assertEquals('vendredi 15 novembre 2013', fmtFr.format(date));
  661. assertEquals('Freitag, 15. November 2013', fmtDe.format(date));
  662. }
  663. function testQuotedPattern() {
  664. // Regression test for b/29990921.
  665. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_en;
  666. date = new Date(2013, 10, 15);
  667. // Literal apostrophe
  668. var fmt = new goog.i18n.DateTimeFormat('MMM \'\'yy');
  669. assertEquals('Nov \'13', fmt.format(date));
  670. // Quoted text
  671. fmt = new goog.i18n.DateTimeFormat('MMM dd\'th\' yyyy');
  672. assertEquals('Nov 15th 2013', fmt.format(date));
  673. // Quoted text (only opening apostrophe)
  674. fmt = new goog.i18n.DateTimeFormat('MMM dd\'th yyyy');
  675. assertEquals('Nov 15th yyyy', fmt.format(date));
  676. // Quoted text with literal apostrophe
  677. fmt = new goog.i18n.DateTimeFormat('MMM dd\'th\'\'\'');
  678. assertEquals('Nov 15th\'', fmt.format(date));
  679. // Quoted text with literal apostrophe (only opening apostrophe)
  680. fmt = new goog.i18n.DateTimeFormat('MMM dd\'th\'\'');
  681. assertEquals('Nov 15th\'', fmt.format(date));
  682. }
  683. function testSupportForWeekInYear() {
  684. var date = new Date(2013, 1, 25);
  685. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_fr;
  686. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_fr;
  687. var fmt = new goog.i18n.DateTimeFormat('\'week\' w');
  688. assertEquals('week 9', fmt.format(date));
  689. var fmt = new goog.i18n.DateTimeFormat('\'week\' ww');
  690. assertEquals('week 09', fmt.format(date));
  691. // Make sure it uses native digits when needed
  692. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_fa;
  693. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_fa;
  694. var fmt = new goog.i18n.DateTimeFormat('\'week\' w');
  695. assertEquals('week ۹', fmt.format(date));
  696. var fmt = new goog.i18n.DateTimeFormat('\'week\' ww');
  697. assertEquals('week ۰۹', fmt.format(date));
  698. }
  699. function testSupportForYearAndEra() {
  700. var date = new Date(2013, 1, 25);
  701. var fmt = new goog.i18n.DateTimeFormat(
  702. goog.i18n.DateTimePatterns.YEAR_FULL_WITH_ERA);
  703. assertEquals('2013 AD', fmt.format(date));
  704. date.setFullYear(213);
  705. assertEquals('213 AD', fmt.format(date));
  706. date.setFullYear(11);
  707. assertEquals('11 AD', fmt.format(date));
  708. date.setFullYear(-213);
  709. assertEquals('213 BC', fmt.format(date));
  710. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
  711. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
  712. fmt = new goog.i18n.DateTimeFormat(
  713. goog.i18n.DateTimePatterns.YEAR_FULL_WITH_ERA);
  714. date.setFullYear(2013);
  715. assertEquals('2013 n. Chr.', fmt.format(date));
  716. date.setFullYear(213);
  717. assertEquals('213 n. Chr.', fmt.format(date));
  718. date.setFullYear(11);
  719. assertEquals('11 n. Chr.', fmt.format(date));
  720. date.setFullYear(-213);
  721. assertEquals('213 v. Chr.', fmt.format(date));
  722. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_ja;
  723. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_ja;
  724. fmt = new goog.i18n.DateTimeFormat(
  725. goog.i18n.DateTimePatterns.YEAR_FULL_WITH_ERA);
  726. date.setFullYear(2013);
  727. assertEquals('西暦2013年', fmt.format(date));
  728. date.setFullYear(213);
  729. assertEquals('西暦213年', fmt.format(date));
  730. date.setFullYear(11);
  731. assertEquals('西暦11年', fmt.format(date));
  732. date.setFullYear(-213);
  733. assertEquals('紀元前213年', fmt.format(date));
  734. }
  735. // Creates a string by concatenating the week number for 7 successive days
  736. function weekInYearFor7Days() {
  737. var date = new Date(2013, 0, 1); // January
  738. var fmt = new goog.i18n.DateTimeFormat('w');
  739. var result = '';
  740. for (var i = 1; i <= 7; ++i) {
  741. date.setDate(i);
  742. result += fmt.format(date);
  743. }
  744. return result;
  745. }
  746. // Expected results from ICU4J v51. One entry will change in v52.
  747. // These cover all combinations of FIRSTDAYOFWEEK / FIRSTWEEKCUTOFFDAY in use.
  748. function testWeekInYearI18n() {
  749. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_bn_BD;
  750. assertEquals('bn_BD', '১১১২২২২', weekInYearFor7Days());
  751. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_en_IE;
  752. assertEquals('en_IE', '1111122', weekInYearFor7Days());
  753. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_fr_DJ;
  754. assertEquals('fr_DJ', '1111222', weekInYearFor7Days());
  755. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_he_IL;
  756. assertEquals('he_IL', '1111122', weekInYearFor7Days());
  757. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_ar_SA;
  758. assertEquals('ar_SA', '١١١١١٢٢', weekInYearFor7Days());
  759. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_ar_AE;
  760. assertEquals('ar_AE', '١١١١٢٢٢', weekInYearFor7Days());
  761. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_en_IN;
  762. assertEquals('en_IN', '1111122', weekInYearFor7Days());
  763. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_en_GB;
  764. assertEquals('en_GB', '1111112', weekInYearFor7Days());
  765. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_en_US;
  766. assertEquals('en_US', '1111122', weekInYearFor7Days());
  767. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_ro_RO;
  768. assertEquals('ro_RO', '1111112', weekInYearFor7Days());
  769. }
  770. // Regression for b/11567443 (no method 'getHours' when formatting a
  771. // goog.date.Date)
  772. function test_variousDateTypes() {
  773. goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_fr;
  774. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_fr;
  775. var fmt = new goog.i18n.DateTimeFormat(
  776. goog.i18n.DateTimeFormat.Format.MEDIUM_DATETIME);
  777. var date = new Date(2006, 6, 27, 13, 10, 42, 250);
  778. assertEquals('27 juil. 2006 à 13:10:42', fmt.format(date));
  779. var gdatetime = new goog.date.DateTime(2006, 6, 27, 13, 10, 42, 250);
  780. assertEquals('27 juil. 2006 à 13:10:42', fmt.format(gdatetime));
  781. var gdate = new goog.date.Date(2006, 6, 27);
  782. var fmtDate =
  783. new goog.i18n.DateTimeFormat(goog.i18n.DateTimeFormat.Format.MEDIUM_DATE);
  784. assertEquals('27 juil. 2006', fmtDate.format(gdatetime));
  785. try {
  786. fmt.format(gdate);
  787. fail('Should have thrown exception.');
  788. } catch (e) {
  789. }
  790. }
  791. function testExceptionWhenFormattingNull() {
  792. var fmt = new goog.i18n.DateTimeFormat('M/d/y');
  793. try {
  794. fmt.format(null);
  795. fail('Should have thrown exception.');
  796. } catch (e) {
  797. }
  798. }