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.HashMap; |
19 | import java.util.Map; |
20 | |
21 | import org.joda.time.Chronology; |
22 | import org.joda.time.DateTimeConstants; |
23 | import org.joda.time.DateTimeZone; |
24 | |
25 | /** |
26 | * Implements a pure proleptic Gregorian calendar system, which defines every |
27 | * fourth year as leap, unless the year is divisible by 100 and not by 400. |
28 | * This improves upon the Julian calendar leap year rule. |
29 | * <p> |
30 | * Although the Gregorian calendar did not exist before 1582 CE, this |
31 | * chronology assumes it did, thus it is proleptic. This implementation also |
32 | * fixes the start of the year at January 1, and defines the year zero. |
33 | * <p> |
34 | * GregorianChronology is thread-safe and immutable. |
35 | * |
36 | * @see <a href="http://en.wikipedia.org/wiki/Gregorian_calendar">Wikipedia</a> |
37 | * @see JulianChronology |
38 | * @see GJChronology |
39 | * |
40 | * @author Guy Allard |
41 | * @author Stephen Colebourne |
42 | * @author Brian S O'Neill |
43 | * @since 1.0 |
44 | */ |
45 | public final class GregorianChronology extends BasicGJChronology { |
46 | |
47 | /** Serialization lock */ |
48 | private static final long serialVersionUID = -861407383323710522L; |
49 | |
50 | private static final long MILLIS_PER_YEAR = |
51 | (long) (365.2425 * DateTimeConstants.MILLIS_PER_DAY); |
52 | |
53 | private static final long MILLIS_PER_MONTH = |
54 | (long) (365.2425 * DateTimeConstants.MILLIS_PER_DAY / 12); |
55 | |
56 | private static final int DAYS_0000_TO_1970 = 719527; |
57 | |
58 | /** The lowest year that can be fully supported. */ |
59 | private static final int MIN_YEAR = -292275054; |
60 | |
61 | /** The highest year that can be fully supported. */ |
62 | private static final int MAX_YEAR = 292278993; |
63 | |
64 | /** Singleton instance of a UTC GregorianChronology */ |
65 | private static final GregorianChronology INSTANCE_UTC; |
66 | |
67 | /** Cache of zone to chronology arrays */ |
68 | private static final Map cCache = new HashMap(); |
69 | |
70 | static { |
71 | INSTANCE_UTC = getInstance(DateTimeZone.UTC); |
72 | } |
73 | |
74 | /** |
75 | * Gets an instance of the GregorianChronology. |
76 | * The time zone of the returned instance is UTC. |
77 | * |
78 | * @return a singleton UTC instance of the chronology |
79 | */ |
80 | public static GregorianChronology getInstanceUTC() { |
81 | return INSTANCE_UTC; |
82 | } |
83 | |
84 | /** |
85 | * Gets an instance of the GregorianChronology in the default time zone. |
86 | * |
87 | * @return a chronology in the default time zone |
88 | */ |
89 | public static GregorianChronology getInstance() { |
90 | return getInstance(DateTimeZone.getDefault(), 4); |
91 | } |
92 | |
93 | /** |
94 | * Gets an instance of the GregorianChronology in the given time zone. |
95 | * |
96 | * @param zone the time zone to get the chronology in, null is default |
97 | * @return a chronology in the specified time zone |
98 | */ |
99 | public static GregorianChronology getInstance(DateTimeZone zone) { |
100 | return getInstance(zone, 4); |
101 | } |
102 | |
103 | /** |
104 | * Gets an instance of the GregorianChronology in the given time zone. |
105 | * |
106 | * @param zone the time zone to get the chronology in, null is default |
107 | * @param minDaysInFirstWeek minimum number of days in first week of the year; default is 4 |
108 | * @return a chronology in the specified time zone |
109 | */ |
110 | public static GregorianChronology getInstance(DateTimeZone zone, int minDaysInFirstWeek) { |
111 | if (zone == null) { |
112 | zone = DateTimeZone.getDefault(); |
113 | } |
114 | GregorianChronology chrono; |
115 | synchronized (cCache) { |
116 | GregorianChronology[] chronos = (GregorianChronology[]) cCache.get(zone); |
117 | if (chronos == null) { |
118 | chronos = new GregorianChronology[7]; |
119 | cCache.put(zone, chronos); |
120 | } |
121 | try { |
122 | chrono = chronos[minDaysInFirstWeek - 1]; |
123 | } catch (ArrayIndexOutOfBoundsException e) { |
124 | throw new IllegalArgumentException |
125 | ("Invalid min days in first week: " + minDaysInFirstWeek); |
126 | } |
127 | if (chrono == null) { |
128 | if (zone == DateTimeZone.UTC) { |
129 | chrono = new GregorianChronology(null, null, minDaysInFirstWeek); |
130 | } else { |
131 | chrono = getInstance(DateTimeZone.UTC, minDaysInFirstWeek); |
132 | chrono = new GregorianChronology |
133 | (ZonedChronology.getInstance(chrono, zone), null, minDaysInFirstWeek); |
134 | } |
135 | chronos[minDaysInFirstWeek - 1] = chrono; |
136 | } |
137 | } |
138 | return chrono; |
139 | } |
140 | |
141 | // Constructors and instance variables |
142 | //----------------------------------------------------------------------- |
143 | |
144 | /** |
145 | * Restricted constructor |
146 | */ |
147 | private GregorianChronology(Chronology base, Object param, int minDaysInFirstWeek) { |
148 | super(base, param, minDaysInFirstWeek); |
149 | } |
150 | |
151 | /** |
152 | * Serialization singleton |
153 | */ |
154 | private Object readResolve() { |
155 | Chronology base = getBase(); |
156 | int minDays = getMinimumDaysInFirstWeek(); |
157 | minDays = (minDays == 0 ? 4 : minDays); // handle rename of BaseGJChronology |
158 | return base == null ? |
159 | getInstance(DateTimeZone.UTC, minDays) : |
160 | getInstance(base.getZone(), minDays); |
161 | } |
162 | |
163 | // Conversion |
164 | //----------------------------------------------------------------------- |
165 | /** |
166 | * Gets the Chronology in the UTC time zone. |
167 | * |
168 | * @return the chronology in UTC |
169 | */ |
170 | public Chronology withUTC() { |
171 | return INSTANCE_UTC; |
172 | } |
173 | |
174 | /** |
175 | * Gets the Chronology in a specific time zone. |
176 | * |
177 | * @param zone the zone to get the chronology in, null is default |
178 | * @return the chronology |
179 | */ |
180 | public Chronology withZone(DateTimeZone zone) { |
181 | if (zone == null) { |
182 | zone = DateTimeZone.getDefault(); |
183 | } |
184 | if (zone == getZone()) { |
185 | return this; |
186 | } |
187 | return getInstance(zone); |
188 | } |
189 | |
190 | protected void assemble(Fields fields) { |
191 | if (getBase() == null) { |
192 | super.assemble(fields); |
193 | } |
194 | } |
195 | |
196 | boolean isLeapYear(int year) { |
197 | return ((year & 3) == 0) && ((year % 100) != 0 || (year % 400) == 0); |
198 | } |
199 | |
200 | long calculateFirstDayOfYearMillis(int year) { |
201 | // Initial value is just temporary. |
202 | int leapYears = year / 100; |
203 | if (year < 0) { |
204 | // Add 3 before shifting right since /4 and >>2 behave differently |
205 | // on negative numbers. When the expression is written as |
206 | // (year / 4) - (year / 100) + (year / 400), |
207 | // it works for both positive and negative values, except this optimization |
208 | // eliminates two divisions. |
209 | leapYears = ((year + 3) >> 2) - leapYears + ((leapYears + 3) >> 2) - 1; |
210 | } else { |
211 | leapYears = (year >> 2) - leapYears + (leapYears >> 2); |
212 | if (isLeapYear(year)) { |
213 | leapYears--; |
214 | } |
215 | } |
216 | |
217 | return (year * 365L + (leapYears - DAYS_0000_TO_1970)) * DateTimeConstants.MILLIS_PER_DAY; |
218 | } |
219 | |
220 | int getMinYear() { |
221 | return MIN_YEAR; |
222 | } |
223 | |
224 | int getMaxYear() { |
225 | return MAX_YEAR; |
226 | } |
227 | |
228 | long getAverageMillisPerYear() { |
229 | return MILLIS_PER_YEAR; |
230 | } |
231 | |
232 | long getAverageMillisPerYearDividedByTwo() { |
233 | return MILLIS_PER_YEAR / 2; |
234 | } |
235 | |
236 | long getAverageMillisPerMonth() { |
237 | return MILLIS_PER_MONTH; |
238 | } |
239 | |
240 | long getApproxMillisAtEpochDividedByTwo() { |
241 | return (1970L * MILLIS_PER_YEAR) / 2; |
242 | } |
243 | |
244 | } |