KickJava   Java API By Example, From Geeks To Geeks.

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


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 EthiopicChronology.
36  *
37  * @author Stephen Colebourne
38  */

39 public class TestEthiopicChronology 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 ETHIOPIC_UTC = EthiopicChronology.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(TestEthiopicChronology.class);
71     }
72
73     public TestEthiopicChronology(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, EthiopicChronology.getInstanceUTC().getZone());
100         assertSame(EthiopicChronology.class, EthiopicChronology.getInstanceUTC().getClass());
101     }
102
103     public void testFactory() {
104         assertEquals(LONDON, EthiopicChronology.getInstance().getZone());
105         assertSame(EthiopicChronology.class, EthiopicChronology.getInstance().getClass());
106     }
107
108     public void testFactory_Zone() {
109         assertEquals(TOKYO, EthiopicChronology.getInstance(TOKYO).getZone());
110         assertEquals(PARIS, EthiopicChronology.getInstance(PARIS).getZone());
111         assertEquals(LONDON, EthiopicChronology.getInstance(null).getZone());
112         assertSame(EthiopicChronology.class, EthiopicChronology.getInstance(TOKYO).getClass());
113     }
114
115     //-----------------------------------------------------------------------
116
public void testEquality() {
117         assertSame(EthiopicChronology.getInstance(TOKYO), EthiopicChronology.getInstance(TOKYO));
118         assertSame(EthiopicChronology.getInstance(LONDON), EthiopicChronology.getInstance(LONDON));
119         assertSame(EthiopicChronology.getInstance(PARIS), EthiopicChronology.getInstance(PARIS));
120         assertSame(EthiopicChronology.getInstanceUTC(), EthiopicChronology.getInstanceUTC());
121         assertSame(EthiopicChronology.getInstance(), EthiopicChronology.getInstance(LONDON));
122     }
123
124     public void testWithUTC() {
125         assertSame(EthiopicChronology.getInstanceUTC(), EthiopicChronology.getInstance(LONDON).withUTC());
126         assertSame(EthiopicChronology.getInstanceUTC(), EthiopicChronology.getInstance(TOKYO).withUTC());
127         assertSame(EthiopicChronology.getInstanceUTC(), EthiopicChronology.getInstanceUTC().withUTC());
128         assertSame(EthiopicChronology.getInstanceUTC(), EthiopicChronology.getInstance().withUTC());
129     }
130
131     public void testWithZone() {
132         assertSame(EthiopicChronology.getInstance(TOKYO), EthiopicChronology.getInstance(TOKYO).withZone(TOKYO));
133         assertSame(EthiopicChronology.getInstance(LONDON), EthiopicChronology.getInstance(TOKYO).withZone(LONDON));
134         assertSame(EthiopicChronology.getInstance(PARIS), EthiopicChronology.getInstance(TOKYO).withZone(PARIS));
135         assertSame(EthiopicChronology.getInstance(LONDON), EthiopicChronology.getInstance(TOKYO).withZone(null));
136         assertSame(EthiopicChronology.getInstance(PARIS), EthiopicChronology.getInstance().withZone(PARIS));
137         assertSame(EthiopicChronology.getInstance(PARIS), EthiopicChronology.getInstanceUTC().withZone(PARIS));
138     }
139
140     public void testToString() {
141         assertEquals("EthiopicChronology[Europe/London]", EthiopicChronology.getInstance(LONDON).toString());
142         assertEquals("EthiopicChronology[Asia/Tokyo]", EthiopicChronology.getInstance(TOKYO).toString());
143         assertEquals("EthiopicChronology[Europe/London]", EthiopicChronology.getInstance().toString());
144         assertEquals("EthiopicChronology[UTC]", EthiopicChronology.getInstanceUTC().toString());
145     }
146
147     //-----------------------------------------------------------------------
148
public void testDurationFields() {
149         assertEquals("eras", EthiopicChronology.getInstance().eras().getName());
150         assertEquals("centuries", EthiopicChronology.getInstance().centuries().getName());
151         assertEquals("years", EthiopicChronology.getInstance().years().getName());
152         assertEquals("weekyears", EthiopicChronology.getInstance().weekyears().getName());
153         assertEquals("months", EthiopicChronology.getInstance().months().getName());
154         assertEquals("weeks", EthiopicChronology.getInstance().weeks().getName());
155         assertEquals("days", EthiopicChronology.getInstance().days().getName());
156         assertEquals("halfdays", EthiopicChronology.getInstance().halfdays().getName());
157         assertEquals("hours", EthiopicChronology.getInstance().hours().getName());
158         assertEquals("minutes", EthiopicChronology.getInstance().minutes().getName());
159         assertEquals("seconds", EthiopicChronology.getInstance().seconds().getName());
160         assertEquals("millis", EthiopicChronology.getInstance().millis().getName());
161         
162         assertEquals(false, EthiopicChronology.getInstance().eras().isSupported());
163         assertEquals(true, EthiopicChronology.getInstance().centuries().isSupported());
164         assertEquals(true, EthiopicChronology.getInstance().years().isSupported());
165         assertEquals(true, EthiopicChronology.getInstance().weekyears().isSupported());
166         assertEquals(true, EthiopicChronology.getInstance().months().isSupported());
167         assertEquals(true, EthiopicChronology.getInstance().weeks().isSupported());
168         assertEquals(true, EthiopicChronology.getInstance().days().isSupported());
169         assertEquals(true, EthiopicChronology.getInstance().halfdays().isSupported());
170         assertEquals(true, EthiopicChronology.getInstance().hours().isSupported());
171         assertEquals(true, EthiopicChronology.getInstance().minutes().isSupported());
172         assertEquals(true, EthiopicChronology.getInstance().seconds().isSupported());
173         assertEquals(true, EthiopicChronology.getInstance().millis().isSupported());
174         
175         assertEquals(false, EthiopicChronology.getInstance().centuries().isPrecise());
176         assertEquals(false, EthiopicChronology.getInstance().years().isPrecise());
177         assertEquals(false, EthiopicChronology.getInstance().weekyears().isPrecise());
178         assertEquals(false, EthiopicChronology.getInstance().months().isPrecise());
179         assertEquals(false, EthiopicChronology.getInstance().weeks().isPrecise());
180         assertEquals(false, EthiopicChronology.getInstance().days().isPrecise());
181         assertEquals(false, EthiopicChronology.getInstance().halfdays().isPrecise());
182         assertEquals(true, EthiopicChronology.getInstance().hours().isPrecise());
183         assertEquals(true, EthiopicChronology.getInstance().minutes().isPrecise());
184         assertEquals(true, EthiopicChronology.getInstance().seconds().isPrecise());
185         assertEquals(true, EthiopicChronology.getInstance().millis().isPrecise());
186         
187         assertEquals(false, EthiopicChronology.getInstanceUTC().centuries().isPrecise());
188         assertEquals(false, EthiopicChronology.getInstanceUTC().years().isPrecise());
189         assertEquals(false, EthiopicChronology.getInstanceUTC().weekyears().isPrecise());
190         assertEquals(false, EthiopicChronology.getInstanceUTC().months().isPrecise());
191         assertEquals(true, EthiopicChronology.getInstanceUTC().weeks().isPrecise());
192         assertEquals(true, EthiopicChronology.getInstanceUTC().days().isPrecise());
193         assertEquals(true, EthiopicChronology.getInstanceUTC().halfdays().isPrecise());
194         assertEquals(true, EthiopicChronology.getInstanceUTC().hours().isPrecise());
195         assertEquals(true, EthiopicChronology.getInstanceUTC().minutes().isPrecise());
196         assertEquals(true, EthiopicChronology.getInstanceUTC().seconds().isPrecise());
197         assertEquals(true, EthiopicChronology.getInstanceUTC().millis().isPrecise());
198     }
199
200     public void testDateFields() {
201         assertEquals("era", EthiopicChronology.getInstance().era().getName());
202         assertEquals("centuryOfEra", EthiopicChronology.getInstance().centuryOfEra().getName());
203         assertEquals("yearOfCentury", EthiopicChronology.getInstance().yearOfCentury().getName());
204         assertEquals("yearOfEra", EthiopicChronology.getInstance().yearOfEra().getName());
205         assertEquals("year", EthiopicChronology.getInstance().year().getName());
206         assertEquals("monthOfYear", EthiopicChronology.getInstance().monthOfYear().getName());
207         assertEquals("weekyearOfCentury", EthiopicChronology.getInstance().weekyearOfCentury().getName());
208         assertEquals("weekyear", EthiopicChronology.getInstance().weekyear().getName());
209         assertEquals("weekOfWeekyear", EthiopicChronology.getInstance().weekOfWeekyear().getName());
210         assertEquals("dayOfYear", EthiopicChronology.getInstance().dayOfYear().getName());
211         assertEquals("dayOfMonth", EthiopicChronology.getInstance().dayOfMonth().getName());
212         assertEquals("dayOfWeek", EthiopicChronology.getInstance().dayOfWeek().getName());
213         
214         assertEquals(true, EthiopicChronology.getInstance().era().isSupported());
215         assertEquals(true, EthiopicChronology.getInstance().centuryOfEra().isSupported());
216         assertEquals(true, EthiopicChronology.getInstance().yearOfCentury().isSupported());
217         assertEquals(true, EthiopicChronology.getInstance().yearOfEra().isSupported());
218         assertEquals(true, EthiopicChronology.getInstance().year().isSupported());
219         assertEquals(true, EthiopicChronology.getInstance().monthOfYear().isSupported());
220         assertEquals(true, EthiopicChronology.getInstance().weekyearOfCentury().isSupported());
221         assertEquals(true, EthiopicChronology.getInstance().weekyear().isSupported());
222         assertEquals(true, EthiopicChronology.getInstance().weekOfWeekyear().isSupported());
223         assertEquals(true, EthiopicChronology.getInstance().dayOfYear().isSupported());
224         assertEquals(true, EthiopicChronology.getInstance().dayOfMonth().isSupported());
225         assertEquals(true, EthiopicChronology.getInstance().dayOfWeek().isSupported());
226     }
227
228     public void testTimeFields() {
229         assertEquals("halfdayOfDay", EthiopicChronology.getInstance().halfdayOfDay().getName());
230         assertEquals("clockhourOfHalfday", EthiopicChronology.getInstance().clockhourOfHalfday().getName());
231         assertEquals("hourOfHalfday", EthiopicChronology.getInstance().hourOfHalfday().getName());
232         assertEquals("clockhourOfDay", EthiopicChronology.getInstance().clockhourOfDay().getName());
233         assertEquals("hourOfDay", EthiopicChronology.getInstance().hourOfDay().getName());
234         assertEquals("minuteOfDay", EthiopicChronology.getInstance().minuteOfDay().getName());
235         assertEquals("minuteOfHour", EthiopicChronology.getInstance().minuteOfHour().getName());
236         assertEquals("secondOfDay", EthiopicChronology.getInstance().secondOfDay().getName());
237         assertEquals("secondOfMinute", EthiopicChronology.getInstance().secondOfMinute().getName());
238         assertEquals("millisOfDay", EthiopicChronology.getInstance().millisOfDay().getName());
239         assertEquals("millisOfSecond", EthiopicChronology.getInstance().millisOfSecond().getName());
240         
241         assertEquals(true, EthiopicChronology.getInstance().halfdayOfDay().isSupported());
242         assertEquals(true, EthiopicChronology.getInstance().clockhourOfHalfday().isSupported());
243         assertEquals(true, EthiopicChronology.getInstance().hourOfHalfday().isSupported());
244         assertEquals(true, EthiopicChronology.getInstance().clockhourOfDay().isSupported());
245         assertEquals(true, EthiopicChronology.getInstance().hourOfDay().isSupported());
246         assertEquals(true, EthiopicChronology.getInstance().minuteOfDay().isSupported());
247         assertEquals(true, EthiopicChronology.getInstance().minuteOfHour().isSupported());
248         assertEquals(true, EthiopicChronology.getInstance().secondOfDay().isSupported());
249         assertEquals(true, EthiopicChronology.getInstance().secondOfMinute().isSupported());
250         assertEquals(true, EthiopicChronology.getInstance().millisOfDay().isSupported());
251         assertEquals(true, EthiopicChronology.getInstance().millisOfSecond().isSupported());
252     }
253
254     //-----------------------------------------------------------------------
255
public void testEpoch() {
256         DateTime epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, ETHIOPIC_UTC);
257         assertEquals(new DateTime(8, 8, 29, 0, 0, 0, 0, JULIAN_UTC), epoch.withChronology(JULIAN_UTC));
258     }
259
260     public void testEra() {
261         assertEquals(1, EthiopicChronology.EE);
262         try {
263             new DateTime(-1, 13, 5, 0, 0, 0, 0, ETHIOPIC_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("\nTestEthiopicChronology.testCalendar");
277         DateTime epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, ETHIOPIC_UTC);
278         long millis = epoch.getMillis();
279         long end = new DateTime(3000, 1, 1, 0, 0, 0, 0, ISO_UTC).getMillis();
280         DateTimeField dayOfWeek = ETHIOPIC_UTC.dayOfWeek();
281         DateTimeField dayOfYear = ETHIOPIC_UTC.dayOfYear();
282         DateTimeField dayOfMonth = ETHIOPIC_UTC.dayOfMonth();
283         DateTimeField monthOfYear = ETHIOPIC_UTC.monthOfYear();
284         DateTimeField year = ETHIOPIC_UTC.year();
285         DateTimeField yearOfEra = ETHIOPIC_UTC.yearOfEra();
286         DateTimeField era = ETHIOPIC_UTC.era();
287         int expectedDOW = new DateTime(8, 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("EE", era.getAsText(millis));
307             assertEquals("EE", 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(ETHIOPIC_UTC);
358         assertEquals(EthiopicChronology.EE, dt.getEra());
359         assertEquals(20, dt.getCenturyOfEra()); // TODO confirm
360
assertEquals(96, dt.getYearOfCentury());
361         assertEquals(1996, dt.getYearOfEra());
362         
363         assertEquals(1996, 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(1997, 10, 2, 0, 0, 0, 0, ETHIOPIC_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(1997, 1, 2, 0, 0, 0, 0, ETHIOPIC_UTC), fld.addToCopy(4));
380         assertEquals(new DateTime(1996, 1, 2, 0, 0, 0, 0, ETHIOPIC_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(1996, 10, 3, 0, 0, 0, 0, ETHIOPIC_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(1996, 10, 3, 0, 0, 0, 0, ETHIOPIC_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(1996, 10, 3, 0, 0, 0, 0, ETHIOPIC_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(ETHIOPIC_UTC);
423         assertEquals(EthiopicChronology.EE, dt.getEra());
424         assertEquals(1996, dt.getYear());
425         assertEquals(1996, 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 1999, NotLeap 1996,97,98
436
DateTime dt96 = new DateTime(1996, 10, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
437         DateTime dt97 = new DateTime(1997, 10, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
438         DateTime dt98 = new DateTime(1998, 10, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
439         DateTime dt99 = new DateTime(1999, 10, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
440         DateTime dt00 = new DateTime(2000, 10, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
441         
442         DurationField fld = dt96.year().getDurationField();
443         assertEquals(ETHIOPIC_UTC.years(), fld);
444         assertEquals(1L * 365L * MILLIS_PER_DAY, fld.getMillis(1, dt96.getMillis()));
445         assertEquals(2L * 365L * MILLIS_PER_DAY, fld.getMillis(2, dt96.getMillis()));
446         assertEquals(3L * 365L * MILLIS_PER_DAY, fld.getMillis(3, dt96.getMillis()));
447         assertEquals((4L * 365L + 1L) * MILLIS_PER_DAY, fld.getMillis(4, dt96.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, dt96.getMillis()));
453         assertEquals(2L * 365L * MILLIS_PER_DAY, fld.getMillis(2L, dt96.getMillis()));
454         assertEquals(3L * 365L * MILLIS_PER_DAY, fld.getMillis(3L, dt96.getMillis()));
455         assertEquals((4L * 365L + 1L) * MILLIS_PER_DAY, fld.getMillis(4L, dt96.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, dt96.getMillis()));
463         assertEquals(1, fld.getValue(1L * 365L * MILLIS_PER_DAY, dt96.getMillis()));
464         assertEquals(1, fld.getValue(1L * 365L * MILLIS_PER_DAY + 1L, dt96.getMillis()));
465         assertEquals(1, fld.getValue(2L * 365L * MILLIS_PER_DAY - 1L, dt96.getMillis()));
466         assertEquals(2, fld.getValue(2L * 365L * MILLIS_PER_DAY, dt96.getMillis()));
467         assertEquals(2, fld.getValue(2L * 365L * MILLIS_PER_DAY + 1L, dt96.getMillis()));
468         assertEquals(2, fld.getValue(3L * 365L * MILLIS_PER_DAY - 1L, dt96.getMillis()));
469         assertEquals(3, fld.getValue(3L * 365L * MILLIS_PER_DAY, dt96.getMillis()));
470         assertEquals(3, fld.getValue(3L * 365L * MILLIS_PER_DAY + 1L, dt96.getMillis()));
471         assertEquals(3, fld.getValue((4L * 365L + 1L) * MILLIS_PER_DAY - 1L, dt96.getMillis()));
472         assertEquals(4, fld.getValue((4L * 365L + 1L) * MILLIS_PER_DAY, dt96.getMillis()));
473         assertEquals(4, fld.getValue((4L * 365L + 1L) * MILLIS_PER_DAY + 1L, dt96.getMillis()));
474         
475         assertEquals(dt97.getMillis(), fld.add(dt96.getMillis(), 1));
476         assertEquals(dt98.getMillis(), fld.add(dt96.getMillis(), 2));
477         assertEquals(dt99.getMillis(), fld.add(dt96.getMillis(), 3));
478         assertEquals(dt00.getMillis(), fld.add(dt96.getMillis(), 4));
479         
480         assertEquals(dt97.getMillis(), fld.add(dt96.getMillis(), 1L));
481         assertEquals(dt98.getMillis(), fld.add(dt96.getMillis(), 2L));
482         assertEquals(dt99.getMillis(), fld.add(dt96.getMillis(), 3L));
483         assertEquals(dt00.getMillis(), fld.add(dt96.getMillis(), 4L));
484     }
485
486     public void testDurationMonth() {
487         // Leap 1999, NotLeap 1996,97,98
488
DateTime dt11 = new DateTime(1999, 11, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
489         DateTime dt12 = new DateTime(1999, 12, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
490         DateTime dt13 = new DateTime(1999, 13, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
491         DateTime dt01 = new DateTime(2000, 1, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
492         
493         DurationField fld = dt11.monthOfYear().getDurationField();
494         assertEquals(ETHIOPIC_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