datetimeparse_test.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. // Copyright 2006 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.DateTimeParseTest');
  15. goog.setTestOnly('goog.i18n.DateTimeParseTest');
  16. goog.require('goog.date.Date');
  17. goog.require('goog.i18n.DateTimeFormat');
  18. goog.require('goog.i18n.DateTimeParse');
  19. goog.require('goog.i18n.DateTimeSymbols');
  20. goog.require('goog.i18n.DateTimeSymbols_en');
  21. goog.require('goog.i18n.DateTimeSymbols_fa');
  22. goog.require('goog.i18n.DateTimeSymbols_fr');
  23. goog.require('goog.i18n.DateTimeSymbols_pl');
  24. goog.require('goog.i18n.DateTimeSymbols_zh');
  25. goog.require('goog.testing.ExpectedFailures');
  26. goog.require('goog.testing.jsunit');
  27. goog.require('goog.userAgent');
  28. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_en;
  29. var expectedFailures;
  30. function setUpPage() {
  31. expectedFailures = new goog.testing.ExpectedFailures();
  32. }
  33. function tearDown() {
  34. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_en;
  35. expectedFailures.handleTearDown();
  36. }
  37. // Helper equivalent of assertEquals for dates, with day of month optional
  38. function assertDateEquals(expectYear, expectMonth, expectDate, date) {
  39. assertEquals(expectYear, date.getFullYear());
  40. assertEquals(expectMonth, date.getMonth());
  41. if (expectDate) assertEquals(expectDate, date.getDate());
  42. }
  43. // Helper equivalent of assertEquals for times, with seconds and milliseconds
  44. function assertTimeEquals(expectHour, expectMin, expectSec, expectMilli, date) {
  45. assertEquals(expectHour, date.getHours());
  46. assertEquals(expectMin, date.getMinutes());
  47. if (expectSec) assertEquals(expectSec, date.getSeconds());
  48. if (expectMilli) assertEquals(expectMilli, date.getTime() % 1000);
  49. }
  50. // Helper function, doing parse and assert on dates
  51. function assertParsedDateEquals(
  52. expectYear, expectMonth, expectDate, parser, stringToParse, date) {
  53. assertTrue(parser.parse(stringToParse, date) > 0);
  54. assertDateEquals(expectYear, expectMonth, expectDate, date);
  55. }
  56. // Helper function, doing parse and assert on times
  57. function assertParsedTimeEquals(
  58. expectHour, expectMin, expectSec, expectMilli, parser, stringToParse,
  59. date) {
  60. assertTrue(parser.parse(stringToParse, date) > 0);
  61. assertTimeEquals(expectHour, expectMin, expectSec, expectMilli, date);
  62. }
  63. function testNegativeYear() {
  64. var date = new Date();
  65. var parser = new goog.i18n.DateTimeParse('MM/dd, yyyy');
  66. assertParsedDateEquals(1999, 11 - 1, 22, parser, '11/22, 1999', date);
  67. assertParsedDateEquals(-1999, 11 - 1, 22, parser, '11/22, -1999', date);
  68. }
  69. function testEra() {
  70. // Bug 2350397
  71. if (goog.userAgent.WEBKIT) {
  72. // Bug 2350397 Test seems to be very flaky on Chrome. Disabling it
  73. return;
  74. }
  75. var date = new Date();
  76. var parser = new goog.i18n.DateTimeParse('MM/dd, yyyyG');
  77. assertParsedDateEquals(-1998, 11 - 1, 22, parser, '11/22, 1999BC', date);
  78. assertParsedDateEquals(0, 11 - 1, 22, parser, '11/22, 1BC', date);
  79. assertParsedDateEquals(1999, 11 - 1, 22, parser, '11/22, 1999AD', date);
  80. }
  81. function testFractionalSeconds() {
  82. var date = new Date();
  83. var parser = new goog.i18n.DateTimeParse('hh:mm:ss.SSS');
  84. assertParsedTimeEquals(11, 12, 13, 956, parser, '11:12:13.956', date);
  85. assertParsedTimeEquals(11, 12, 13, 950, parser, '11:12:13.95', date);
  86. assertParsedTimeEquals(11, 12, 13, 900, parser, '11:12:13.9', date);
  87. }
  88. function testAmbiguousYear() {
  89. // assume this year is 2006, year 27 to 99 will be interpret as 1927 to 1999
  90. // year 00 to 25 will be 2000 to 2025. Year 26 can be either 1926 or 2026
  91. // depend on the time being parsed and the time when this program runs.
  92. // For example, if the program is run at 2006/03/03 12:12:12, the following
  93. // code should work.
  94. // assertTrue(parser.parse('01/01/26 00:00:00:001', date) > 0);
  95. // assertTrue(date.getFullYear() == 2026 - 1900);
  96. // assertTrue(parser.parse('12/30/26 23:59:59:999', date) > 0);
  97. // assertTrue(date.getFullYear() == 1926 - 1900);
  98. // Since this test can run in any time, some logic needed here.
  99. var futureDate = new Date();
  100. futureDate.setFullYear(
  101. futureDate.getFullYear() + 100 -
  102. goog.i18n.DateTimeParse.ambiguousYearCenturyStart);
  103. var ambiguousYear = futureDate.getFullYear() % 100;
  104. var parser = new goog.i18n.DateTimeParse('MM/dd/yy HH:mm:ss:SSS');
  105. var date = new Date();
  106. var str = '01/01/' + ambiguousYear + ' 00:00:00:001';
  107. assertTrue(parser.parse(str, date) > 0);
  108. assertEquals(futureDate.getFullYear(), date.getFullYear());
  109. str = '12/31/' + ambiguousYear + ' 23:59:59:999';
  110. assertTrue(parser.parse(str, date) > 0);
  111. assertEquals(futureDate.getFullYear(), date.getFullYear() + 100);
  112. // Test the ability to move the disambiguation century
  113. goog.i18n.DateTimeParse.ambiguousYearCenturyStart = 60;
  114. futureDate = new Date();
  115. futureDate.setFullYear(
  116. futureDate.getFullYear() + 100 -
  117. goog.i18n.DateTimeParse.ambiguousYearCenturyStart);
  118. ambiguousYear = futureDate.getFullYear() % 100;
  119. var str = '01/01/' + ambiguousYear + ' 00:00:00:001';
  120. assertTrue(parser.parse(str, date) > 0);
  121. assertEquals(futureDate.getFullYear(), date.getFullYear());
  122. str = '12/31/' + ambiguousYear + ' 23:59:59:999';
  123. assertTrue(parser.parse(str, date) > 0);
  124. assertEquals(futureDate.getFullYear(), date.getFullYear() + 100);
  125. // Reset parameter for other test cases
  126. goog.i18n.DateTimeParse.ambiguousYearCenturyStart = 80;
  127. }
  128. function testLeapYear() {
  129. var date = new Date();
  130. var parser = new goog.i18n.DateTimeParse('MMdd, yyyy');
  131. assertParsedDateEquals(2001, 3 - 1, 1, parser, '0229, 2001', date);
  132. assertParsedDateEquals(2000, 2 - 1, 29, parser, '0229, 2000', date);
  133. }
  134. function testAbutField() {
  135. var date = new Date();
  136. var parser = new goog.i18n.DateTimeParse('hhmm');
  137. assertParsedTimeEquals(11, 22, undefined, undefined, parser, '1122', date);
  138. assertParsedTimeEquals(1, 22, undefined, undefined, parser, '122', date);
  139. var parser2 = new goog.i18n.DateTimeParse('hhmmss');
  140. assertParsedTimeEquals(11, 22, 33, undefined, parser2, '112233', date);
  141. assertParsedTimeEquals(1, 22, 33, undefined, parser2, '12233', date);
  142. var parser3 = new goog.i18n.DateTimeParse('yyyyMMdd');
  143. assertParsedDateEquals(1999, 12 - 1, 2, parser3, '19991202', date);
  144. assertParsedDateEquals(999, 12 - 1, 2, parser3, '9991202', date);
  145. assertParsedDateEquals(99, 12 - 1, 2, parser3, '991202', date);
  146. assertParsedDateEquals(9, 12 - 1, 2, parser3, '91202', date);
  147. }
  148. function testYearParsing() {
  149. var date = new Date();
  150. var parser = new goog.i18n.DateTimeParse('yyMMdd');
  151. assertParsedDateEquals(1999, 12 - 1, 2, parser, '991202', date);
  152. var parser2 = new goog.i18n.DateTimeParse('yyyyMMdd');
  153. assertParsedDateEquals(2005, 12 - 1, 2, parser2, '20051202', date);
  154. }
  155. function testGoogDateParsing() {
  156. var date = new goog.date.Date();
  157. var parser = new goog.i18n.DateTimeParse('yyMMdd');
  158. assertParsedDateEquals(1999, 12 - 1, 2, parser, '991202', date);
  159. }
  160. function testHourParsing_hh() {
  161. var date = new Date();
  162. var parser = new goog.i18n.DateTimeParse('hhmm');
  163. assertParsedTimeEquals(0, 22, undefined, undefined, parser, '0022', date);
  164. assertParsedTimeEquals(11, 22, undefined, undefined, parser, '1122', date);
  165. assertParsedTimeEquals(0, 22, undefined, undefined, parser, '1222', date);
  166. assertParsedTimeEquals(23, 22, undefined, undefined, parser, '2322', date);
  167. assertParsedTimeEquals(0, 22, undefined, undefined, parser, '2422', date);
  168. var parser2 = new goog.i18n.DateTimeParse('hhmma');
  169. assertParsedTimeEquals(0, 22, undefined, undefined, parser2, '0022am', date);
  170. assertParsedTimeEquals(11, 22, undefined, undefined, parser2, '1122am', date);
  171. assertParsedTimeEquals(0, 22, undefined, undefined, parser2, '1222am', date);
  172. assertParsedTimeEquals(23, 22, undefined, undefined, parser2, '2322am', date);
  173. assertParsedTimeEquals(0, 22, undefined, undefined, parser2, '2422am', date);
  174. assertParsedTimeEquals(12, 22, undefined, undefined, parser2, '0022pm', date);
  175. assertParsedTimeEquals(23, 22, undefined, undefined, parser2, '1122pm', date);
  176. assertParsedTimeEquals(12, 22, undefined, undefined, parser2, '1222pm', date);
  177. assertParsedTimeEquals(23, 22, undefined, undefined, parser2, '2322pm', date);
  178. assertParsedTimeEquals(0, 22, undefined, undefined, parser2, '2422pm', date);
  179. }
  180. function testHourParsing_KK() {
  181. var date = new Date();
  182. var parser = new goog.i18n.DateTimeParse('KKmm');
  183. assertParsedTimeEquals(0, 22, undefined, undefined, parser, '0022', date);
  184. assertParsedTimeEquals(11, 22, undefined, undefined, parser, '1122', date);
  185. assertParsedTimeEquals(12, 22, undefined, undefined, parser, '1222', date);
  186. assertParsedTimeEquals(23, 22, undefined, undefined, parser, '2322', date);
  187. assertParsedTimeEquals(0, 22, undefined, undefined, parser, '2422', date);
  188. var parser2 = new goog.i18n.DateTimeParse('KKmma');
  189. assertParsedTimeEquals(0, 22, undefined, undefined, parser2, '0022am', date);
  190. assertParsedTimeEquals(11, 22, undefined, undefined, parser2, '1122am', date);
  191. assertParsedTimeEquals(12, 22, undefined, undefined, parser2, '1222am', date);
  192. assertParsedTimeEquals(23, 22, undefined, undefined, parser2, '2322am', date);
  193. assertParsedTimeEquals(0, 22, undefined, undefined, parser2, '2422am', date);
  194. assertParsedTimeEquals(12, 22, undefined, undefined, parser2, '0022pm', date);
  195. assertParsedTimeEquals(23, 22, undefined, undefined, parser2, '1122pm', date);
  196. assertParsedTimeEquals(12, 22, undefined, undefined, parser2, '1222pm', date);
  197. assertParsedTimeEquals(23, 22, undefined, undefined, parser2, '2322pm', date);
  198. assertParsedTimeEquals(0, 22, undefined, undefined, parser2, '2422pm', date);
  199. }
  200. function testHourParsing_kk() {
  201. var date = new Date();
  202. var parser = new goog.i18n.DateTimeParse('kkmm');
  203. assertParsedTimeEquals(0, 22, undefined, undefined, parser, '0022', date);
  204. assertParsedTimeEquals(11, 22, undefined, undefined, parser, '1122', date);
  205. assertParsedTimeEquals(12, 22, undefined, undefined, parser, '1222', date);
  206. assertParsedTimeEquals(23, 22, undefined, undefined, parser, '2322', date);
  207. assertParsedTimeEquals(0, 22, undefined, undefined, parser, '2422', date);
  208. var parser2 = new goog.i18n.DateTimeParse('kkmma');
  209. assertParsedTimeEquals(0, 22, undefined, undefined, parser2, '0022am', date);
  210. assertParsedTimeEquals(11, 22, undefined, undefined, parser2, '1122am', date);
  211. assertParsedTimeEquals(12, 22, undefined, undefined, parser2, '1222am', date);
  212. assertParsedTimeEquals(23, 22, undefined, undefined, parser2, '2322am', date);
  213. assertParsedTimeEquals(0, 22, undefined, undefined, parser2, '2422am', date);
  214. assertParsedTimeEquals(12, 22, undefined, undefined, parser2, '0022pm', date);
  215. assertParsedTimeEquals(23, 22, undefined, undefined, parser2, '1122pm', date);
  216. assertParsedTimeEquals(12, 22, undefined, undefined, parser2, '1222pm', date);
  217. assertParsedTimeEquals(23, 22, undefined, undefined, parser2, '2322pm', date);
  218. assertParsedTimeEquals(0, 22, undefined, undefined, parser2, '2422pm', date);
  219. }
  220. function testHourParsing_HH() {
  221. var date = new Date();
  222. var parser = new goog.i18n.DateTimeParse('HHmm');
  223. assertParsedTimeEquals(0, 22, undefined, undefined, parser, '0022', date);
  224. assertParsedTimeEquals(11, 22, undefined, undefined, parser, '1122', date);
  225. assertParsedTimeEquals(12, 22, undefined, undefined, parser, '1222', date);
  226. assertParsedTimeEquals(23, 22, undefined, undefined, parser, '2322', date);
  227. assertParsedTimeEquals(0, 22, undefined, undefined, parser, '2422', date);
  228. var parser2 = new goog.i18n.DateTimeParse('HHmma');
  229. assertParsedTimeEquals(0, 22, undefined, undefined, parser2, '0022am', date);
  230. assertParsedTimeEquals(11, 22, undefined, undefined, parser2, '1122am', date);
  231. assertParsedTimeEquals(12, 22, undefined, undefined, parser2, '1222am', date);
  232. assertParsedTimeEquals(23, 22, undefined, undefined, parser2, '2322am', date);
  233. assertParsedTimeEquals(0, 22, undefined, undefined, parser2, '2422am', date);
  234. assertParsedTimeEquals(12, 22, undefined, undefined, parser2, '0022pm', date);
  235. assertParsedTimeEquals(23, 22, undefined, undefined, parser2, '1122pm', date);
  236. assertParsedTimeEquals(12, 22, undefined, undefined, parser2, '1222pm', date);
  237. assertParsedTimeEquals(23, 22, undefined, undefined, parser2, '2322pm', date);
  238. assertParsedTimeEquals(0, 22, undefined, undefined, parser2, '2422pm', date);
  239. }
  240. function testEnglishDate() {
  241. var date = new Date();
  242. var parser = new goog.i18n.DateTimeParse('yyyy MMM dd hh:mm');
  243. // Fails in Safari4/Chrome Winxp because of infrastructure issues, temporarily
  244. // disabled. See b/4274778.
  245. expectedFailures.expectFailureFor(goog.userAgent.WEBKIT);
  246. try {
  247. assertParsedDateEquals(2006, 7 - 1, 10, parser, '2006 Jul 10 15:44', date);
  248. assertTimeEquals(15, 44, undefined, undefined, date);
  249. } catch (e) {
  250. expectedFailures.handleException(e);
  251. }
  252. }
  253. function testChineseDate() {
  254. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_zh;
  255. // Javascript month start from 0, July is 7 - 1
  256. var date = new Date(2006, 7 - 1, 24, 12, 12, 12, 0);
  257. var formatter =
  258. new goog.i18n.DateTimeFormat(goog.i18n.DateTimeFormat.Format.FULL_DATE);
  259. var dateStr = formatter.format(date);
  260. var parser =
  261. new goog.i18n.DateTimeParse(goog.i18n.DateTimeFormat.Format.FULL_DATE);
  262. assertParsedDateEquals(2006, 7 - 1, 24, parser, dateStr, date);
  263. parser =
  264. new goog.i18n.DateTimeParse(goog.i18n.DateTimeFormat.Format.LONG_DATE);
  265. assertParsedDateEquals(
  266. 2006, 7 - 1, 24, parser, '2006\u5E747\u670824\u65E5', date);
  267. parser =
  268. new goog.i18n.DateTimeParse(goog.i18n.DateTimeFormat.Format.FULL_TIME);
  269. assertTrue(parser.parse('GMT-07:00 \u4E0B\u534803:26:28', date) > 0);
  270. // Fails in Safari4/Chrome Winxp because of infrastructure issues, temporarily
  271. // disabled. See b/4274778.
  272. expectedFailures.expectFailureFor(goog.userAgent.WEBKIT);
  273. try {
  274. assertEquals(
  275. 22, (24 + date.getHours() + date.getTimezoneOffset() / 60) % 24);
  276. assertEquals(26, date.getMinutes());
  277. assertEquals(28, date.getSeconds());
  278. } catch (e) {
  279. expectedFailures.handleException(e);
  280. }
  281. }
  282. // For languages with goog.i18n.DateTimeSymbols.ZERODIGIT defined, the int
  283. // digits are localized by the locale in datetimeformat.js. This test case is
  284. // for parsing dates with such native digits.
  285. function testDatesWithNativeDigits() {
  286. // Language Arabic is one example with
  287. // goog.i18n.DateTimeSymbols.ZERODIGIT defined.
  288. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_fa;
  289. // Javascript month starts from 0, July is 7 - 1
  290. var date = new Date(2006, 7 - 1, 24, 12, 12, 12, 0);
  291. var formatter =
  292. new goog.i18n.DateTimeFormat(goog.i18n.DateTimeFormat.Format.FULL_DATE);
  293. dateStr = formatter.format(date);
  294. var parser =
  295. new goog.i18n.DateTimeParse(goog.i18n.DateTimeFormat.Format.FULL_DATE);
  296. assertParsedDateEquals(2006, 7 - 1, 24, parser, dateStr, date);
  297. date = new Date(2006, 7 - 1, 24);
  298. formatter =
  299. new goog.i18n.DateTimeFormat(goog.i18n.DateTimeFormat.Format.SHORT_DATE);
  300. dateStr = formatter.format(date);
  301. parser =
  302. new goog.i18n.DateTimeParse(goog.i18n.DateTimeFormat.Format.SHORT_DATE);
  303. assertParsedDateEquals(2006, 7 - 1, 24, parser, dateStr, date);
  304. date = new Date();
  305. parser = new goog.i18n.DateTimeParse('y/MM/dd H:mm:ss٫SS');
  306. assertParsedDateEquals(2006, 6, 27, parser, '۲۰۰۶/۰۷/۲۷ ۱۳:۱۰:۱۰٫۲۵', date);
  307. }
  308. function testTimeZone() {
  309. var date = new Date();
  310. var parser = new goog.i18n.DateTimeParse('MM/dd/yyyy, hh:mm:ss zzz');
  311. assertTrue(parser.parse('07/21/2003, 11:22:33 GMT-0700', date) > 0);
  312. var hourGmtMinus07 = date.getHours();
  313. assertTrue(parser.parse('07/21/2003, 11:22:33 GMT-0600', date) > 0);
  314. var hourGmtMinus06 = date.getHours();
  315. assertEquals(1, (hourGmtMinus07 + 24 - hourGmtMinus06) % 24);
  316. assertTrue(parser.parse('07/21/2003, 11:22:33 GMT-0800', date) > 0);
  317. var hourGmtMinus08 = date.getHours();
  318. assertEquals(1, (hourGmtMinus08 + 24 - hourGmtMinus07) % 24);
  319. assertTrue(parser.parse('07/21/2003, 23:22:33 GMT-0800', date) > 0);
  320. assertEquals((date.getHours() + 24 - hourGmtMinus07) % 24, 13);
  321. assertTrue(parser.parse('07/21/2003, 11:22:33 GMT+0800', date) > 0);
  322. var hourGmt08 = date.getHours();
  323. assertEquals(16, (hourGmtMinus08 + 24 - hourGmt08) % 24);
  324. assertTrue(parser.parse('07/21/2003, 11:22:33 GMT0800', date) > 0);
  325. assertEquals(hourGmt08, date.getHours());
  326. // 'foo' is not a timezone
  327. assertFalse(parser.parse('07/21/2003, 11:22:33 foo', date) > 0);
  328. }
  329. function testWeekDay() {
  330. var date = new Date();
  331. var parser = new goog.i18n.DateTimeParse('EEEE, MM/dd/yyyy');
  332. assertTrue(parser.parse('Wednesday, 08/16/2006', date) > 0);
  333. assertDateEquals(2006, 8 - 1, 16, date);
  334. assertTrue(parser.parse('Tuesday, 08/16/2006', date) == 0);
  335. assertTrue(parser.parse('Thursday, 08/16/2006', date) == 0);
  336. assertTrue(parser.parse('Wed, 08/16/2006', date) > 0);
  337. assertTrue(parser.parse('Wasdfed, 08/16/2006', date) == 0);
  338. date.setDate(25);
  339. parser = new goog.i18n.DateTimeParse('EEEE, MM/yyyy');
  340. assertTrue(parser.parse('Wed, 09/2006', date) > 0);
  341. assertEquals(27, date.getDate());
  342. date.setDate(30);
  343. assertTrue(parser.parse('Wed, 09/2006', date) > 0);
  344. assertEquals(27, date.getDate());
  345. date.setDate(30);
  346. assertTrue(parser.parse('Mon, 09/2006', date) > 0);
  347. assertEquals(25, date.getDate());
  348. }
  349. function testStrictParse() {
  350. var date = new Date();
  351. var parser = new goog.i18n.DateTimeParse('yyyy/MM/dd');
  352. assertTrue(parser.strictParse('2000/13/10', date) == 0);
  353. assertTrue(parser.strictParse('2000/13/40', date) == 0);
  354. assertTrue(parser.strictParse('2000/11/10', date) > 0);
  355. assertDateEquals(2000, 11 - 1, 10, date);
  356. parser = new goog.i18n.DateTimeParse('yy/MM/dd');
  357. assertTrue(parser.strictParse('00/11/10', date) > 0);
  358. assertTrue(parser.strictParse('99/11/10', date) > 0);
  359. assertTrue(parser.strictParse('00/13/10', date) == 0);
  360. assertTrue(parser.strictParse('00/11/32', date) == 0);
  361. assertTrue(parser.strictParse('1900/11/2', date) > 0);
  362. parser = new goog.i18n.DateTimeParse('hh:mm');
  363. assertTrue(parser.strictParse('15:44', date) > 0);
  364. assertTrue(parser.strictParse('25:44', date) == 0);
  365. assertTrue(parser.strictParse('15:64', date) == 0);
  366. // leap year
  367. parser = new goog.i18n.DateTimeParse('yy/MM/dd');
  368. assertTrue(parser.strictParse('00/02/29', date) > 0);
  369. assertTrue(parser.strictParse('01/02/29', date) == 0);
  370. }
  371. function testPartialParses() {
  372. var date = new Date(0);
  373. var parser = new goog.i18n.DateTimeParse('h:mma');
  374. assertTrue(parser.parse('5:', date) > 0);
  375. assertEquals(5, date.getHours());
  376. assertEquals(0, date.getMinutes());
  377. date = new Date(0);
  378. assertTrue(parser.parse('5:44pm', date) > 0);
  379. assertEquals(17, date.getHours());
  380. assertEquals(44, date.getMinutes());
  381. date = new Date(0);
  382. assertTrue(parser.parse('5:44ym', date) > 0);
  383. assertEquals(5, date.getHours());
  384. assertEquals(44, date.getMinutes());
  385. parser = new goog.i18n.DateTimeParse('mm:ss');
  386. date = new Date(0);
  387. assertTrue(parser.parse('15:', date) > 0);
  388. assertEquals(15, date.getMinutes());
  389. assertEquals(0, date.getSeconds());
  390. }
  391. function testEnglishQuarter() {
  392. var date = new Date();
  393. var parser = new goog.i18n.DateTimeParse('QQQQ yyyy');
  394. // Fails in Safari4/Chrome Winxp because of infrastructure issues, temporarily
  395. // disabled. See b/4274778.
  396. expectedFailures.expectFailureFor(goog.userAgent.WEBKIT);
  397. try {
  398. assertParsedDateEquals(2009, 0, 1, parser, '1st quarter 2009', date);
  399. } catch (e) {
  400. expectedFailures.handleException(e);
  401. }
  402. }
  403. function testEnglishShortQuarter() {
  404. var date = new Date();
  405. var parser = new goog.i18n.DateTimeParse('yyyyQQ');
  406. // Fails in Safari4/Chrome Winxp because of infrastructure issues, temporarily
  407. // disabled. See b/4274778.
  408. expectedFailures.expectFailureFor(goog.userAgent.WEBKIT);
  409. try {
  410. assertParsedDateEquals(2006, 4 - 1, 1, parser, '2006Q2', date);
  411. } catch (e) {
  412. expectedFailures.handleException(e);
  413. }
  414. }
  415. function testFrenchShortQuarter() {
  416. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_fr;
  417. var date = new Date();
  418. var parser = new goog.i18n.DateTimeParse('yyyyQQ');
  419. assertParsedDateEquals(2009, 7 - 1, 1, parser, '2009T3', date);
  420. }
  421. function testDateTime() {
  422. var dateOrg = new Date(2006, 7 - 1, 24, 17, 21, 42, 0);
  423. var formatter = new goog.i18n.DateTimeFormat(
  424. goog.i18n.DateTimeFormat.Format.MEDIUM_DATETIME);
  425. dateStr = formatter.format(dateOrg);
  426. var parser = new goog.i18n.DateTimeParse(
  427. goog.i18n.DateTimeFormat.Format.MEDIUM_DATETIME);
  428. var dateParsed = new Date();
  429. assertParsedDateEquals(
  430. dateOrg.getFullYear(), dateOrg.getMonth(), dateOrg.getDate(), parser,
  431. dateStr, dateParsed);
  432. assertTimeEquals(
  433. dateOrg.getHours(), dateOrg.getMinutes(), dateOrg.getSeconds(), undefined,
  434. dateParsed);
  435. }
  436. /** @bug 10075434 */
  437. function testParseDateWithOverflow() {
  438. // We force the initial day of month to 30 so that it will always cause an
  439. // overflow in February, no matter if it is a leap year or not.
  440. var dateOrg = new Date(2006, 7 - 1, 30, 17, 21, 42, 0);
  441. var dateParsed; // this will receive the result of the parsing
  442. var parserMonthYear = new goog.i18n.DateTimeParse('MMMM yyyy');
  443. // The API can be a bit confusing, as this date is both input and output.
  444. // Benefit: fields that don't come from parsing are preserved.
  445. // In the typical use case, dateParsed = new Date()
  446. // and when you parse "February 3" the year is implied as "this year"
  447. // This works as intended.
  448. // But because of this we will initialize dateParsed from dateOrg
  449. // before every test (because the previous test changes it).
  450. dateParsed = new Date(dateOrg.getTime());
  451. // if preserved February 30 overflows, so we get the closest February day, 28
  452. assertParsedDateEquals(
  453. 2013, 2 - 1, 28, parserMonthYear, 'February 2013', dateParsed);
  454. // Same as above, but the last February date is 29 (leap year)
  455. dateParsed = new Date(dateOrg.getTime());
  456. assertParsedDateEquals(
  457. 2012, 2 - 1, 29, parserMonthYear, 'February 2012', dateParsed);
  458. // Same as above, but no overflow (Match has 31 days, the parsed 30 is OK)
  459. dateParsed = new Date(dateOrg.getTime());
  460. assertParsedDateEquals(
  461. 2013, 3 - 1, 30, parserMonthYear, 'March 2013', dateParsed);
  462. // The pattern does not expect the day of month, so 12 is interpreted
  463. // as year, 12. May be weird, but this is the original behavior.
  464. // The overflow for leap year applies, same as above.
  465. dateParsed = new Date(dateOrg.getTime());
  466. assertParsedDateEquals(
  467. 12, 2 - 1, 29, parserMonthYear, 'February 12, 2013', dateParsed);
  468. // We make sure that the fix did not break parsing with day of month present
  469. var parserMonthDayYear = new goog.i18n.DateTimeParse('MMMM d, yyyy');
  470. dateParsed = new Date(dateOrg.getTime());
  471. assertParsedDateEquals(
  472. 2012, 2 - 1, 12, parserMonthDayYear, 'February 12, 2012', dateParsed);
  473. // The current behavior when parsing 'February 31, 2012' is to
  474. // return 'March 2, 2012'
  475. // Expected or not, we make sure the fix does not break this.
  476. assertParsedDateEquals(
  477. 2012, 3 - 1, 2, parserMonthDayYear, 'February 31, 2012', dateParsed);
  478. }
  479. /** @bug 9901750 */
  480. function testStandaloneMonthPattern() {
  481. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_pl;
  482. var date1 = new goog.date.Date(2006, 7 - 1);
  483. var date2 = new goog.date.Date();
  484. var formatter = new goog.i18n.DateTimeFormat('LLLL yyyy');
  485. var parser = new goog.i18n.DateTimeParse('LLLL yyyy');
  486. var dateStr = formatter.format(date1);
  487. assertParsedDateEquals(
  488. date1.getFullYear(), date1.getMonth(), undefined, parser, dateStr, date2);
  489. // Sanity tests to make sure MMM... (and LLL...) formats still work for
  490. // different locales.
  491. var symbols = [goog.i18n.DateTimeSymbols_en, goog.i18n.DateTimeSymbols_pl];
  492. for (var i = 0; i < symbols.length; i++) {
  493. goog.i18n.DateTimeSymbols = symbols[i];
  494. var tests = {
  495. 'MMMM yyyy': goog.i18n.DateTimeSymbols.MONTHS,
  496. 'LLLL yyyy': goog.i18n.DateTimeSymbols.STANDALONEMONTHS,
  497. 'MMM yyyy': goog.i18n.DateTimeSymbols.SHORTMONTHS,
  498. 'LLL yyyy': goog.i18n.DateTimeSymbols.STANDALONESHORTMONTHS
  499. };
  500. for (var format in tests) {
  501. var parser = new goog.i18n.DateTimeParse(format);
  502. var months = tests[format];
  503. for (var m = 0; m < months.length; m++) {
  504. var dateStr = months[m] + ' 2006';
  505. var date = new goog.date.Date();
  506. assertParsedDateEquals(2006, m, undefined, parser, dateStr, date);
  507. }
  508. }
  509. }
  510. }
  511. function testConstructorSymbols() {
  512. var y = 2015, m = 8, d = 28;
  513. var dateFr = new Date(y, m, d);
  514. var dateZh = new Date(y, m, d);
  515. var parserFr = new goog.i18n.DateTimeParse(
  516. goog.i18n.DateTimeFormat.Format.FULL_DATE, goog.i18n.DateTimeSymbols_fr);
  517. var parserZh = new goog.i18n.DateTimeParse(
  518. goog.i18n.DateTimeFormat.Format.FULL_DATE, goog.i18n.DateTimeSymbols_zh);
  519. var fmtFr = new goog.i18n.DateTimeFormat(
  520. goog.i18n.DateTimeFormat.Format.FULL_DATE, goog.i18n.DateTimeSymbols_fr);
  521. var fmtZh = new goog.i18n.DateTimeFormat(
  522. goog.i18n.DateTimeFormat.Format.FULL_DATE, goog.i18n.DateTimeSymbols_zh);
  523. var dateStrFr = fmtFr.format(dateFr);
  524. var dateStrZh = fmtZh.format(dateZh);
  525. assertParsedDateEquals(y, m, d, parserFr, dateStrFr, dateFr);
  526. assertParsedDateEquals(y, m, d, parserZh, dateStrZh, dateZh);
  527. }
  528. function testQuotedPattern() {
  529. // Regression test for b/29990921.
  530. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_en;
  531. var y = 2013, m = 10, d = 15;
  532. // Literal apostrophe
  533. var date = new Date(y, m, d);
  534. var parser = new goog.i18n.DateTimeParse('MMM \'\'yy');
  535. assertParsedDateEquals(y, m, d, parser, 'Nov \'13', date);
  536. // Quoted text
  537. date = new Date(y, m, d);
  538. parser = new goog.i18n.DateTimeParse('MMM dd\'th\' yyyy');
  539. assertParsedDateEquals(y, m, d, parser, 'Nov 15th 2013', date);
  540. // Quoted text (only opening apostrophe)
  541. date = new Date(y, m, d);
  542. parser = new goog.i18n.DateTimeParse('MMM dd\'th yyyy');
  543. assertParsedDateEquals(y, m, d, parser, 'Nov 15th yyyy', date);
  544. // Quoted text with literal apostrophe
  545. date = new Date(y, m, d);
  546. parser = new goog.i18n.DateTimeParse('MMM dd\'th\'\'\'');
  547. assertParsedDateEquals(y, m, d, parser, 'Nov 15th\'', date);
  548. // Quoted text with literal apostrophe (only opening apostrophe)
  549. date = new Date(y, m, d);
  550. parser = new goog.i18n.DateTimeParse('MMM dd\'th\'\'');
  551. assertParsedDateEquals(y, m, d, parser, 'Nov 15th\'', date);
  552. }
  553. function testNullDate() {
  554. var date = new Date();
  555. var parser = new goog.i18n.DateTimeParse('MM/dd, yyyyG');
  556. assertNotThrows(function() {
  557. parser.parse('11/22, 1999', date);
  558. });
  559. assertThrows(function() {
  560. parser.parse('11/22, 1999', null);
  561. });
  562. }