KickJava   Java API By Example, From Geeks To Geeks.

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


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

26 public final class TestGregorianChronology extends TestGJChronology {
27     /**
28      * Constructs with an epoch of 1970-01-01.
29      */

30     public TestGregorianChronology() {
31         super(1970, 1, 1);
32     }
33
34     public TestGregorianChronology(int epochYear, int epochMonth, int epochDay) {
35         super(epochYear, epochMonth, epochDay);
36     }
37
38     public String JavaDoc toString() {
39         return "TestGregorianChronology";
40     }
41
42     long millisPerYear() {
43         return (long)(365.2425 * MILLIS_PER_DAY);
44     }
45
46     long millisPerMonth() {
47         return (long)(365.2425 * MILLIS_PER_DAY / 12);
48     }
49
50     boolean isLeapYear(int year) {
51         if (mod(year, 4) == 0) {
52             int t = (int)mod(year, 400);
53             if (t != 100 && t != 200 & t != 300) {
54                 return true;
55             }
56         }
57         return false;
58     }
59
60     /**
61      * @return days from 0001-01-01
62      */

63     long fixedFromGJ(int year, int monthOfYear, int dayOfMonth) {
64         long year_m1 = year - 1;
65         long f = 365 * year_m1 + div(year_m1, 4) - div(year_m1, 100)
66             + div(year_m1, 400) + div(367 * monthOfYear - 362, 12) + dayOfMonth;
67         if (monthOfYear > 2) {
68             f += isLeapYear(year) ? -1 : -2;
69         }
70         return f;
71     }
72
73     /**
74      * @param date days from 0001-01-01
75      * @return gj year
76      */

77     int gjYearFromFixed(long date) {
78         long d0 = date - 1;
79         long n400 = div(d0, 146097);
80         long d1 = mod(d0, 146097);
81         long n100 = div(d1, 36524);
82         long d2 = mod(d1, 36524);
83         long n4 = div(d2, 1461);
84         long d3 = mod(d2, 1461);
85         long n1 = div(d3, 365);
86         long year = 400 * n400 + 100 * n100 + 4 * n4 + n1;
87         if (!(n100 == 4 || n1 == 4)) {
88             year += 1;
89         }
90
91         int year_i = (int)year;
92         if (year_i == year) {
93             return year_i;
94         } else {
95             throw new RuntimeException JavaDoc("year cannot be cast to an int: " + year);
96         }
97     }
98
99     /**
100      * @param date days from 0001-01-01
101      * @return gj year, monthOfYear, dayOfMonth
102      */

103     int[] gjFromFixed(long date) {
104         int year = gjYearFromFixed(date);
105         long priorDays = date - fixedFromGJ(year, 1, 1);
106         long correction;
107         if (date < fixedFromGJ(year, 3, 1)) {
108             correction = 0;
109         } else if (isLeapYear(year)) {
110             correction = 1;
111         } else {
112             correction = 2;
113         }
114         int monthOfYear = (int)div(12 * (priorDays + correction) + 373, 367);
115         int day = (int)(date - fixedFromGJ(year, monthOfYear, 1) + 1);
116
117         return new int[]{year, monthOfYear, day};
118     }
119
120     long fixedFromISO(int weekyear, int weekOfWeekyear, int dayOfWeek) {
121         return nthWeekday(weekOfWeekyear, 0, weekyear - 1, 12, 28) + dayOfWeek;
122     }
123
124     /**
125      * @param date days from 0001-01-01
126      * @return iso weekyear, weekOfWeekyear, dayOfWeek (1=Monday to 7)
127      */

128     int[] isoFromFixed(long date) {
129         int weekyear = gjYearFromFixed(date - 3);
130         if (date >= fixedFromISO(weekyear + 1, 1, 1)) {
131             weekyear += 1;
132         }
133         int weekOfWeekyear = (int)(div(date - fixedFromISO(weekyear, 1, 1), 7) + 1);
134         int dayOfWeek = (int)amod(date, 7);
135         return new int[]{weekyear, weekOfWeekyear, dayOfWeek};
136     }
137 }
138
Popular Tags