001: /*
002: * Copyright 2001-2006 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.time.format;
017:
018: import java.io.CharArrayWriter;
019: import java.util.Locale;
020: import java.util.TimeZone;
021:
022: import junit.framework.TestCase;
023: import junit.framework.TestSuite;
024:
025: import org.joda.time.Chronology;
026: import org.joda.time.DateTime;
027: import org.joda.time.DateTimeConstants;
028: import org.joda.time.DateTimeUtils;
029: import org.joda.time.DateTimeZone;
030: import org.joda.time.MutableDateTime;
031: import org.joda.time.ReadablePartial;
032: import org.joda.time.chrono.BuddhistChronology;
033: import org.joda.time.chrono.ISOChronology;
034:
035: /**
036: * This class is a Junit unit test for DateTime Formating.
037: *
038: * @author Stephen Colebourne
039: */
040: public class TestDateTimeFormatter extends TestCase {
041:
042: private static final DateTimeZone UTC = DateTimeZone.UTC;
043: private static final DateTimeZone PARIS = DateTimeZone
044: .forID("Europe/Paris");
045: private static final DateTimeZone LONDON = DateTimeZone
046: .forID("Europe/London");
047: private static final DateTimeZone TOKYO = DateTimeZone
048: .forID("Asia/Tokyo");
049: private static final DateTimeZone NEWYORK = DateTimeZone
050: .forID("America/New_York");
051: private static final Chronology ISO_UTC = ISOChronology
052: .getInstanceUTC();
053: private static final Chronology ISO_PARIS = ISOChronology
054: .getInstance(PARIS);
055: private static final Chronology BUDDHIST_PARIS = BuddhistChronology
056: .getInstance(PARIS);
057:
058: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
059: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
060: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
061: + 365 + 365 + 366 + 365;
062: // 2002-06-09
063: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
064: + 31L + 9L - 1L)
065: * DateTimeConstants.MILLIS_PER_DAY;
066:
067: private DateTimeZone originalDateTimeZone = null;
068: private TimeZone originalTimeZone = null;
069: private Locale originalLocale = null;
070: private DateTimeFormatter f = null;
071: private DateTimeFormatter g = null;
072:
073: public static void main(String[] args) {
074: junit.textui.TestRunner.run(suite());
075: }
076:
077: public static TestSuite suite() {
078: return new TestSuite(TestDateTimeFormatter.class);
079: }
080:
081: public TestDateTimeFormatter(String name) {
082: super (name);
083: }
084:
085: protected void setUp() throws Exception {
086: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
087: originalDateTimeZone = DateTimeZone.getDefault();
088: originalTimeZone = TimeZone.getDefault();
089: originalLocale = Locale.getDefault();
090: DateTimeZone.setDefault(LONDON);
091: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
092: Locale.setDefault(Locale.UK);
093: f = new DateTimeFormatterBuilder().appendDayOfWeekShortText()
094: .appendLiteral(' ').append(
095: ISODateTimeFormat.dateTimeNoMillis())
096: .toFormatter();
097: g = ISODateTimeFormat.dateTimeNoMillis();
098: }
099:
100: protected void tearDown() throws Exception {
101: DateTimeUtils.setCurrentMillisSystem();
102: DateTimeZone.setDefault(originalDateTimeZone);
103: TimeZone.setDefault(originalTimeZone);
104: Locale.setDefault(originalLocale);
105: originalDateTimeZone = null;
106: originalTimeZone = null;
107: originalLocale = null;
108: f = null;
109: g = null;
110: }
111:
112: //-----------------------------------------------------------------------
113: public void testPrint_simple() {
114: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
115: assertEquals("Wed 2004-06-09T10:20:30Z", f.print(dt));
116:
117: dt = dt.withZone(PARIS);
118: assertEquals("Wed 2004-06-09T12:20:30+02:00", f.print(dt));
119:
120: dt = dt.withZone(NEWYORK);
121: assertEquals("Wed 2004-06-09T06:20:30-04:00", f.print(dt));
122:
123: dt = dt.withChronology(BUDDHIST_PARIS);
124: assertEquals("Wed 2547-06-09T12:20:30+02:00", f.print(dt));
125: }
126:
127: //-----------------------------------------------------------------------
128: public void testPrint_locale() {
129: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
130: assertEquals("mer. 2004-06-09T10:20:30Z", f.withLocale(
131: Locale.FRENCH).print(dt));
132: assertEquals("Wed 2004-06-09T10:20:30Z", f.withLocale(null)
133: .print(dt));
134: }
135:
136: //-----------------------------------------------------------------------
137: public void testPrint_zone() {
138: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
139: assertEquals("Wed 2004-06-09T06:20:30-04:00", f.withZone(
140: NEWYORK).print(dt));
141: assertEquals("Wed 2004-06-09T12:20:30+02:00", f.withZone(PARIS)
142: .print(dt));
143: assertEquals("Wed 2004-06-09T10:20:30Z", f.withZone(null)
144: .print(dt));
145:
146: dt = dt.withZone(NEWYORK);
147: assertEquals("Wed 2004-06-09T06:20:30-04:00", f.withZone(
148: NEWYORK).print(dt));
149: assertEquals("Wed 2004-06-09T12:20:30+02:00", f.withZone(PARIS)
150: .print(dt));
151: assertEquals("Wed 2004-06-09T10:20:30Z", f.withZone(UTC).print(
152: dt));
153: assertEquals("Wed 2004-06-09T06:20:30-04:00", f.withZone(null)
154: .print(dt));
155: }
156:
157: //-----------------------------------------------------------------------
158: public void testPrint_chrono() {
159: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
160: assertEquals("Wed 2004-06-09T12:20:30+02:00", f.withChronology(
161: ISO_PARIS).print(dt));
162: assertEquals("Wed 2547-06-09T12:20:30+02:00", f.withChronology(
163: BUDDHIST_PARIS).print(dt));
164: assertEquals("Wed 2004-06-09T10:20:30Z", f.withChronology(null)
165: .print(dt));
166:
167: dt = dt.withChronology(BUDDHIST_PARIS);
168: assertEquals("Wed 2004-06-09T12:20:30+02:00", f.withChronology(
169: ISO_PARIS).print(dt));
170: assertEquals("Wed 2547-06-09T12:20:30+02:00", f.withChronology(
171: BUDDHIST_PARIS).print(dt));
172: assertEquals("Wed 2004-06-09T10:20:30Z", f.withChronology(
173: ISO_UTC).print(dt));
174: assertEquals("Wed 2547-06-09T12:20:30+02:00", f.withChronology(
175: null).print(dt));
176: }
177:
178: //-----------------------------------------------------------------------
179: public void testPrint_bufferMethods() throws Exception {
180: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
181: StringBuffer buf = new StringBuffer();
182: f.printTo(buf, dt);
183: assertEquals("Wed 2004-06-09T10:20:30Z", buf.toString());
184:
185: buf = new StringBuffer();
186: f.printTo(buf, dt.getMillis());
187: assertEquals("Wed 2004-06-09T11:20:30+01:00", buf.toString());
188:
189: buf = new StringBuffer();
190: ISODateTimeFormat.yearMonthDay().printTo(buf,
191: dt.toYearMonthDay());
192: assertEquals("2004-06-09", buf.toString());
193:
194: buf = new StringBuffer();
195: try {
196: ISODateTimeFormat.yearMonthDay().printTo(buf,
197: (ReadablePartial) null);
198: fail();
199: } catch (IllegalArgumentException ex) {
200: }
201: }
202:
203: //-----------------------------------------------------------------------
204: public void testPrint_writerMethods() throws Exception {
205: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
206: CharArrayWriter out = new CharArrayWriter();
207: f.printTo(out, dt);
208: assertEquals("Wed 2004-06-09T10:20:30Z", out.toString());
209:
210: out = new CharArrayWriter();
211: f.printTo(out, dt.getMillis());
212: assertEquals("Wed 2004-06-09T11:20:30+01:00", out.toString());
213:
214: out = new CharArrayWriter();
215: ISODateTimeFormat.yearMonthDay().printTo(out,
216: dt.toYearMonthDay());
217: assertEquals("2004-06-09", out.toString());
218:
219: out = new CharArrayWriter();
220: try {
221: ISODateTimeFormat.yearMonthDay().printTo(out,
222: (ReadablePartial) null);
223: fail();
224: } catch (IllegalArgumentException ex) {
225: }
226: }
227:
228: //-----------------------------------------------------------------------
229: public void testPrint_chrono_and_zone() {
230: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
231: assertEquals("Wed 2004-06-09T10:20:30Z", f.withChronology(null)
232: .withZone(null).print(dt));
233: assertEquals("Wed 2004-06-09T12:20:30+02:00", f.withChronology(
234: ISO_PARIS).withZone(null).print(dt));
235: assertEquals("Wed 2004-06-09T12:20:30+02:00", f.withChronology(
236: ISO_PARIS).withZone(PARIS).print(dt));
237: assertEquals("Wed 2004-06-09T06:20:30-04:00", f.withChronology(
238: ISO_PARIS).withZone(NEWYORK).print(dt));
239: assertEquals("Wed 2004-06-09T06:20:30-04:00", f.withChronology(
240: null).withZone(NEWYORK).print(dt));
241:
242: dt = dt.withChronology(ISO_PARIS);
243: assertEquals("Wed 2004-06-09T12:20:30+02:00", f.withChronology(
244: null).withZone(null).print(dt));
245: assertEquals("Wed 2004-06-09T12:20:30+02:00", f.withChronology(
246: ISO_PARIS).withZone(null).print(dt));
247: assertEquals("Wed 2004-06-09T12:20:30+02:00", f.withChronology(
248: ISO_PARIS).withZone(PARIS).print(dt));
249: assertEquals("Wed 2004-06-09T06:20:30-04:00", f.withChronology(
250: ISO_PARIS).withZone(NEWYORK).print(dt));
251: assertEquals("Wed 2004-06-09T06:20:30-04:00", f.withChronology(
252: null).withZone(NEWYORK).print(dt));
253:
254: dt = dt.withChronology(BUDDHIST_PARIS);
255: assertEquals("Wed 2547-06-09T12:20:30+02:00", f.withChronology(
256: null).withZone(null).print(dt));
257: assertEquals("Wed 2004-06-09T12:20:30+02:00", f.withChronology(
258: ISO_PARIS).withZone(null).print(dt));
259: assertEquals("Wed 2004-06-09T12:20:30+02:00", f.withChronology(
260: ISO_PARIS).withZone(PARIS).print(dt));
261: assertEquals("Wed 2004-06-09T06:20:30-04:00", f.withChronology(
262: ISO_PARIS).withZone(NEWYORK).print(dt));
263: assertEquals("Wed 2547-06-09T06:20:30-04:00", f.withChronology(
264: null).withZone(NEWYORK).print(dt));
265: }
266:
267: public void testWithGetLocale() {
268: DateTimeFormatter f2 = f.withLocale(Locale.FRENCH);
269: assertEquals(Locale.FRENCH, f2.getLocale());
270: assertSame(f2, f2.withLocale(Locale.FRENCH));
271:
272: f2 = f.withLocale(null);
273: assertEquals(null, f2.getLocale());
274: assertSame(f2, f2.withLocale(null));
275: }
276:
277: public void testWithGetZone() {
278: DateTimeFormatter f2 = f.withZone(PARIS);
279: assertEquals(PARIS, f2.getZone());
280: assertSame(f2, f2.withZone(PARIS));
281:
282: f2 = f.withZone(null);
283: assertEquals(null, f2.getZone());
284: assertSame(f2, f2.withZone(null));
285: }
286:
287: public void testWithGetChronology() {
288: DateTimeFormatter f2 = f.withChronology(BUDDHIST_PARIS);
289: assertEquals(BUDDHIST_PARIS, f2.getChronolgy());
290: assertSame(f2, f2.withChronology(BUDDHIST_PARIS));
291:
292: f2 = f.withChronology(null);
293: assertEquals(null, f2.getChronolgy());
294: assertSame(f2, f2.withChronology(null));
295: }
296:
297: public void testWithGetPivotYear() {
298: DateTimeFormatter f2 = f.withPivotYear(13);
299: assertEquals(new Integer(13), f2.getPivotYear());
300: assertSame(f2, f2.withPivotYear(13));
301:
302: f2 = f.withPivotYear(new Integer(14));
303: assertEquals(new Integer(14), f2.getPivotYear());
304: assertSame(f2, f2.withPivotYear(new Integer(14)));
305:
306: f2 = f.withPivotYear(null);
307: assertEquals(null, f2.getPivotYear());
308: assertSame(f2, f2.withPivotYear(null));
309: }
310:
311: public void testWithGetOffsetParsedMethods() {
312: DateTimeFormatter f2 = f;
313: assertEquals(false, f2.isOffsetParsed());
314: assertEquals(null, f2.getZone());
315:
316: f2 = f.withOffsetParsed();
317: assertEquals(true, f2.isOffsetParsed());
318: assertEquals(null, f2.getZone());
319:
320: f2 = f2.withZone(PARIS);
321: assertEquals(false, f2.isOffsetParsed());
322: assertEquals(PARIS, f2.getZone());
323:
324: f2 = f2.withOffsetParsed();
325: assertEquals(true, f2.isOffsetParsed());
326: assertEquals(null, f2.getZone());
327:
328: f2 = f.withOffsetParsed();
329: assertNotSame(f, f2);
330: DateTimeFormatter f3 = f2.withOffsetParsed();
331: assertSame(f2, f3);
332: }
333:
334: public void testPrinterParserMethods() {
335: DateTimeFormatter f2 = new DateTimeFormatter(f.getPrinter(), f
336: .getParser());
337: assertEquals(f.getPrinter(), f2.getPrinter());
338: assertEquals(f.getParser(), f2.getParser());
339: assertEquals(true, f2.isPrinter());
340: assertEquals(true, f2.isParser());
341: assertNotNull(f2.print(0L));
342: assertNotNull(f2.parseDateTime("Thu 1970-01-01T00:00:00Z"));
343:
344: f2 = new DateTimeFormatter(f.getPrinter(), null);
345: assertEquals(f.getPrinter(), f2.getPrinter());
346: assertEquals(null, f2.getParser());
347: assertEquals(true, f2.isPrinter());
348: assertEquals(false, f2.isParser());
349: assertNotNull(f2.print(0L));
350: try {
351: f2.parseDateTime("Thu 1970-01-01T00:00:00Z");
352: fail();
353: } catch (UnsupportedOperationException ex) {
354: }
355:
356: f2 = new DateTimeFormatter(null, f.getParser());
357: assertEquals(null, f2.getPrinter());
358: assertEquals(f.getParser(), f2.getParser());
359: assertEquals(false, f2.isPrinter());
360: assertEquals(true, f2.isParser());
361: try {
362: f2.print(0L);
363: fail();
364: } catch (UnsupportedOperationException ex) {
365: }
366: assertNotNull(f2.parseDateTime("Thu 1970-01-01T00:00:00Z"));
367: }
368:
369: //-----------------------------------------------------------------------
370: public void testParseDateTime_simple() {
371: DateTime expect = null;
372: expect = new DateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
373: assertEquals(expect, g.parseDateTime("2004-06-09T10:20:30Z"));
374:
375: try {
376: g.parseDateTime("ABC");
377: fail();
378: } catch (IllegalArgumentException ex) {
379: }
380: }
381:
382: public void testParseDateTime_zone() {
383: DateTime expect = null;
384: expect = new DateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
385: assertEquals(expect, g.withZone(LONDON).parseDateTime(
386: "2004-06-09T10:20:30Z"));
387:
388: expect = new DateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
389: assertEquals(expect, g.withZone(null).parseDateTime(
390: "2004-06-09T10:20:30Z"));
391:
392: expect = new DateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
393: assertEquals(expect, g.withZone(PARIS).parseDateTime(
394: "2004-06-09T10:20:30Z"));
395: }
396:
397: public void testParseDateTime_zone2() {
398: DateTime expect = null;
399: expect = new DateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
400: assertEquals(expect, g.withZone(LONDON).parseDateTime(
401: "2004-06-09T06:20:30-04:00"));
402:
403: expect = new DateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
404: assertEquals(expect, g.withZone(null).parseDateTime(
405: "2004-06-09T06:20:30-04:00"));
406:
407: expect = new DateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
408: assertEquals(expect, g.withZone(PARIS).parseDateTime(
409: "2004-06-09T06:20:30-04:00"));
410: }
411:
412: public void testParseDateTime_zone3() {
413: DateTimeFormatter h = new DateTimeFormatterBuilder().append(
414: ISODateTimeFormat.date()).appendLiteral('T').append(
415: ISODateTimeFormat.timeElementParser()).toFormatter();
416:
417: DateTime expect = null;
418: expect = new DateTime(2004, 6, 9, 10, 20, 30, 0, LONDON);
419: assertEquals(expect, h.withZone(LONDON).parseDateTime(
420: "2004-06-09T10:20:30"));
421:
422: expect = new DateTime(2004, 6, 9, 10, 20, 30, 0, LONDON);
423: assertEquals(expect, h.withZone(null).parseDateTime(
424: "2004-06-09T10:20:30"));
425:
426: expect = new DateTime(2004, 6, 9, 10, 20, 30, 0, PARIS);
427: assertEquals(expect, h.withZone(PARIS).parseDateTime(
428: "2004-06-09T10:20:30"));
429: }
430:
431: public void testParseDateTime_simple_precedence() {
432: DateTime expect = null;
433: // use correct day of week
434: expect = new DateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
435: assertEquals(expect, f
436: .parseDateTime("Wed 2004-06-09T10:20:30Z"));
437:
438: // use wrong day of week
439: expect = new DateTime(2004, 6, 7, 11, 20, 30, 0, LONDON);
440: // DayOfWeek takes precedence, because week < month in length
441: assertEquals(expect, f
442: .parseDateTime("Mon 2004-06-09T10:20:30Z"));
443: }
444:
445: public void testParseDateTime_offsetParsed() {
446: DateTime expect = null;
447: expect = new DateTime(2004, 6, 9, 10, 20, 30, 0, UTC);
448: assertEquals(expect, g.withOffsetParsed().parseDateTime(
449: "2004-06-09T10:20:30Z"));
450:
451: expect = new DateTime(2004, 6, 9, 6, 20, 30, 0, DateTimeZone
452: .forOffsetHours(-4));
453: assertEquals(expect, g.withOffsetParsed().parseDateTime(
454: "2004-06-09T06:20:30-04:00"));
455:
456: expect = new DateTime(2004, 6, 9, 10, 20, 30, 0, UTC);
457: assertEquals(expect, g.withZone(PARIS).withOffsetParsed()
458: .parseDateTime("2004-06-09T10:20:30Z"));
459: expect = new DateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
460: assertEquals(expect, g.withOffsetParsed().withZone(PARIS)
461: .parseDateTime("2004-06-09T10:20:30Z"));
462: }
463:
464: public void testParseDateTime_chrono() {
465: DateTime expect = null;
466: expect = new DateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
467: assertEquals(expect, g.withChronology(ISO_PARIS).parseDateTime(
468: "2004-06-09T10:20:30Z"));
469:
470: expect = new DateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
471: assertEquals(expect, g.withChronology(null).parseDateTime(
472: "2004-06-09T10:20:30Z"));
473:
474: expect = new DateTime(2547, 6, 9, 12, 20, 30, 0, BUDDHIST_PARIS);
475: assertEquals(expect, g.withChronology(BUDDHIST_PARIS)
476: .parseDateTime("2547-06-09T10:20:30Z"));
477:
478: expect = new DateTime(2004, 6, 9, 10, 29, 51, 0, BUDDHIST_PARIS); // zone is +00:09:21 in 1451
479: assertEquals(expect, g.withChronology(BUDDHIST_PARIS)
480: .parseDateTime("2004-06-09T10:20:30Z"));
481: }
482:
483: //-----------------------------------------------------------------------
484: public void testParseMutableDateTime_simple() {
485: MutableDateTime expect = null;
486: expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
487: assertEquals(expect, g
488: .parseMutableDateTime("2004-06-09T10:20:30Z"));
489:
490: try {
491: g.parseMutableDateTime("ABC");
492: fail();
493: } catch (IllegalArgumentException ex) {
494: }
495: }
496:
497: public void testParseMutableDateTime_zone() {
498: MutableDateTime expect = null;
499: expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
500: assertEquals(expect, g.withZone(LONDON).parseMutableDateTime(
501: "2004-06-09T10:20:30Z"));
502:
503: expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
504: assertEquals(expect, g.withZone(null).parseMutableDateTime(
505: "2004-06-09T10:20:30Z"));
506:
507: expect = new MutableDateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
508: assertEquals(expect, g.withZone(PARIS).parseMutableDateTime(
509: "2004-06-09T10:20:30Z"));
510: }
511:
512: public void testParseMutableDateTime_zone2() {
513: MutableDateTime expect = null;
514: expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
515: assertEquals(expect, g.withZone(LONDON).parseMutableDateTime(
516: "2004-06-09T06:20:30-04:00"));
517:
518: expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
519: assertEquals(expect, g.withZone(null).parseMutableDateTime(
520: "2004-06-09T06:20:30-04:00"));
521:
522: expect = new MutableDateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
523: assertEquals(expect, g.withZone(PARIS).parseMutableDateTime(
524: "2004-06-09T06:20:30-04:00"));
525: }
526:
527: public void testParseMutableDateTime_zone3() {
528: DateTimeFormatter h = new DateTimeFormatterBuilder().append(
529: ISODateTimeFormat.date()).appendLiteral('T').append(
530: ISODateTimeFormat.timeElementParser()).toFormatter();
531:
532: MutableDateTime expect = null;
533: expect = new MutableDateTime(2004, 6, 9, 10, 20, 30, 0, LONDON);
534: assertEquals(expect, h.withZone(LONDON).parseMutableDateTime(
535: "2004-06-09T10:20:30"));
536:
537: expect = new MutableDateTime(2004, 6, 9, 10, 20, 30, 0, LONDON);
538: assertEquals(expect, h.withZone(null).parseMutableDateTime(
539: "2004-06-09T10:20:30"));
540:
541: expect = new MutableDateTime(2004, 6, 9, 10, 20, 30, 0, PARIS);
542: assertEquals(expect, h.withZone(PARIS).parseMutableDateTime(
543: "2004-06-09T10:20:30"));
544: }
545:
546: public void testParseMutableDateTime_simple_precedence() {
547: MutableDateTime expect = null;
548: // use correct day of week
549: expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
550: assertEquals(expect, f
551: .parseDateTime("Wed 2004-06-09T10:20:30Z"));
552:
553: // use wrong day of week
554: expect = new MutableDateTime(2004, 6, 7, 11, 20, 30, 0, LONDON);
555: // DayOfWeek takes precedence, because week < month in length
556: assertEquals(expect, f
557: .parseDateTime("Mon 2004-06-09T10:20:30Z"));
558: }
559:
560: public void testParseMutableDateTime_offsetParsed() {
561: MutableDateTime expect = null;
562: expect = new MutableDateTime(2004, 6, 9, 10, 20, 30, 0, UTC);
563: assertEquals(expect, g.withOffsetParsed().parseMutableDateTime(
564: "2004-06-09T10:20:30Z"));
565:
566: expect = new MutableDateTime(2004, 6, 9, 6, 20, 30, 0,
567: DateTimeZone.forOffsetHours(-4));
568: assertEquals(expect, g.withOffsetParsed().parseMutableDateTime(
569: "2004-06-09T06:20:30-04:00"));
570:
571: expect = new MutableDateTime(2004, 6, 9, 10, 20, 30, 0, UTC);
572: assertEquals(expect, g.withZone(PARIS).withOffsetParsed()
573: .parseMutableDateTime("2004-06-09T10:20:30Z"));
574: expect = new MutableDateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
575: assertEquals(expect, g.withOffsetParsed().withZone(PARIS)
576: .parseMutableDateTime("2004-06-09T10:20:30Z"));
577: }
578:
579: public void testParseMutableDateTime_chrono() {
580: MutableDateTime expect = null;
581: expect = new MutableDateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
582: assertEquals(expect, g.withChronology(ISO_PARIS)
583: .parseMutableDateTime("2004-06-09T10:20:30Z"));
584:
585: expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
586: assertEquals(expect, g.withChronology(null)
587: .parseMutableDateTime("2004-06-09T10:20:30Z"));
588:
589: expect = new MutableDateTime(2547, 6, 9, 12, 20, 30, 0,
590: BUDDHIST_PARIS);
591: assertEquals(expect, g.withChronology(BUDDHIST_PARIS)
592: .parseMutableDateTime("2547-06-09T10:20:30Z"));
593:
594: expect = new MutableDateTime(2004, 6, 9, 10, 29, 51, 0,
595: BUDDHIST_PARIS); // zone is +00:09:21 in 1451
596: assertEquals(expect, g.withChronology(BUDDHIST_PARIS)
597: .parseMutableDateTime("2004-06-09T10:20:30Z"));
598: }
599:
600: //-----------------------------------------------------------------------
601: public void testParseInto_simple() {
602: MutableDateTime expect = null;
603: expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
604: MutableDateTime result = new MutableDateTime(0L);
605: assertEquals(20, g.parseInto(result, "2004-06-09T10:20:30Z", 0));
606: assertEquals(expect, result);
607:
608: try {
609: g.parseInto(null, "2004-06-09T10:20:30Z", 0);
610: fail();
611: } catch (IllegalArgumentException ex) {
612: }
613:
614: assertEquals(~0, g.parseInto(result, "ABC", 0));
615: assertEquals(~10, g.parseInto(result, "2004-06-09", 0));
616: assertEquals(~13, g.parseInto(result, "XX2004-06-09T", 2));
617: }
618:
619: public void testParseInto_zone() {
620: MutableDateTime expect = null;
621: MutableDateTime result = null;
622: expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
623: result = new MutableDateTime(0L);
624: assertEquals(20, g.withZone(LONDON).parseInto(result,
625: "2004-06-09T10:20:30Z", 0));
626: assertEquals(expect, result);
627:
628: expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
629: result = new MutableDateTime(0L);
630: assertEquals(20, g.withZone(null).parseInto(result,
631: "2004-06-09T10:20:30Z", 0));
632: assertEquals(expect, result);
633:
634: expect = new MutableDateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
635: result = new MutableDateTime(0L);
636: assertEquals(20, g.withZone(PARIS).parseInto(result,
637: "2004-06-09T10:20:30Z", 0));
638: assertEquals(expect, result);
639: }
640:
641: public void testParseInto_zone2() {
642: MutableDateTime expect = null;
643: MutableDateTime result = null;
644: expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
645: result = new MutableDateTime(0L);
646: assertEquals(25, g.withZone(LONDON).parseInto(result,
647: "2004-06-09T06:20:30-04:00", 0));
648: assertEquals(expect, result);
649:
650: expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
651: assertEquals(25, g.withZone(null).parseInto(result,
652: "2004-06-09T06:20:30-04:00", 0));
653: assertEquals(expect, result);
654:
655: expect = new MutableDateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
656: assertEquals(25, g.withZone(PARIS).parseInto(result,
657: "2004-06-09T06:20:30-04:00", 0));
658: assertEquals(expect, result);
659: }
660:
661: public void testParseInto_zone3() {
662: DateTimeFormatter h = new DateTimeFormatterBuilder().append(
663: ISODateTimeFormat.date()).appendLiteral('T').append(
664: ISODateTimeFormat.timeElementParser()).toFormatter();
665:
666: MutableDateTime expect = null;
667: MutableDateTime result = null;
668: expect = new MutableDateTime(2004, 6, 9, 10, 20, 30, 0, LONDON);
669: result = new MutableDateTime(0L);
670: assertEquals(19, h.withZone(LONDON).parseInto(result,
671: "2004-06-09T10:20:30", 0));
672: assertEquals(expect, result);
673:
674: expect = new MutableDateTime(2004, 6, 9, 10, 20, 30, 0, LONDON);
675: result = new MutableDateTime(0L);
676: assertEquals(19, h.withZone(null).parseInto(result,
677: "2004-06-09T10:20:30", 0));
678: assertEquals(expect, result);
679:
680: expect = new MutableDateTime(2004, 6, 9, 10, 20, 30, 0, PARIS);
681: result = new MutableDateTime(0L);
682: assertEquals(19, h.withZone(PARIS).parseInto(result,
683: "2004-06-09T10:20:30", 0));
684: assertEquals(expect, result);
685: }
686:
687: public void testParseInto_simple_precedence() {
688: MutableDateTime expect = null;
689: MutableDateTime result = null;
690: expect = new MutableDateTime(2004, 6, 7, 11, 20, 30, 0, LONDON);
691: result = new MutableDateTime(0L);
692: // DayOfWeek takes precedence, because week < month in length
693: assertEquals(24, f.parseInto(result,
694: "Mon 2004-06-09T10:20:30Z", 0));
695: assertEquals(expect, result);
696: }
697:
698: public void testParseInto_offsetParsed() {
699: MutableDateTime expect = null;
700: MutableDateTime result = null;
701: expect = new MutableDateTime(2004, 6, 9, 10, 20, 30, 0, UTC);
702: result = new MutableDateTime(0L);
703: assertEquals(20, g.withOffsetParsed().parseInto(result,
704: "2004-06-09T10:20:30Z", 0));
705: assertEquals(expect, result);
706:
707: expect = new MutableDateTime(2004, 6, 9, 6, 20, 30, 0,
708: DateTimeZone.forOffsetHours(-4));
709: result = new MutableDateTime(0L);
710: assertEquals(25, g.withOffsetParsed().parseInto(result,
711: "2004-06-09T06:20:30-04:00", 0));
712: assertEquals(expect, result);
713:
714: expect = new MutableDateTime(2004, 6, 9, 10, 20, 30, 0, UTC);
715: result = new MutableDateTime(0L);
716: assertEquals(20, g.withZone(PARIS).withOffsetParsed()
717: .parseInto(result, "2004-06-09T10:20:30Z", 0));
718: assertEquals(expect, result);
719: expect = new MutableDateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
720: result = new MutableDateTime(0L);
721: assertEquals(20, g.withOffsetParsed().withZone(PARIS)
722: .parseInto(result, "2004-06-09T10:20:30Z", 0));
723: assertEquals(expect, result);
724: }
725:
726: public void testParseInto_chrono() {
727: MutableDateTime expect = null;
728: MutableDateTime result = null;
729: expect = new MutableDateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
730: result = new MutableDateTime(0L);
731: assertEquals(20, g.withChronology(ISO_PARIS).parseInto(result,
732: "2004-06-09T10:20:30Z", 0));
733: assertEquals(expect, result);
734:
735: expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
736: result = new MutableDateTime(0L);
737: assertEquals(20, g.withChronology(null).parseInto(result,
738: "2004-06-09T10:20:30Z", 0));
739: assertEquals(expect, result);
740:
741: expect = new MutableDateTime(2547, 6, 9, 12, 20, 30, 0,
742: BUDDHIST_PARIS);
743: result = new MutableDateTime(0L);
744: assertEquals(20, g.withChronology(BUDDHIST_PARIS).parseInto(
745: result, "2547-06-09T10:20:30Z", 0));
746: assertEquals(expect, result);
747:
748: expect = new MutableDateTime(2004, 6, 9, 10, 29, 51, 0,
749: BUDDHIST_PARIS);
750: result = new MutableDateTime(0L);
751: assertEquals(20, g.withChronology(BUDDHIST_PARIS).parseInto(
752: result, "2004-06-09T10:20:30Z", 0));
753: assertEquals(expect, result);
754: }
755:
756: public void testParseMillis_fractionOfSecondLong() {
757: DateTimeFormatter f = new DateTimeFormatterBuilder()
758: .appendSecondOfDay(2).appendLiteral('.')
759: .appendFractionOfSecond(1, 9).toFormatter().withZone(
760: DateTimeZone.UTC);
761: assertEquals(10512, f.parseMillis("10.5123456"));
762: assertEquals(10512, f.parseMillis("10.512999"));
763: }
764:
765: //-----------------------------------------------------------------------
766: // Ensure time zone name switches properly at the zone DST transition.
767: public void testZoneNameNearTransition() {
768: DateTime inDST_1 = new DateTime(2005, 10, 30, 1, 0, 0, 0,
769: NEWYORK);
770: DateTime inDST_2 = new DateTime(2005, 10, 30, 1, 59, 59, 999,
771: NEWYORK);
772: DateTime onDST = new DateTime(2005, 10, 30, 2, 0, 0, 0, NEWYORK);
773: DateTime outDST = new DateTime(2005, 10, 30, 2, 0, 0, 1,
774: NEWYORK);
775: DateTime outDST_2 = new DateTime(2005, 10, 30, 2, 0, 1, 0,
776: NEWYORK);
777:
778: DateTimeFormatter fmt = DateTimeFormat
779: .forPattern("yyy-MM-dd HH:mm:ss.S zzzz");
780: assertEquals("2005-10-30 01:00:00.0 Eastern Daylight Time", fmt
781: .print(inDST_1));
782: assertEquals("2005-10-30 01:59:59.9 Eastern Daylight Time", fmt
783: .print(inDST_2));
784: assertEquals("2005-10-30 02:00:00.0 Eastern Standard Time", fmt
785: .print(onDST));
786: assertEquals("2005-10-30 02:00:00.0 Eastern Standard Time", fmt
787: .print(outDST));
788: assertEquals("2005-10-30 02:00:01.0 Eastern Standard Time", fmt
789: .print(outDST_2));
790: }
791:
792: // Ensure time zone name switches properly at the zone DST transition.
793: public void testZoneShortNameNearTransition() {
794: DateTime inDST_1 = new DateTime(2005, 10, 30, 1, 0, 0, 0,
795: NEWYORK);
796: DateTime inDST_2 = new DateTime(2005, 10, 30, 1, 59, 59, 999,
797: NEWYORK);
798: DateTime onDST = new DateTime(2005, 10, 30, 2, 0, 0, 0, NEWYORK);
799: DateTime outDST = new DateTime(2005, 10, 30, 2, 0, 0, 1,
800: NEWYORK);
801: DateTime outDST_2 = new DateTime(2005, 10, 30, 2, 0, 1, 0,
802: NEWYORK);
803:
804: DateTimeFormatter fmt = DateTimeFormat
805: .forPattern("yyy-MM-dd HH:mm:ss.S z");
806: assertEquals("2005-10-30 01:00:00.0 EDT", fmt.print(inDST_1));
807: assertEquals("2005-10-30 01:59:59.9 EDT", fmt.print(inDST_2));
808: assertEquals("2005-10-30 02:00:00.0 EST", fmt.print(onDST));
809: assertEquals("2005-10-30 02:00:00.0 EST", fmt.print(outDST));
810: assertEquals("2005-10-30 02:00:01.0 EST", fmt.print(outDST_2));
811: }
812:
813: }
|