1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.joda.time.chrono;
17
18 import org.joda.time.DateTimeConstants;
19 import org.joda.time.DateTimeFieldType;
20 import org.joda.time.DurationField;
21 import org.joda.time.field.FieldUtils;
22 import org.joda.time.field.ImpreciseDateTimeField;
23
24
25
26
27
28
29
30
31
32 final class BasicWeekyearDateTimeField extends ImpreciseDateTimeField {
33
34 private static final long serialVersionUID = 6215066916806820644L;
35
36 private static final long WEEK_53 = (53L - 1) * DateTimeConstants.MILLIS_PER_WEEK;
37
38 private final BasicChronology iChronology;
39
40
41
42
43 BasicWeekyearDateTimeField(BasicChronology chronology) {
44 super(DateTimeFieldType.weekyear(), chronology.getAverageMillisPerYear());
45 iChronology = chronology;
46 }
47
48 public boolean isLenient() {
49 return false;
50 }
51
52
53
54
55
56
57
58
59 public int get(long instant) {
60 return iChronology.getWeekyear(instant);
61 }
62
63
64
65
66
67
68
69
70
71 public long add(long instant, int years) {
72 if (years == 0) {
73 return instant;
74 }
75 return set(instant, get(instant) + years);
76 }
77
78 public long add(long instant, long value) {
79 return add(instant, FieldUtils.safeToInt(value));
80 }
81
82
83
84
85
86
87
88
89
90
91 public long addWrapField(long instant, int years) {
92 return add(instant, years);
93 }
94
95 public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) {
96 if (minuendInstant < subtrahendInstant) {
97 return -getDifference(subtrahendInstant, minuendInstant);
98 }
99
100 int minuendWeekyear = get(minuendInstant);
101 int subtrahendWeekyear = get(subtrahendInstant);
102
103 long minuendRem = remainder(minuendInstant);
104 long subtrahendRem = remainder(subtrahendInstant);
105
106
107 if (subtrahendRem >= WEEK_53 && iChronology.getWeeksInYear(minuendWeekyear) <= 52) {
108 subtrahendRem -= DateTimeConstants.MILLIS_PER_WEEK;
109 }
110
111 int difference = minuendWeekyear - subtrahendWeekyear;
112 if (minuendRem < subtrahendRem) {
113 difference--;
114 }
115 return difference;
116 }
117
118
119
120
121
122
123
124
125
126
127 public long set(long instant, int year) {
128 FieldUtils.verifyValueBounds(this, Math.abs(year),
129 iChronology.getMinYear(), iChronology.getMaxYear());
130
131
132
133 int thisWeekyear = get( instant );
134 if ( thisWeekyear == year ) {
135 return instant;
136 }
137
138
139
140 int thisDow = iChronology.getDayOfWeek(instant);
141
142
143
144 int weeksInFromYear = iChronology.getWeeksInYear( thisWeekyear );
145 int weeksInToYear = iChronology.getWeeksInYear( year );
146 int maxOutWeeks = (weeksInToYear < weeksInFromYear) ?
147 weeksInToYear : weeksInFromYear;
148
149
150
151
152
153
154 int setToWeek = iChronology.getWeekOfWeekyear(instant);
155 if ( setToWeek > maxOutWeeks ) {
156 setToWeek = maxOutWeeks;
157 }
158
159
160
161
162 long workInstant = instant;
163
164
165
166
167
168 workInstant = iChronology.setYear( workInstant, year );
169
170
171
172
173 int workWoyYear = get( workInstant );
174
175
176
177
178
179 if ( workWoyYear < year ) {
180 workInstant += DateTimeConstants.MILLIS_PER_WEEK;
181 } else if ( workWoyYear > year ) {
182 workInstant -= DateTimeConstants.MILLIS_PER_WEEK;
183 }
184
185
186
187
188
189 int currentWoyWeek = iChronology.getWeekOfWeekyear(workInstant);
190
191 workInstant = workInstant + (setToWeek - currentWoyWeek)
192 * (long)DateTimeConstants.MILLIS_PER_WEEK;
193
194
195
196
197
198
199
200 workInstant = iChronology.dayOfWeek().set( workInstant, thisDow );
201
202
203
204 return workInstant;
205 }
206
207 public DurationField getRangeDurationField() {
208 return null;
209 }
210
211 public boolean isLeap(long instant) {
212 return iChronology.getWeeksInYear(iChronology.getWeekyear(instant)) > 52;
213 }
214
215 public int getLeapAmount(long instant) {
216 return iChronology.getWeeksInYear(iChronology.getWeekyear(instant)) - 52;
217 }
218
219 public DurationField getLeapDurationField() {
220 return iChronology.weeks();
221 }
222
223 public int getMinimumValue() {
224 return iChronology.getMinYear();
225 }
226
227 public int getMaximumValue() {
228 return iChronology.getMaxYear();
229 }
230
231 public long roundFloor(long instant) {
232
233
234 instant = iChronology.weekOfWeekyear().roundFloor(instant);
235 int wow = iChronology.getWeekOfWeekyear(instant);
236 if (wow > 1) {
237 instant -= ((long) DateTimeConstants.MILLIS_PER_WEEK) * (wow - 1);
238 }
239 return instant;
240 }
241
242 public long remainder(long instant) {
243 return instant - roundFloor(instant);
244 }
245
246
247
248
249 private Object readResolve() {
250 return iChronology.weekyear();
251 }
252 }