KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > chrono > TestCopticChronology


1 /*
2  * Copyright 2001-2005 Stephen Colebourne
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.joda.time.chrono;
17
18 import java.util.Locale JavaDoc;
19 import java.util.TimeZone JavaDoc;
20
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23
24 import org.joda.time.Chronology;
25 import org.joda.time.DateTime;
26 import org.joda.time.DateTimeConstants;
27 import org.joda.time.DateTimeField;
28 import org.joda.time.DateTimeUtils;
29 import org.joda.time.DateTimeZone;
30 import org.joda.time.DurationField;
31 import org.joda.time.DurationFieldType;
32 import org.joda.time.DateTime.Property;
33
34 /**
35  * This class is a Junit unit test for CopticChronology.
36  *
37  * @author Stephen Colebourne
38  */

39 public class TestCopticChronology extends TestCase {
40
41     private static final int MILLIS_PER_DAY = DateTimeConstants.MILLIS_PER_DAY;
42
43     private static long SKIP = 1 * MILLIS_PER_DAY;
44
45     private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
46     private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
47     private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
48     private static final Chronology COPTIC_UTC = CopticChronology.getInstanceUTC();
49     private static final Chronology JULIAN_UTC = JulianChronology.getInstanceUTC();
50     private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC();
51
52     long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
53                      366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
54                      365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
55                      366 + 365;
56     // 2002-06-09
57
private long TEST_TIME_NOW =
58             (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * MILLIS_PER_DAY;
59
60     private DateTimeZone originalDateTimeZone = null;
61     private TimeZone JavaDoc originalTimeZone = null;
62     private Locale JavaDoc originalLocale = null;
63
64     public static void main(String JavaDoc[] args) {
65         junit.textui.TestRunner.run(suite());
66     }
67
68     public static TestSuite suite() {
69         SKIP = 1 * MILLIS_PER_DAY;
70         return new TestSuite(TestCopticChronology.class);
71     }
72
73     public TestCopticChronology(String JavaDoc name) {
74         super(name);
75     }
76
77     protected void setUp() throws Exception JavaDoc {
78         DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
79         originalDateTimeZone = DateTimeZone.getDefault();
80         originalTimeZone = TimeZone.getDefault();
81         originalLocale = Locale.getDefault();
82         DateTimeZone.setDefault(LONDON);
83         TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
84         Locale.setDefault(Locale.UK);
85     }
86
87     protected void tearDown() throws Exception JavaDoc {
88         DateTimeUtils.setCurrentMillisSystem();
89         DateTimeZone.setDefault(originalDateTimeZone);
90         TimeZone.setDefault(originalTimeZone);
91         Locale.setDefault(originalLocale);
92         originalDateTimeZone = null;
93         originalTimeZone = null;
94         originalLocale = null;
95     }
96
97     //-----------------------------------------------------------------------
98
public void testFactoryUTC() {
99         assertEquals(DateTimeZone.UTC, CopticChronology.getInstanceUTC().getZone());
100         assertSame(CopticChronology.class, CopticChronology.getInstanceUTC().getClass());
101     }
102
103     public void testFactory() {
104         assertEquals(LONDON, CopticChronology.getInstance().getZone());
105         assertSame(CopticChronology.class, CopticChronology.getInstance().getClass());
106     }
107
108     public void testFactory_Zone() {
109         assertEquals(TOKYO, CopticChronology.getInstance(TOKYO).getZone());
110         assertEquals(PARIS, CopticChronology.getInstance(PARIS).getZone());
111         assertEquals(LONDON, CopticChronology.getInstance(null).getZone());
112         assertSame(CopticChronology.class, CopticChronology.getInstance(TOKYO).getClass());
113     }
114
115     //-----------------------------------------------------------------------
116
public void testEquality() {
117         assertSame(CopticChronology.getInstance(TOKYO), CopticChronology.getInstance(TOKYO));
118         assertSame(CopticChronology.getInstance(LONDON), CopticChronology.getInstance(LONDON));
119         assertSame(CopticChronology.getInstance(PARIS), CopticChronology.getInstance(PARIS));
120         assertSame(CopticChronology.getInstanceUTC(), CopticChronology.getInstanceUTC());
121         assertSame(CopticChronology.getInstance(), CopticChronology.getInstance(LONDON));
122     }
123
124     public void testWithUTC() {
125         assertSame(CopticChronology.getInstanceUTC(), CopticChronology.getInstance(LONDON).withUTC());
126         assertSame(CopticChronology.getInstanceUTC(), CopticChronology.getInstance(TOKYO).withUTC());
127         assertSame(CopticChronology.getInstanceUTC(), CopticChronology.getInstanceUTC().withUTC());
128         assertSame(CopticChronology.getInstanceUTC(), CopticChronology.getInstance().withUTC());
129     }
130
131     public void testWithZone() {
132         assertSame(CopticChronology.getInstance(TOKYO), CopticChronology.getInstance(TOKYO).withZone(TOKYO));
133         assertSame(CopticChronology.getInstance(LONDON), CopticChronology.getInstance(TOKYO).withZone(LONDON));
134         assertSame(CopticChronology.getInstance(PARIS), CopticChronology.getInstance(TOKYO).withZone(PARIS));
135         assertSame(CopticChronology.getInstance(LONDON), CopticChronology.getInstance(TOKYO).withZone(null));
136         assertSame(CopticChronology.getInstance(PARIS), CopticChronology.getInstance().withZone(PARIS));
137         assertSame(CopticChronology.getInstance(PARIS), CopticChronology.getInstanceUTC().withZone(PARIS));
138     }
139
140     public void testToString() {
141         assertEquals("CopticChronology[Europe/London]", CopticChronology.getInstance(LONDON).toString());
142         assertEquals("CopticChronology[Asia/Tokyo]", CopticChronology.getInstance(TOKYO).toString());
143         assertEquals("CopticChronology[Europe/London]", CopticChronology.getInstance().toString());
144         assertEquals("CopticChronology[UTC]", CopticChronology.getInstanceUTC().toString());
145     }
146
147     //-----------------------------------------------------------------------
148
public void testDurationFields() {
149         assertEquals("eras", CopticChronology.getInstance().eras().getName());
150         assertEquals("centuries", CopticChronology.getInstance().centuries().getName());
151         assertEquals("years", CopticChronology.getInstance().years().getName());
152         assertEquals("weekyears", CopticChronology.getInstance().weekyears().getName());
153         assertEquals("months", CopticChronology.getInstance().months().getName());
154         assertEquals("weeks", CopticChronology.getInstance().weeks().getName());
155         assertEquals("days", CopticChronology.getInstance().days().getName());
156         assertEquals("halfdays", CopticChronology.getInstance().halfdays().getName());
157         assertEquals("hours", CopticChronology.getInstance().hours().getName());
158         assertEquals("minutes", CopticChronology.getInstance().minutes().getName());
159         assertEquals("seconds", CopticChronology.getInstance().seconds().getName());
160         assertEquals("millis", CopticChronology.getInstance().millis().getName());
161         
162         assertEquals(false, CopticChronology.getInstance().eras().isSupported());
163         assertEquals(true, CopticChronology.getInstance().centuries().isSupported());
164         assertEquals(true, CopticChronology.getInstance().years().isSupported());
165         assertEquals(true, CopticChronology.getInstance().weekyears().isSupported());
166         assertEquals(true, CopticChronology.getInstance().months().isSupported());
167         assertEquals(true, CopticChronology.getInstance().weeks().isSupported());
168         assertEquals(true, CopticChronology.getInstance().days().isSupported());
169         assertEquals(true, CopticChronology.getInstance().halfdays().isSupported());
170         assertEquals(true, CopticChronology.getInstance().hours().isSupported());
171         assertEquals(true, CopticChronology.getInstance().minutes().isSupported());
172         assertEquals(true, CopticChronology.getInstance().seconds().isSupported());
173         assertEquals(true, CopticChronology.getInstance().millis().isSupported());
174         
175         assertEquals(false, CopticChronology.getInstance().centuries().isPrecise());
176         assertEquals(false, CopticChronology.getInstance().years().isPrecise());
177         assertEquals(false, CopticChronology.getInstance().weekyears().isPrecise());
178         assertEquals(false, CopticChronology.getInstance().months().isPrecise());
179         assertEquals(false, CopticChronology.getInstance().weeks().isPrecise());
180         assertEquals(false, CopticChronology.getInstance().days().isPrecise());
181         assertEquals(false, CopticChronology.getInstance().halfdays().isPrecise());
182         assertEquals(true, CopticChronology.getInstance().hours().isPrecise());
183         assertEquals(true, CopticChronology.getInstance().minutes().isPrecise());
184         assertEquals(true, CopticChronology.getInstance().seconds().isPrecise());
185         assertEquals(true, CopticChronology.getInstance().millis().isPrecise());
186         
187         assertEquals(false, CopticChronology.getInstanceUTC().centuries().isPrecise());
188         assertEquals(false, CopticChronology.getInstanceUTC().years().isPrecise());
189         assertEquals(false, CopticChronology.getInstanceUTC().weekyears().isPrecise());
190         assertEquals(false, CopticChronology.getInstanceUTC().months().isPrecise());
191         assertEquals(true, CopticChronology.getInstanceUTC().weeks().isPrecise());
192         assertEquals(true, CopticChronology.getInstanceUTC().days().isPrecise());
193         assertEquals(true, CopticChronology.getInstanceUTC().halfdays().isPrecise());
194         assertEquals(true, CopticChronology.getInstanceUTC().hours().isPrecise());
195         assertEquals(true, CopticChronology.getInstanceUTC().minutes().isPrecise());
196         assertEquals(true, CopticChronology.getInstanceUTC().seconds().isPrecise());
197         assertEquals(true, CopticChronology.getInstanceUTC().millis().isPrecise());
198     }
199
200     public void testDateFields() {
201         assertEquals("era", CopticChronology.getInstance().era().getName());
202         assertEquals("centuryOfEra", CopticChronology.getInstance().centuryOfEra().getName());
203         assertEquals("yearOfCentury", CopticChronology.getInstance().yearOfCentury().getName());
204         assertEquals("yearOfEra", CopticChronology.getInstance().yearOfEra().getName());
205         assertEquals("year", CopticChronology.getInstance().year().getName());
206         assertEquals("monthOfYear", CopticChronology.getInstance().monthOfYear().getName());
207         assertEquals("weekyearOfCentury", CopticChronology.getInstance().weekyearOfCentury().getName());
208         assertEquals("weekyear", CopticChronology.getInstance().weekyear().getName());
209         assertEquals("weekOfWeekyear", CopticChronology.getInstance().weekOfWeekyear().getName());
210         assertEquals("dayOfYear", CopticChronology.getInstance().dayOfYear().getName());
211         assertEquals("dayOfMonth", CopticChronology.getInstance().dayOfMonth().getName());
212         assertEquals("dayOfWeek", CopticChronology.getInstance().dayOfWeek().getName());
213         
214         assertEquals(true, CopticChronology.getInstance().era().isSupported());
215         assertEquals(true, CopticChronology.getInstance().centuryOfEra().isSupported());
216         assertEquals(true, CopticChronology.getInstance().yearOfCentury().isSupported());
217         assertEquals(true, CopticChronology.getInstance().yearOfEra().isSupported());
218         assertEquals(true, CopticChronology.getInstance().year().isSupported());
219         assertEquals(true, CopticChronology.getInstance().monthOfYear().isSupported());
220         assertEquals(true, CopticChronology.getInstance().weekyearOfCentury().isSupported());
221         assertEquals(true, CopticChronology.getInstance().weekyear().isSupported());
222         assertEquals(true, CopticChronology.getInstance().weekOfWeekyear().isSupported());
223         assertEquals(true, CopticChronology.getInstance().dayOfYear().isSupported());
224         assertEquals(true, CopticChronology.getInstance().dayOfMonth().isSupported());
225         assertEquals(true, CopticChronology.getInstance().dayOfWeek().isSupported());
226     }
227
228     public void testTimeFields() {
229         assertEquals("halfdayOfDay", CopticChronology.getInstance().halfdayOfDay().getName());
230         assertEquals("clockhourOfHalfday", CopticChronology.getInstance().clockhourOfHalfday().getName());
231         assertEquals("hourOfHalfday", CopticChronology.getInstance().hourOfHalfday().getName());
232         assertEquals("clockhourOfDay", CopticChronology.getInstance().clockhourOfDay().getName());
233         assertEquals("hourOfDay", CopticChronology.getInstance().hourOfDay().getName());
234         assertEquals("minuteOfDay", CopticChronology.getInstance().minuteOfDay().getName());
235         assertEquals("minuteOfHour", CopticChronology.getInstance().minuteOfHour().getName());
236         assertEquals("secondOfDay", CopticChronology.getInstance().secondOfDay().getName());
237         assertEquals("secondOfMinute", CopticChronology.getInstance().secondOfMinute().getName());
238         assertEquals("millisOfDay", CopticChronology.getInstance().millisOfDay().getName());
239         assertEquals("millisOfSecond", CopticChronology.getInstance().millisOfSecond().getName());
240         
241         assertEquals(true, CopticChronology.getInstance().halfdayOfDay().isSupported());
242         assertEquals(true, CopticChronology.getInstance().clockhourOfHalfday().isSupported());
243         assertEquals(true, CopticChronology.getInstance().hourOfHalfday().isSupported());
244         assertEquals(true, CopticChronology.getInstance().clockhourOfDay().isSupported());
245         assertEquals(true, CopticChronology.getInstance().hourOfDay().isSupported());
246         assertEquals(true, CopticChronology.getInstance().minuteOfDay().isSupported());
247         assertEquals(true, CopticChronology.getInstance().minuteOfHour().isSupported());
248         assertEquals(true, CopticChronology.getInstance().secondOfDay().isSupported());
249         assertEquals(true, CopticChronology.getInstance().secondOfMinute().isSupported());
250         assertEquals(true, CopticChronology.getInstance().millisOfDay().isSupported());
251         assertEquals(true, CopticChronology.getInstance().millisOfSecond().isSupported());
252     }
253
254     //-----------------------------------------------------------------------
255
public void testEpoch() {
256         DateTime epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, COPTIC_UTC);
257         assertEquals(new DateTime(284, 8, 29, 0, 0, 0, 0, JULIAN_UTC), epoch.withChronology(JULIAN_UTC));
258     }
259
260     public void testEra() {
261         assertEquals(1, CopticChronology.AM);
262         try {
263             new DateTime(-1, 13, 5, 0, 0, 0, 0, COPTIC_UTC);
264             fail();
265         } catch (IllegalArgumentException JavaDoc ex) {}
266     }
267
268     //-----------------------------------------------------------------------
269
/**
270      * Tests era, year, monthOfYear, dayOfMonth and dayOfWeek.
271      */

272     public void testCalendar() {
273         if (TestAll.FAST) {
274             return;
275         }
276         System.out.println("\nTestCopticChronology.testCalendar");
277         DateTime epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, COPTIC_UTC);
278         long millis = epoch.getMillis();
279         long end = new DateTime(3000, 1, 1, 0, 0, 0, 0, ISO_UTC).getMillis();
280         DateTimeField dayOfWeek = COPTIC_UTC.dayOfWeek();
281         DateTimeField dayOfYear = COPTIC_UTC.dayOfYear();
282         DateTimeField dayOfMonth = COPTIC_UTC.dayOfMonth();
283         DateTimeField monthOfYear = COPTIC_UTC.monthOfYear();
284         DateTimeField year = COPTIC_UTC.year();
285         DateTimeField yearOfEra = COPTIC_UTC.yearOfEra();
286         DateTimeField era = COPTIC_UTC.era();
287         int expectedDOW = new DateTime(284, 8, 29, 0, 0, 0, 0, JULIAN_UTC).getDayOfWeek();
288         int expectedDOY = 1;
289         int expectedDay = 1;
290         int expectedMonth = 1;
291         int expectedYear = 1;
292         while (millis < end) {
293             int dowValue = dayOfWeek.get(millis);
294             int doyValue = dayOfYear.get(millis);
295             int dayValue = dayOfMonth.get(millis);
296             int monthValue = monthOfYear.get(millis);
297             int yearValue = year.get(millis);
298             int yearOfEraValue = yearOfEra.get(millis);
299             int monthLen = dayOfMonth.getMaximumValue(millis);
300             if (monthValue < 1 || monthValue > 13) {
301                 fail("Bad month: " + millis);
302             }
303             
304             // test era
305
assertEquals(1, era.get(millis));
306             assertEquals("AM", era.getAsText(millis));
307             assertEquals("AM", era.getAsShortText(millis));
308             
309             // test date
310
assertEquals(expectedYear, yearValue);
311             assertEquals(expectedYear, yearOfEraValue);
312             assertEquals(expectedMonth, monthValue);
313             assertEquals(expectedDay, dayValue);
314             assertEquals(expectedDOW, dowValue);
315             assertEquals(expectedDOY, doyValue);
316             
317             // test leap year
318
assertEquals(yearValue % 4 == 3, year.isLeap(millis));
319             
320             // test month length
321
if (monthValue == 13) {
322                 assertEquals(yearValue % 4 == 3, monthOfYear.isLeap(millis));
323                 if (yearValue % 4 == 3) {
324                     assertEquals(6, monthLen);
325                 } else {
326                     assertEquals(5, monthLen);
327                 }
328             } else {
329                 assertEquals(30, monthLen);
330             }
331             
332             // recalculate date
333
expectedDOW = (((expectedDOW + 1) - 1) % 7) + 1;
334             expectedDay++;
335             expectedDOY++;
336             if (expectedDay == 31 && expectedMonth < 13) {
337                 expectedDay = 1;
338                 expectedMonth++;
339             } else if (expectedMonth == 13) {
340                 if (expectedYear % 4 == 3 && expectedDay == 7) {
341                     expectedDay = 1;
342                     expectedMonth = 1;
343                     expectedYear++;
344                     expectedDOY = 1;
345                 } else if (expectedYear % 4 != 3 && expectedDay == 6) {
346                     expectedDay = 1;
347                     expectedMonth = 1;
348                     expectedYear++;
349                     expectedDOY = 1;
350                 }
351             }
352             millis += SKIP;
353         }
354     }
355
356     public void testSampleDate() {
357         DateTime dt = new DateTime(2004, 6, 9, 0, 0, 0, 0, ISO_UTC).withChronology(COPTIC_UTC);
358         assertEquals(CopticChronology.AM, dt.getEra());
359         assertEquals(18, dt.getCenturyOfEra()); // TODO confirm
360
assertEquals(20, dt.getYearOfCentury());
361         assertEquals(1720, dt.getYearOfEra());
362         
363         assertEquals(1720, dt.getYear());
364         Property fld = dt.year();
365         assertEquals(false, fld.isLeap());
366         assertEquals(0, fld.getLeapAmount());
367         assertEquals(DurationFieldType.days(), fld.getLeapDurationField().getType());
368         assertEquals(new DateTime(1721, 10, 2, 0, 0, 0, 0, COPTIC_UTC), fld.addToCopy(1));
369         
370         assertEquals(10, dt.getMonthOfYear());
371         fld = dt.monthOfYear();
372         assertEquals(false, fld.isLeap());
373         assertEquals(0, fld.getLeapAmount());
374         assertEquals(DurationFieldType.days(), fld.getLeapDurationField().getType());
375         assertEquals(1, fld.getMinimumValue());
376         assertEquals(1, fld.getMinimumValueOverall());
377         assertEquals(13, fld.getMaximumValue());
378         assertEquals(13, fld.getMaximumValueOverall());
379         assertEquals(new DateTime(1721, 1, 2, 0, 0, 0, 0, COPTIC_UTC), fld.addToCopy(4));
380         assertEquals(new DateTime(1720, 1, 2, 0, 0, 0, 0, COPTIC_UTC), fld.addWrapFieldToCopy(4));
381         
382         assertEquals(2, dt.getDayOfMonth());
383         fld = dt.dayOfMonth();
384         assertEquals(false, fld.isLeap());
385         assertEquals(0, fld.getLeapAmount());
386         assertEquals(null, fld.getLeapDurationField());
387         assertEquals(1, fld.getMinimumValue());
388         assertEquals(1, fld.getMinimumValueOverall());
389         assertEquals(30, fld.getMaximumValue());
390         assertEquals(30, fld.getMaximumValueOverall());
391         assertEquals(new DateTime(1720, 10, 3, 0, 0, 0, 0, COPTIC_UTC), fld.addToCopy(1));
392         
393         assertEquals(DateTimeConstants.WEDNESDAY, dt.getDayOfWeek());
394         fld = dt.dayOfWeek();
395         assertEquals(false, fld.isLeap());
396         assertEquals(0, fld.getLeapAmount());
397         assertEquals(null, fld.getLeapDurationField());
398         assertEquals(1, fld.getMinimumValue());
399         assertEquals(1, fld.getMinimumValueOverall());
400         assertEquals(7, fld.getMaximumValue());
401         assertEquals(7, fld.getMaximumValueOverall());
402         assertEquals(new DateTime(1720, 10, 3, 0, 0, 0, 0, COPTIC_UTC), fld.addToCopy(1));
403         
404         assertEquals(9 * 30 + 2, dt.getDayOfYear());
405         fld = dt.dayOfYear();
406         assertEquals(false, fld.isLeap());
407         assertEquals(0, fld.getLeapAmount());
408         assertEquals(null, fld.getLeapDurationField());
409         assertEquals(1, fld.getMinimumValue());
410         assertEquals(1, fld.getMinimumValueOverall());
411         assertEquals(365, fld.getMaximumValue());
412         assertEquals(366, fld.getMaximumValueOverall());
413         assertEquals(new DateTime(1720, 10, 3, 0, 0, 0, 0, COPTIC_UTC), fld.addToCopy(1));
414         
415         assertEquals(0, dt.getHourOfDay());
416         assertEquals(0, dt.getMinuteOfHour());
417         assertEquals(0, dt.getSecondOfMinute());
418         assertEquals(0, dt.getMillisOfSecond());
419     }
420
421     public void testSampleDateWithZone() {
422         DateTime dt = new DateTime(2004, 6, 9, 12, 0, 0, 0, PARIS).withChronology(COPTIC_UTC);
423         assertEquals(CopticChronology.AM, dt.getEra());
424         assertEquals(1720, dt.getYear());
425         assertEquals(1720, dt.getYearOfEra());
426         assertEquals(10, dt.getMonthOfYear());
427         assertEquals(2, dt.getDayOfMonth());
428         assertEquals(10, dt.getHourOfDay()); // PARIS is UTC+2 in summer (12-2=10)
429
assertEquals(0, dt.getMinuteOfHour());
430         assertEquals(0, dt.getSecondOfMinute());
431         assertEquals(0, dt.getMillisOfSecond());
432     }
433
434     public void testDurationYear() {
435         // Leap 1723
436
DateTime dt20 = new DateTime(1720, 10, 2, 0, 0, 0, 0, COPTIC_UTC);
437         DateTime dt21 = new DateTime(1721, 10, 2, 0, 0, 0, 0, COPTIC_UTC);
438         DateTime dt22 = new DateTime(1722, 10, 2, 0, 0, 0, 0, COPTIC_UTC);
439         DateTime dt23 = new DateTime(1723, 10, 2, 0, 0, 0, 0, COPTIC_UTC);
440         DateTime dt24 = new DateTime(1724, 10, 2, 0, 0, 0, 0, COPTIC_UTC);
441         
442         DurationField fld = dt20.year().getDurationField();
443         assertEquals(COPTIC_UTC.years(), fld);
444         assertEquals(1L * 365L * MILLIS_PER_DAY, fld.getMillis(1, dt20.getMillis()));
445         assertEquals(2L * 365L * MILLIS_PER_DAY, fld.getMillis(2, dt20.getMillis()));
446         assertEquals(3L * 365L * MILLIS_PER_DAY, fld.getMillis(3, dt20.getMillis()));
447         assertEquals((4L * 365L + 1L) * MILLIS_PER_DAY, fld.getMillis(4, dt20.getMillis()));
448         
449         assertEquals(((4L * 365L + 1L) * MILLIS_PER_DAY) / 4, fld.getMillis(1));
450         assertEquals(((4L * 365L + 1L) * MILLIS_PER_DAY) / 2, fld.getMillis(2));
451         
452         assertEquals(1L * 365L * MILLIS_PER_DAY, fld.getMillis(1L, dt20.getMillis()));
453         assertEquals(2L * 365L * MILLIS_PER_DAY, fld.getMillis(2L, dt20.getMillis()));
454         assertEquals(3L * 365L * MILLIS_PER_DAY, fld.getMillis(3L, dt20.getMillis()));
455         assertEquals((4L * 365L + 1L) * MILLIS_PER_DAY, fld.getMillis(4L, dt20.getMillis()));
456         
457         assertEquals(((4L * 365L + 1L) * MILLIS_PER_DAY) / 4, fld.getMillis(1L));
458         assertEquals(((4L * 365L + 1L) * MILLIS_PER_DAY) / 2, fld.getMillis(2L));
459         
460         assertEquals(((4L * 365L + 1L) * MILLIS_PER_DAY) / 4, fld.getUnitMillis());
461         
462         assertEquals(0, fld.getValue(1L * 365L * MILLIS_PER_DAY - 1L, dt20.getMillis()));
463         assertEquals(1, fld.getValue(1L * 365L * MILLIS_PER_DAY, dt20.getMillis()));
464         assertEquals(1, fld.getValue(1L * 365L * MILLIS_PER_DAY + 1L, dt20.getMillis()));
465         assertEquals(1, fld.getValue(2L * 365L * MILLIS_PER_DAY - 1L, dt20.getMillis()));
466         assertEquals(2, fld.getValue(2L * 365L * MILLIS_PER_DAY, dt20.getMillis()));
467         assertEquals(2, fld.getValue(2L * 365L * MILLIS_PER_DAY + 1L, dt20.getMillis()));
468         assertEquals(2, fld.getValue(3L * 365L * MILLIS_PER_DAY - 1L, dt20.getMillis()));
469         assertEquals(3, fld.getValue(3L * 365L * MILLIS_PER_DAY, dt20.getMillis()));
470         assertEquals(3, fld.getValue(3L * 365L * MILLIS_PER_DAY + 1L, dt20.getMillis()));
471         assertEquals(3, fld.getValue((4L * 365L + 1L) * MILLIS_PER_DAY - 1L, dt20.getMillis()));
472         assertEquals(4, fld.getValue((4L * 365L + 1L) * MILLIS_PER_DAY, dt20.getMillis()));
473         assertEquals(4, fld.getValue((4L * 365L + 1L) * MILLIS_PER_DAY + 1L, dt20.getMillis()));
474         
475         assertEquals(dt21.getMillis(), fld.add(dt20.getMillis(), 1));
476         assertEquals(dt22.getMillis(), fld.add(dt20.getMillis(), 2));
477         assertEquals(dt23.getMillis(), fld.add(dt20.getMillis(), 3));
478         assertEquals(dt24.getMillis(), fld.add(dt20.getMillis(), 4));
479         
480         assertEquals(dt21.getMillis(), fld.add(dt20.getMillis(), 1L));
481         assertEquals(dt22.getMillis(), fld.add(dt20.getMillis(), 2L));
482         assertEquals(dt23.getMillis(), fld.add(dt20.getMillis(), 3L));
483         assertEquals(dt24.getMillis(), fld.add(dt20.getMillis(), 4L));
484     }
485
486     public void testDurationMonth() {
487         // Leap 1723
488
DateTime dt11 = new DateTime(1723, 11, 2, 0, 0, 0, 0, COPTIC_UTC);
489         DateTime dt12 = new DateTime(1723, 12, 2, 0, 0, 0, 0, COPTIC_UTC);
490         DateTime dt13 = new DateTime(1723, 13, 2, 0, 0, 0, 0, COPTIC_UTC);
491         DateTime dt01 = new DateTime(1724, 1, 2, 0, 0, 0, 0, COPTIC_UTC);
492         
493         DurationField fld = dt11.monthOfYear().getDurationField();
494         assertEquals(COPTIC_UTC.months(), fld);
495         assertEquals(1L * 30L * MILLIS_PER_DAY, fld.getMillis(1, dt11.getMillis()));
496         assertEquals(2L * 30L * MILLIS_PER_DAY, fld.getMillis(2, dt11.getMillis()));
497         assertEquals((2L * 30L + 6L) * MILLIS_PER_DAY, fld.getMillis(3, dt11.getMillis()));
498         assertEquals((3L * 30L + 6L) * MILLIS_PER_DAY, fld.getMillis(4, dt11.getMillis()));
499         
500         assertEquals(1L * 30L * MILLIS_PER_DAY, fld.getMillis(1));
501         assertEquals(2L * 30L * MILLIS_PER_DAY, fld.getMillis(2));
502         assertEquals(13L * 30L * MILLIS_PER_DAY, fld.getMillis(13));
503         
504         assertEquals(1L * 30L * MILLIS_PER_DAY, fld.getMillis(1L, dt11.getMillis()));
505         assertEquals(2L * 30L * MILLIS_PER_DAY, fld.getMillis(2L, dt11.getMillis()));
506         assertEquals((2L * 30L + 6L) * MILLIS_PER_DAY, fld.getMillis(3L, dt11.getMillis()));
507         assertEquals((3L * 30L + 6L) * MILLIS_PER_DAY, fld.getMillis(4L, dt11.getMillis()));
508         
509         assertEquals(1L * 30L * MILLIS_PER_DAY, fld.getMillis(1L));
510         assertEquals(2L * 30L * MILLIS_PER_DAY, fld.getMillis(2L));
511         assertEquals(13L * 30L * MILLIS_PER_DAY, fld.getMillis(13L));
512         
513         assertEquals(0, fld.getValue(1L * 30L * MILLIS_PER_DAY - 1L, dt11.getMillis()));
514         assertEquals(1, fld.getValue(1L * 30L * MILLIS_PER_DAY, dt11.getMillis()));
515         assertEquals(1, fld.getValue(1L * 30L * MILLIS_PER_DAY + 1L, dt11.getMillis()));
516         assertEquals(1, fld.getValue(2L * 30L * MILLIS_PER_DAY - 1L, dt11.getMillis()));
517         assertEquals(2, fld.getValue(2L * 30L * MILLIS_PER_DAY, dt11.getMillis()));
518         assertEquals(2, fld.getValue(2L * 30L * MILLIS_PER_DAY + 1L, dt11.getMillis()));
519         assertEquals(2, fld.getValue((2L * 30L + 6L) * MILLIS_PER_DAY - 1L, dt11.getMillis()));
520         assertEquals(3, fld.getValue((2L * 30L + 6L) * MILLIS_PER_DAY, dt11.getMillis()));
521         assertEquals(3, fld.getValue((2L * 30L + 6L) * MILLIS_PER_DAY + 1L, dt11.getMillis()));
522         assertEquals(3, fld.getValue((3L * 30L + 6L) * MILLIS_PER_DAY - 1L, dt11.getMillis()));
523         assertEquals(4, fld.getValue((3L * 30L + 6L) * MILLIS_PER_DAY, dt11.getMillis()));
524         assertEquals(4, fld.getValue((3L * 30L + 6L) * MILLIS_PER_DAY + 1L, dt11.getMillis()));
525         
526         assertEquals(dt12.getMillis(), fld.add(dt11.getMillis(), 1));
527         assertEquals(dt13.getMillis(), fld.add(dt11.getMillis(), 2));
528         assertEquals(dt01.getMillis(), fld.add(dt11.getMillis(), 3));
529         
530         assertEquals(dt12.getMillis(), fld.add(dt11.getMillis(), 1L));
531         assertEquals(dt13.getMillis(), fld.add(dt11.getMillis(), 2L));
532         assertEquals(dt01.getMillis(), fld.add(dt11.getMillis(), 3L));
533     }
534
535 }
536
Popular Tags