001: /*
002: * Copyright 2001-2005 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.chrono;
017:
018: import java.util.Locale;
019: import java.util.TimeZone;
020:
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: import org.joda.time.Chronology;
025: import org.joda.time.DateTime;
026: import org.joda.time.DateTimeConstants;
027: import org.joda.time.DateTimeField;
028: import org.joda.time.DateTimeUtils;
029: import org.joda.time.DateTimeZone;
030: import org.joda.time.DurationField;
031: import org.joda.time.DurationFieldType;
032: import org.joda.time.DateTime.Property;
033:
034: /**
035: * This class is a Junit unit test for EthiopicChronology.
036: *
037: * @author Stephen Colebourne
038: */
039: public class TestEthiopicChronology extends TestCase {
040:
041: private static final int MILLIS_PER_DAY = DateTimeConstants.MILLIS_PER_DAY;
042:
043: private static long SKIP = 1 * MILLIS_PER_DAY;
044:
045: private static final DateTimeZone PARIS = DateTimeZone
046: .forID("Europe/Paris");
047: private static final DateTimeZone LONDON = DateTimeZone
048: .forID("Europe/London");
049: private static final DateTimeZone TOKYO = DateTimeZone
050: .forID("Asia/Tokyo");
051: private static final Chronology ETHIOPIC_UTC = EthiopicChronology
052: .getInstanceUTC();
053: private static final Chronology JULIAN_UTC = JulianChronology
054: .getInstanceUTC();
055: private static final Chronology ISO_UTC = ISOChronology
056: .getInstanceUTC();
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: * MILLIS_PER_DAY;
066:
067: private DateTimeZone originalDateTimeZone = null;
068: private TimeZone originalTimeZone = null;
069: private Locale originalLocale = null;
070:
071: public static void main(String[] args) {
072: junit.textui.TestRunner.run(suite());
073: }
074:
075: public static TestSuite suite() {
076: SKIP = 1 * MILLIS_PER_DAY;
077: return new TestSuite(TestEthiopicChronology.class);
078: }
079:
080: public TestEthiopicChronology(String name) {
081: super (name);
082: }
083:
084: protected void setUp() throws Exception {
085: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
086: originalDateTimeZone = DateTimeZone.getDefault();
087: originalTimeZone = TimeZone.getDefault();
088: originalLocale = Locale.getDefault();
089: DateTimeZone.setDefault(LONDON);
090: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
091: Locale.setDefault(Locale.UK);
092: }
093:
094: protected void tearDown() throws Exception {
095: DateTimeUtils.setCurrentMillisSystem();
096: DateTimeZone.setDefault(originalDateTimeZone);
097: TimeZone.setDefault(originalTimeZone);
098: Locale.setDefault(originalLocale);
099: originalDateTimeZone = null;
100: originalTimeZone = null;
101: originalLocale = null;
102: }
103:
104: //-----------------------------------------------------------------------
105: public void testFactoryUTC() {
106: assertEquals(DateTimeZone.UTC, EthiopicChronology
107: .getInstanceUTC().getZone());
108: assertSame(EthiopicChronology.class, EthiopicChronology
109: .getInstanceUTC().getClass());
110: }
111:
112: public void testFactory() {
113: assertEquals(LONDON, EthiopicChronology.getInstance().getZone());
114: assertSame(EthiopicChronology.class, EthiopicChronology
115: .getInstance().getClass());
116: }
117:
118: public void testFactory_Zone() {
119: assertEquals(TOKYO, EthiopicChronology.getInstance(TOKYO)
120: .getZone());
121: assertEquals(PARIS, EthiopicChronology.getInstance(PARIS)
122: .getZone());
123: assertEquals(LONDON, EthiopicChronology.getInstance(null)
124: .getZone());
125: assertSame(EthiopicChronology.class, EthiopicChronology
126: .getInstance(TOKYO).getClass());
127: }
128:
129: //-----------------------------------------------------------------------
130: public void testEquality() {
131: assertSame(EthiopicChronology.getInstance(TOKYO),
132: EthiopicChronology.getInstance(TOKYO));
133: assertSame(EthiopicChronology.getInstance(LONDON),
134: EthiopicChronology.getInstance(LONDON));
135: assertSame(EthiopicChronology.getInstance(PARIS),
136: EthiopicChronology.getInstance(PARIS));
137: assertSame(EthiopicChronology.getInstanceUTC(),
138: EthiopicChronology.getInstanceUTC());
139: assertSame(EthiopicChronology.getInstance(), EthiopicChronology
140: .getInstance(LONDON));
141: }
142:
143: public void testWithUTC() {
144: assertSame(EthiopicChronology.getInstanceUTC(),
145: EthiopicChronology.getInstance(LONDON).withUTC());
146: assertSame(EthiopicChronology.getInstanceUTC(),
147: EthiopicChronology.getInstance(TOKYO).withUTC());
148: assertSame(EthiopicChronology.getInstanceUTC(),
149: EthiopicChronology.getInstanceUTC().withUTC());
150: assertSame(EthiopicChronology.getInstanceUTC(),
151: EthiopicChronology.getInstance().withUTC());
152: }
153:
154: public void testWithZone() {
155: assertSame(EthiopicChronology.getInstance(TOKYO),
156: EthiopicChronology.getInstance(TOKYO).withZone(TOKYO));
157: assertSame(EthiopicChronology.getInstance(LONDON),
158: EthiopicChronology.getInstance(TOKYO).withZone(LONDON));
159: assertSame(EthiopicChronology.getInstance(PARIS),
160: EthiopicChronology.getInstance(TOKYO).withZone(PARIS));
161: assertSame(EthiopicChronology.getInstance(LONDON),
162: EthiopicChronology.getInstance(TOKYO).withZone(null));
163: assertSame(EthiopicChronology.getInstance(PARIS),
164: EthiopicChronology.getInstance().withZone(PARIS));
165: assertSame(EthiopicChronology.getInstance(PARIS),
166: EthiopicChronology.getInstanceUTC().withZone(PARIS));
167: }
168:
169: public void testToString() {
170: assertEquals("EthiopicChronology[Europe/London]",
171: EthiopicChronology.getInstance(LONDON).toString());
172: assertEquals("EthiopicChronology[Asia/Tokyo]",
173: EthiopicChronology.getInstance(TOKYO).toString());
174: assertEquals("EthiopicChronology[Europe/London]",
175: EthiopicChronology.getInstance().toString());
176: assertEquals("EthiopicChronology[UTC]", EthiopicChronology
177: .getInstanceUTC().toString());
178: }
179:
180: //-----------------------------------------------------------------------
181: public void testDurationFields() {
182: assertEquals("eras", EthiopicChronology.getInstance().eras()
183: .getName());
184: assertEquals("centuries", EthiopicChronology.getInstance()
185: .centuries().getName());
186: assertEquals("years", EthiopicChronology.getInstance().years()
187: .getName());
188: assertEquals("weekyears", EthiopicChronology.getInstance()
189: .weekyears().getName());
190: assertEquals("months", EthiopicChronology.getInstance()
191: .months().getName());
192: assertEquals("weeks", EthiopicChronology.getInstance().weeks()
193: .getName());
194: assertEquals("days", EthiopicChronology.getInstance().days()
195: .getName());
196: assertEquals("halfdays", EthiopicChronology.getInstance()
197: .halfdays().getName());
198: assertEquals("hours", EthiopicChronology.getInstance().hours()
199: .getName());
200: assertEquals("minutes", EthiopicChronology.getInstance()
201: .minutes().getName());
202: assertEquals("seconds", EthiopicChronology.getInstance()
203: .seconds().getName());
204: assertEquals("millis", EthiopicChronology.getInstance()
205: .millis().getName());
206:
207: assertEquals(false, EthiopicChronology.getInstance().eras()
208: .isSupported());
209: assertEquals(true, EthiopicChronology.getInstance().centuries()
210: .isSupported());
211: assertEquals(true, EthiopicChronology.getInstance().years()
212: .isSupported());
213: assertEquals(true, EthiopicChronology.getInstance().weekyears()
214: .isSupported());
215: assertEquals(true, EthiopicChronology.getInstance().months()
216: .isSupported());
217: assertEquals(true, EthiopicChronology.getInstance().weeks()
218: .isSupported());
219: assertEquals(true, EthiopicChronology.getInstance().days()
220: .isSupported());
221: assertEquals(true, EthiopicChronology.getInstance().halfdays()
222: .isSupported());
223: assertEquals(true, EthiopicChronology.getInstance().hours()
224: .isSupported());
225: assertEquals(true, EthiopicChronology.getInstance().minutes()
226: .isSupported());
227: assertEquals(true, EthiopicChronology.getInstance().seconds()
228: .isSupported());
229: assertEquals(true, EthiopicChronology.getInstance().millis()
230: .isSupported());
231:
232: assertEquals(false, EthiopicChronology.getInstance()
233: .centuries().isPrecise());
234: assertEquals(false, EthiopicChronology.getInstance().years()
235: .isPrecise());
236: assertEquals(false, EthiopicChronology.getInstance()
237: .weekyears().isPrecise());
238: assertEquals(false, EthiopicChronology.getInstance().months()
239: .isPrecise());
240: assertEquals(false, EthiopicChronology.getInstance().weeks()
241: .isPrecise());
242: assertEquals(false, EthiopicChronology.getInstance().days()
243: .isPrecise());
244: assertEquals(false, EthiopicChronology.getInstance().halfdays()
245: .isPrecise());
246: assertEquals(true, EthiopicChronology.getInstance().hours()
247: .isPrecise());
248: assertEquals(true, EthiopicChronology.getInstance().minutes()
249: .isPrecise());
250: assertEquals(true, EthiopicChronology.getInstance().seconds()
251: .isPrecise());
252: assertEquals(true, EthiopicChronology.getInstance().millis()
253: .isPrecise());
254:
255: assertEquals(false, EthiopicChronology.getInstanceUTC()
256: .centuries().isPrecise());
257: assertEquals(false, EthiopicChronology.getInstanceUTC().years()
258: .isPrecise());
259: assertEquals(false, EthiopicChronology.getInstanceUTC()
260: .weekyears().isPrecise());
261: assertEquals(false, EthiopicChronology.getInstanceUTC()
262: .months().isPrecise());
263: assertEquals(true, EthiopicChronology.getInstanceUTC().weeks()
264: .isPrecise());
265: assertEquals(true, EthiopicChronology.getInstanceUTC().days()
266: .isPrecise());
267: assertEquals(true, EthiopicChronology.getInstanceUTC()
268: .halfdays().isPrecise());
269: assertEquals(true, EthiopicChronology.getInstanceUTC().hours()
270: .isPrecise());
271: assertEquals(true, EthiopicChronology.getInstanceUTC()
272: .minutes().isPrecise());
273: assertEquals(true, EthiopicChronology.getInstanceUTC()
274: .seconds().isPrecise());
275: assertEquals(true, EthiopicChronology.getInstanceUTC().millis()
276: .isPrecise());
277: }
278:
279: public void testDateFields() {
280: assertEquals("era", EthiopicChronology.getInstance().era()
281: .getName());
282: assertEquals("centuryOfEra", EthiopicChronology.getInstance()
283: .centuryOfEra().getName());
284: assertEquals("yearOfCentury", EthiopicChronology.getInstance()
285: .yearOfCentury().getName());
286: assertEquals("yearOfEra", EthiopicChronology.getInstance()
287: .yearOfEra().getName());
288: assertEquals("year", EthiopicChronology.getInstance().year()
289: .getName());
290: assertEquals("monthOfYear", EthiopicChronology.getInstance()
291: .monthOfYear().getName());
292: assertEquals("weekyearOfCentury", EthiopicChronology
293: .getInstance().weekyearOfCentury().getName());
294: assertEquals("weekyear", EthiopicChronology.getInstance()
295: .weekyear().getName());
296: assertEquals("weekOfWeekyear", EthiopicChronology.getInstance()
297: .weekOfWeekyear().getName());
298: assertEquals("dayOfYear", EthiopicChronology.getInstance()
299: .dayOfYear().getName());
300: assertEquals("dayOfMonth", EthiopicChronology.getInstance()
301: .dayOfMonth().getName());
302: assertEquals("dayOfWeek", EthiopicChronology.getInstance()
303: .dayOfWeek().getName());
304:
305: assertEquals(true, EthiopicChronology.getInstance().era()
306: .isSupported());
307: assertEquals(true, EthiopicChronology.getInstance()
308: .centuryOfEra().isSupported());
309: assertEquals(true, EthiopicChronology.getInstance()
310: .yearOfCentury().isSupported());
311: assertEquals(true, EthiopicChronology.getInstance().yearOfEra()
312: .isSupported());
313: assertEquals(true, EthiopicChronology.getInstance().year()
314: .isSupported());
315: assertEquals(true, EthiopicChronology.getInstance()
316: .monthOfYear().isSupported());
317: assertEquals(true, EthiopicChronology.getInstance()
318: .weekyearOfCentury().isSupported());
319: assertEquals(true, EthiopicChronology.getInstance().weekyear()
320: .isSupported());
321: assertEquals(true, EthiopicChronology.getInstance()
322: .weekOfWeekyear().isSupported());
323: assertEquals(true, EthiopicChronology.getInstance().dayOfYear()
324: .isSupported());
325: assertEquals(true, EthiopicChronology.getInstance()
326: .dayOfMonth().isSupported());
327: assertEquals(true, EthiopicChronology.getInstance().dayOfWeek()
328: .isSupported());
329: }
330:
331: public void testTimeFields() {
332: assertEquals("halfdayOfDay", EthiopicChronology.getInstance()
333: .halfdayOfDay().getName());
334: assertEquals("clockhourOfHalfday", EthiopicChronology
335: .getInstance().clockhourOfHalfday().getName());
336: assertEquals("hourOfHalfday", EthiopicChronology.getInstance()
337: .hourOfHalfday().getName());
338: assertEquals("clockhourOfDay", EthiopicChronology.getInstance()
339: .clockhourOfDay().getName());
340: assertEquals("hourOfDay", EthiopicChronology.getInstance()
341: .hourOfDay().getName());
342: assertEquals("minuteOfDay", EthiopicChronology.getInstance()
343: .minuteOfDay().getName());
344: assertEquals("minuteOfHour", EthiopicChronology.getInstance()
345: .minuteOfHour().getName());
346: assertEquals("secondOfDay", EthiopicChronology.getInstance()
347: .secondOfDay().getName());
348: assertEquals("secondOfMinute", EthiopicChronology.getInstance()
349: .secondOfMinute().getName());
350: assertEquals("millisOfDay", EthiopicChronology.getInstance()
351: .millisOfDay().getName());
352: assertEquals("millisOfSecond", EthiopicChronology.getInstance()
353: .millisOfSecond().getName());
354:
355: assertEquals(true, EthiopicChronology.getInstance()
356: .halfdayOfDay().isSupported());
357: assertEquals(true, EthiopicChronology.getInstance()
358: .clockhourOfHalfday().isSupported());
359: assertEquals(true, EthiopicChronology.getInstance()
360: .hourOfHalfday().isSupported());
361: assertEquals(true, EthiopicChronology.getInstance()
362: .clockhourOfDay().isSupported());
363: assertEquals(true, EthiopicChronology.getInstance().hourOfDay()
364: .isSupported());
365: assertEquals(true, EthiopicChronology.getInstance()
366: .minuteOfDay().isSupported());
367: assertEquals(true, EthiopicChronology.getInstance()
368: .minuteOfHour().isSupported());
369: assertEquals(true, EthiopicChronology.getInstance()
370: .secondOfDay().isSupported());
371: assertEquals(true, EthiopicChronology.getInstance()
372: .secondOfMinute().isSupported());
373: assertEquals(true, EthiopicChronology.getInstance()
374: .millisOfDay().isSupported());
375: assertEquals(true, EthiopicChronology.getInstance()
376: .millisOfSecond().isSupported());
377: }
378:
379: //-----------------------------------------------------------------------
380: public void testEpoch() {
381: DateTime epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, ETHIOPIC_UTC);
382: assertEquals(new DateTime(8, 8, 29, 0, 0, 0, 0, JULIAN_UTC),
383: epoch.withChronology(JULIAN_UTC));
384: }
385:
386: public void testEra() {
387: assertEquals(1, EthiopicChronology.EE);
388: try {
389: new DateTime(-1, 13, 5, 0, 0, 0, 0, ETHIOPIC_UTC);
390: fail();
391: } catch (IllegalArgumentException ex) {
392: }
393: }
394:
395: //-----------------------------------------------------------------------
396: /**
397: * Tests era, year, monthOfYear, dayOfMonth and dayOfWeek.
398: */
399: public void testCalendar() {
400: if (TestAll.FAST) {
401: return;
402: }
403: System.out.println("\nTestEthiopicChronology.testCalendar");
404: DateTime epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, ETHIOPIC_UTC);
405: long millis = epoch.getMillis();
406: long end = new DateTime(3000, 1, 1, 0, 0, 0, 0, ISO_UTC)
407: .getMillis();
408: DateTimeField dayOfWeek = ETHIOPIC_UTC.dayOfWeek();
409: DateTimeField dayOfYear = ETHIOPIC_UTC.dayOfYear();
410: DateTimeField dayOfMonth = ETHIOPIC_UTC.dayOfMonth();
411: DateTimeField monthOfYear = ETHIOPIC_UTC.monthOfYear();
412: DateTimeField year = ETHIOPIC_UTC.year();
413: DateTimeField yearOfEra = ETHIOPIC_UTC.yearOfEra();
414: DateTimeField era = ETHIOPIC_UTC.era();
415: int expectedDOW = new DateTime(8, 8, 29, 0, 0, 0, 0, JULIAN_UTC)
416: .getDayOfWeek();
417: int expectedDOY = 1;
418: int expectedDay = 1;
419: int expectedMonth = 1;
420: int expectedYear = 1;
421: while (millis < end) {
422: int dowValue = dayOfWeek.get(millis);
423: int doyValue = dayOfYear.get(millis);
424: int dayValue = dayOfMonth.get(millis);
425: int monthValue = monthOfYear.get(millis);
426: int yearValue = year.get(millis);
427: int yearOfEraValue = yearOfEra.get(millis);
428: int monthLen = dayOfMonth.getMaximumValue(millis);
429: if (monthValue < 1 || monthValue > 13) {
430: fail("Bad month: " + millis);
431: }
432:
433: // test era
434: assertEquals(1, era.get(millis));
435: assertEquals("EE", era.getAsText(millis));
436: assertEquals("EE", era.getAsShortText(millis));
437:
438: // test date
439: assertEquals(expectedYear, yearValue);
440: assertEquals(expectedYear, yearOfEraValue);
441: assertEquals(expectedMonth, monthValue);
442: assertEquals(expectedDay, dayValue);
443: assertEquals(expectedDOW, dowValue);
444: assertEquals(expectedDOY, doyValue);
445:
446: // test leap year
447: assertEquals(yearValue % 4 == 3, year.isLeap(millis));
448:
449: // test month length
450: if (monthValue == 13) {
451: assertEquals(yearValue % 4 == 3, monthOfYear
452: .isLeap(millis));
453: if (yearValue % 4 == 3) {
454: assertEquals(6, monthLen);
455: } else {
456: assertEquals(5, monthLen);
457: }
458: } else {
459: assertEquals(30, monthLen);
460: }
461:
462: // recalculate date
463: expectedDOW = (((expectedDOW + 1) - 1) % 7) + 1;
464: expectedDay++;
465: expectedDOY++;
466: if (expectedDay == 31 && expectedMonth < 13) {
467: expectedDay = 1;
468: expectedMonth++;
469: } else if (expectedMonth == 13) {
470: if (expectedYear % 4 == 3 && expectedDay == 7) {
471: expectedDay = 1;
472: expectedMonth = 1;
473: expectedYear++;
474: expectedDOY = 1;
475: } else if (expectedYear % 4 != 3 && expectedDay == 6) {
476: expectedDay = 1;
477: expectedMonth = 1;
478: expectedYear++;
479: expectedDOY = 1;
480: }
481: }
482: millis += SKIP;
483: }
484: }
485:
486: public void testSampleDate() {
487: DateTime dt = new DateTime(2004, 6, 9, 0, 0, 0, 0, ISO_UTC)
488: .withChronology(ETHIOPIC_UTC);
489: assertEquals(EthiopicChronology.EE, dt.getEra());
490: assertEquals(20, dt.getCenturyOfEra()); // TODO confirm
491: assertEquals(96, dt.getYearOfCentury());
492: assertEquals(1996, dt.getYearOfEra());
493:
494: assertEquals(1996, dt.getYear());
495: Property fld = dt.year();
496: assertEquals(false, fld.isLeap());
497: assertEquals(0, fld.getLeapAmount());
498: assertEquals(DurationFieldType.days(), fld
499: .getLeapDurationField().getType());
500: assertEquals(
501: new DateTime(1997, 10, 2, 0, 0, 0, 0, ETHIOPIC_UTC),
502: fld.addToCopy(1));
503:
504: assertEquals(10, dt.getMonthOfYear());
505: fld = dt.monthOfYear();
506: assertEquals(false, fld.isLeap());
507: assertEquals(0, fld.getLeapAmount());
508: assertEquals(DurationFieldType.days(), fld
509: .getLeapDurationField().getType());
510: assertEquals(1, fld.getMinimumValue());
511: assertEquals(1, fld.getMinimumValueOverall());
512: assertEquals(13, fld.getMaximumValue());
513: assertEquals(13, fld.getMaximumValueOverall());
514: assertEquals(
515: new DateTime(1997, 1, 2, 0, 0, 0, 0, ETHIOPIC_UTC), fld
516: .addToCopy(4));
517: assertEquals(
518: new DateTime(1996, 1, 2, 0, 0, 0, 0, ETHIOPIC_UTC), fld
519: .addWrapFieldToCopy(4));
520:
521: assertEquals(2, dt.getDayOfMonth());
522: fld = dt.dayOfMonth();
523: assertEquals(false, fld.isLeap());
524: assertEquals(0, fld.getLeapAmount());
525: assertEquals(null, fld.getLeapDurationField());
526: assertEquals(1, fld.getMinimumValue());
527: assertEquals(1, fld.getMinimumValueOverall());
528: assertEquals(30, fld.getMaximumValue());
529: assertEquals(30, fld.getMaximumValueOverall());
530: assertEquals(
531: new DateTime(1996, 10, 3, 0, 0, 0, 0, ETHIOPIC_UTC),
532: fld.addToCopy(1));
533:
534: assertEquals(DateTimeConstants.WEDNESDAY, dt.getDayOfWeek());
535: fld = dt.dayOfWeek();
536: assertEquals(false, fld.isLeap());
537: assertEquals(0, fld.getLeapAmount());
538: assertEquals(null, fld.getLeapDurationField());
539: assertEquals(1, fld.getMinimumValue());
540: assertEquals(1, fld.getMinimumValueOverall());
541: assertEquals(7, fld.getMaximumValue());
542: assertEquals(7, fld.getMaximumValueOverall());
543: assertEquals(
544: new DateTime(1996, 10, 3, 0, 0, 0, 0, ETHIOPIC_UTC),
545: fld.addToCopy(1));
546:
547: assertEquals(9 * 30 + 2, dt.getDayOfYear());
548: fld = dt.dayOfYear();
549: assertEquals(false, fld.isLeap());
550: assertEquals(0, fld.getLeapAmount());
551: assertEquals(null, fld.getLeapDurationField());
552: assertEquals(1, fld.getMinimumValue());
553: assertEquals(1, fld.getMinimumValueOverall());
554: assertEquals(365, fld.getMaximumValue());
555: assertEquals(366, fld.getMaximumValueOverall());
556: assertEquals(
557: new DateTime(1996, 10, 3, 0, 0, 0, 0, ETHIOPIC_UTC),
558: fld.addToCopy(1));
559:
560: assertEquals(0, dt.getHourOfDay());
561: assertEquals(0, dt.getMinuteOfHour());
562: assertEquals(0, dt.getSecondOfMinute());
563: assertEquals(0, dt.getMillisOfSecond());
564: }
565:
566: public void testSampleDateWithZone() {
567: DateTime dt = new DateTime(2004, 6, 9, 12, 0, 0, 0, PARIS)
568: .withChronology(ETHIOPIC_UTC);
569: assertEquals(EthiopicChronology.EE, dt.getEra());
570: assertEquals(1996, dt.getYear());
571: assertEquals(1996, dt.getYearOfEra());
572: assertEquals(10, dt.getMonthOfYear());
573: assertEquals(2, dt.getDayOfMonth());
574: assertEquals(10, dt.getHourOfDay()); // PARIS is UTC+2 in summer (12-2=10)
575: assertEquals(0, dt.getMinuteOfHour());
576: assertEquals(0, dt.getSecondOfMinute());
577: assertEquals(0, dt.getMillisOfSecond());
578: }
579:
580: public void testDurationYear() {
581: // Leap 1999, NotLeap 1996,97,98
582: DateTime dt96 = new DateTime(1996, 10, 2, 0, 0, 0, 0,
583: ETHIOPIC_UTC);
584: DateTime dt97 = new DateTime(1997, 10, 2, 0, 0, 0, 0,
585: ETHIOPIC_UTC);
586: DateTime dt98 = new DateTime(1998, 10, 2, 0, 0, 0, 0,
587: ETHIOPIC_UTC);
588: DateTime dt99 = new DateTime(1999, 10, 2, 0, 0, 0, 0,
589: ETHIOPIC_UTC);
590: DateTime dt00 = new DateTime(2000, 10, 2, 0, 0, 0, 0,
591: ETHIOPIC_UTC);
592:
593: DurationField fld = dt96.year().getDurationField();
594: assertEquals(ETHIOPIC_UTC.years(), fld);
595: assertEquals(1L * 365L * MILLIS_PER_DAY, fld.getMillis(1, dt96
596: .getMillis()));
597: assertEquals(2L * 365L * MILLIS_PER_DAY, fld.getMillis(2, dt96
598: .getMillis()));
599: assertEquals(3L * 365L * MILLIS_PER_DAY, fld.getMillis(3, dt96
600: .getMillis()));
601: assertEquals((4L * 365L + 1L) * MILLIS_PER_DAY, fld.getMillis(
602: 4, dt96.getMillis()));
603:
604: assertEquals(((4L * 365L + 1L) * MILLIS_PER_DAY) / 4, fld
605: .getMillis(1));
606: assertEquals(((4L * 365L + 1L) * MILLIS_PER_DAY) / 2, fld
607: .getMillis(2));
608:
609: assertEquals(1L * 365L * MILLIS_PER_DAY, fld.getMillis(1L, dt96
610: .getMillis()));
611: assertEquals(2L * 365L * MILLIS_PER_DAY, fld.getMillis(2L, dt96
612: .getMillis()));
613: assertEquals(3L * 365L * MILLIS_PER_DAY, fld.getMillis(3L, dt96
614: .getMillis()));
615: assertEquals((4L * 365L + 1L) * MILLIS_PER_DAY, fld.getMillis(
616: 4L, dt96.getMillis()));
617:
618: assertEquals(((4L * 365L + 1L) * MILLIS_PER_DAY) / 4, fld
619: .getMillis(1L));
620: assertEquals(((4L * 365L + 1L) * MILLIS_PER_DAY) / 2, fld
621: .getMillis(2L));
622:
623: assertEquals(((4L * 365L + 1L) * MILLIS_PER_DAY) / 4, fld
624: .getUnitMillis());
625:
626: assertEquals(0, fld.getValue(1L * 365L * MILLIS_PER_DAY - 1L,
627: dt96.getMillis()));
628: assertEquals(1, fld.getValue(1L * 365L * MILLIS_PER_DAY, dt96
629: .getMillis()));
630: assertEquals(1, fld.getValue(1L * 365L * MILLIS_PER_DAY + 1L,
631: dt96.getMillis()));
632: assertEquals(1, fld.getValue(2L * 365L * MILLIS_PER_DAY - 1L,
633: dt96.getMillis()));
634: assertEquals(2, fld.getValue(2L * 365L * MILLIS_PER_DAY, dt96
635: .getMillis()));
636: assertEquals(2, fld.getValue(2L * 365L * MILLIS_PER_DAY + 1L,
637: dt96.getMillis()));
638: assertEquals(2, fld.getValue(3L * 365L * MILLIS_PER_DAY - 1L,
639: dt96.getMillis()));
640: assertEquals(3, fld.getValue(3L * 365L * MILLIS_PER_DAY, dt96
641: .getMillis()));
642: assertEquals(3, fld.getValue(3L * 365L * MILLIS_PER_DAY + 1L,
643: dt96.getMillis()));
644: assertEquals(3, fld.getValue((4L * 365L + 1L) * MILLIS_PER_DAY
645: - 1L, dt96.getMillis()));
646: assertEquals(4, fld.getValue((4L * 365L + 1L) * MILLIS_PER_DAY,
647: dt96.getMillis()));
648: assertEquals(4, fld.getValue((4L * 365L + 1L) * MILLIS_PER_DAY
649: + 1L, dt96.getMillis()));
650:
651: assertEquals(dt97.getMillis(), fld.add(dt96.getMillis(), 1));
652: assertEquals(dt98.getMillis(), fld.add(dt96.getMillis(), 2));
653: assertEquals(dt99.getMillis(), fld.add(dt96.getMillis(), 3));
654: assertEquals(dt00.getMillis(), fld.add(dt96.getMillis(), 4));
655:
656: assertEquals(dt97.getMillis(), fld.add(dt96.getMillis(), 1L));
657: assertEquals(dt98.getMillis(), fld.add(dt96.getMillis(), 2L));
658: assertEquals(dt99.getMillis(), fld.add(dt96.getMillis(), 3L));
659: assertEquals(dt00.getMillis(), fld.add(dt96.getMillis(), 4L));
660: }
661:
662: public void testDurationMonth() {
663: // Leap 1999, NotLeap 1996,97,98
664: DateTime dt11 = new DateTime(1999, 11, 2, 0, 0, 0, 0,
665: ETHIOPIC_UTC);
666: DateTime dt12 = new DateTime(1999, 12, 2, 0, 0, 0, 0,
667: ETHIOPIC_UTC);
668: DateTime dt13 = new DateTime(1999, 13, 2, 0, 0, 0, 0,
669: ETHIOPIC_UTC);
670: DateTime dt01 = new DateTime(2000, 1, 2, 0, 0, 0, 0,
671: ETHIOPIC_UTC);
672:
673: DurationField fld = dt11.monthOfYear().getDurationField();
674: assertEquals(ETHIOPIC_UTC.months(), fld);
675: assertEquals(1L * 30L * MILLIS_PER_DAY, fld.getMillis(1, dt11
676: .getMillis()));
677: assertEquals(2L * 30L * MILLIS_PER_DAY, fld.getMillis(2, dt11
678: .getMillis()));
679: assertEquals((2L * 30L + 6L) * MILLIS_PER_DAY, fld.getMillis(3,
680: dt11.getMillis()));
681: assertEquals((3L * 30L + 6L) * MILLIS_PER_DAY, fld.getMillis(4,
682: dt11.getMillis()));
683:
684: assertEquals(1L * 30L * MILLIS_PER_DAY, fld.getMillis(1));
685: assertEquals(2L * 30L * MILLIS_PER_DAY, fld.getMillis(2));
686: assertEquals(13L * 30L * MILLIS_PER_DAY, fld.getMillis(13));
687:
688: assertEquals(1L * 30L * MILLIS_PER_DAY, fld.getMillis(1L, dt11
689: .getMillis()));
690: assertEquals(2L * 30L * MILLIS_PER_DAY, fld.getMillis(2L, dt11
691: .getMillis()));
692: assertEquals((2L * 30L + 6L) * MILLIS_PER_DAY, fld.getMillis(
693: 3L, dt11.getMillis()));
694: assertEquals((3L * 30L + 6L) * MILLIS_PER_DAY, fld.getMillis(
695: 4L, dt11.getMillis()));
696:
697: assertEquals(1L * 30L * MILLIS_PER_DAY, fld.getMillis(1L));
698: assertEquals(2L * 30L * MILLIS_PER_DAY, fld.getMillis(2L));
699: assertEquals(13L * 30L * MILLIS_PER_DAY, fld.getMillis(13L));
700:
701: assertEquals(0, fld.getValue(1L * 30L * MILLIS_PER_DAY - 1L,
702: dt11.getMillis()));
703: assertEquals(1, fld.getValue(1L * 30L * MILLIS_PER_DAY, dt11
704: .getMillis()));
705: assertEquals(1, fld.getValue(1L * 30L * MILLIS_PER_DAY + 1L,
706: dt11.getMillis()));
707: assertEquals(1, fld.getValue(2L * 30L * MILLIS_PER_DAY - 1L,
708: dt11.getMillis()));
709: assertEquals(2, fld.getValue(2L * 30L * MILLIS_PER_DAY, dt11
710: .getMillis()));
711: assertEquals(2, fld.getValue(2L * 30L * MILLIS_PER_DAY + 1L,
712: dt11.getMillis()));
713: assertEquals(2, fld.getValue((2L * 30L + 6L) * MILLIS_PER_DAY
714: - 1L, dt11.getMillis()));
715: assertEquals(3, fld.getValue((2L * 30L + 6L) * MILLIS_PER_DAY,
716: dt11.getMillis()));
717: assertEquals(3, fld.getValue((2L * 30L + 6L) * MILLIS_PER_DAY
718: + 1L, dt11.getMillis()));
719: assertEquals(3, fld.getValue((3L * 30L + 6L) * MILLIS_PER_DAY
720: - 1L, dt11.getMillis()));
721: assertEquals(4, fld.getValue((3L * 30L + 6L) * MILLIS_PER_DAY,
722: dt11.getMillis()));
723: assertEquals(4, fld.getValue((3L * 30L + 6L) * MILLIS_PER_DAY
724: + 1L, dt11.getMillis()));
725:
726: assertEquals(dt12.getMillis(), fld.add(dt11.getMillis(), 1));
727: assertEquals(dt13.getMillis(), fld.add(dt11.getMillis(), 2));
728: assertEquals(dt01.getMillis(), fld.add(dt11.getMillis(), 3));
729:
730: assertEquals(dt12.getMillis(), fld.add(dt11.getMillis(), 1L));
731: assertEquals(dt13.getMillis(), fld.add(dt11.getMillis(), 2L));
732: assertEquals(dt01.getMillis(), fld.add(dt11.getMillis(), 3L));
733: }
734:
735: }
|