1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.joda.time.chrono.gj;
17
18 import org.joda.time.DateTimeField;
19
20
21
22
23
24
25
26
27
28 public final class TestJulianChronology extends TestGJChronology {
29
30 private static final long JULIAN_EPOCH;
31
32 static {
33
34 JULIAN_EPOCH = new TestGregorianChronology().fixedFromGJ(0, 12, 30);
35 }
36
37
38
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 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("Illegal year: " + year);
79 }
80 return mod(year, 4) == (year > 0 ? 0 : 3);
81 }
82
83
84
85
86 long fixedFromGJ(int year, int monthOfYear, int dayOfMonth) {
87 if (year == 0) {
88 throw new IllegalArgumentException("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
102
103
104 int gjYearFromFixed(long date) {
105 return gjFromFixed(date)[0];
106 }
107
108
109
110
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("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("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
148
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 }