KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > chrono > gj > TestJulianChronology


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.gj;
17
18 import org.joda.time.DateTimeField;
19
20 /**
21  * A reference Julian chronology implementation, intended for testing purposes
22  * only. Correctness is favored over performance. The key functions for date
23  * calculations are based on ones provided in "Calendrical Calculations", ISBN
24  * 0-521-77752-6.
25  *
26  * @author Brian S O'Neill
27  */

28 public final class TestJulianChronology extends TestGJChronology {
29
30     private static final long JULIAN_EPOCH;
31
32     static {
33         // Constant as defined in book.
34
JULIAN_EPOCH = new TestGregorianChronology().fixedFromGJ(0, 12, 30);
35     }
36
37     /**
38      * Constructs with an epoch of 1969-12-19.
39      */

40     public TestJulianChronology() {
41         super(1969, 12, 19);
42     }
43
44     public TestJulianChronology(int epochYear, int epochMonth, int epochDay) {
45         super(epochYear, epochMonth, epochDay);
46     }
47
48     public DateTimeField dayOfMonth() {
49         return new TestJulianDayOfMonthField(this);
50     }
51
52     public DateTimeField weekyear() {
53         return new TestJulianWeekyearField(this);
54     }
55
56     public DateTimeField monthOfYear() {
57         return new TestJulianMonthOfYearField(this);
58     }
59
60     public DateTimeField year() {
61         return new TestJulianYearField(this);
62     }
63
64     public String JavaDoc toString() {
65         return "TestJulianChronology";
66     }
67
68     long millisPerYear() {
69         return (long)(365.25 * MILLIS_PER_DAY);
70     }
71
72     long millisPerMonth() {
73         return (long)(365.25 * MILLIS_PER_DAY / 12);
74     }
75
76     boolean isLeapYear(int year) {
77         if (year == 0) {
78             throw new IllegalArgumentException JavaDoc("Illegal year: " + year);
79         }
80         return mod(year, 4) == (year > 0 ? 0 : 3);
81     }
82
83     /**
84      * @return days from 0001-01-01
85      */

86     long fixedFromGJ(int year, int monthOfYear, int dayOfMonth) {
87         if (year == 0) {
88             throw new IllegalArgumentException JavaDoc("Illegal year: " + year);
89         }
90         int y = (year < 0) ? year + 1 : year;
91         long y_m1 = y - 1;
92         long f = JULIAN_EPOCH - 1 + 365 * y_m1 + div(y_m1, 4)
93             + div(367 * monthOfYear - 362, 12) + dayOfMonth;
94         if (monthOfYear > 2) {
95             f += isLeapYear(year) ? -1 : -2;
96         }
97         return f;
98     }
99
100     /**
101      * @param date days from 0001-01-01
102      * @return gj year
103      */

104     int gjYearFromFixed(long date) {
105         return gjFromFixed(date)[0];
106     }
107
108     /**
109      * @param date days from 0001-01-01
110      * @return gj year, monthOfYear, dayOfMonth
111      */

112     int[] gjFromFixed(long date) {
113         long approx = div(4 * (date - JULIAN_EPOCH) + 1464, 1461);
114         long year = (approx <= 0) ? approx - 1 : approx;
115         int year_i = (int)year;
116         if (year_i != year) {
117             throw new RuntimeException JavaDoc("year cannot be cast to an int: " + year);
118         }
119         long priorDays = date - fixedFromGJ(year_i, 1, 1);
120         long correction;
121         if (date < fixedFromGJ(year_i, 3, 1)) {
122             correction = 0;
123         } else if (isLeapYear(year_i)) {
124             correction = 1;
125         } else {
126             correction = 2;
127         }
128         int monthOfYear = (int)div(12 * (priorDays + correction) + 373, 367);
129         int day = (int)(date - fixedFromGJ(year_i, monthOfYear, 1) + 1);
130
131         return new int[]{year_i, monthOfYear, day};
132     }
133
134     long fixedFromISO(int weekyear, int weekOfWeekyear, int dayOfWeek) {
135         if (weekyear == 0) {
136             throw new IllegalArgumentException JavaDoc("Illegal weekyear: " + weekyear);
137         }
138         if (weekyear == 1) {
139             weekyear = -1;
140         } else {
141             weekyear--;
142         }
143         return nthWeekday(weekOfWeekyear, 0, weekyear, 12, 28) + dayOfWeek;
144     }
145
146     /**
147      * @param date days from 0001-01-01
148      * @return iso weekyear, weekOfWeekyear, dayOfWeek (1=Monday to 7)
149      */

150     int[] isoFromFixed(long date) {
151         int weekyear = gjYearFromFixed(date - 3);
152         int nextWeekyear;
153         if (weekyear == -1) {
154             nextWeekyear = 1;
155         } else {
156             nextWeekyear = weekyear + 1;
157         }
158         if (date >= fixedFromISO(nextWeekyear, 1, 1)) {
159             weekyear = nextWeekyear;
160         }
161         int weekOfWeekyear = (int)(div(date - fixedFromISO(weekyear, 1, 1), 7) + 1);
162         int dayOfWeek = (int)amod(date, 7);
163         return new int[]{weekyear, weekOfWeekyear, dayOfWeek};
164     }
165 }
166
Popular Tags