01: /*
02: * Copyright 2001-2005 Stephen Colebourne
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.DateTimeFieldType;
19: import org.joda.time.DurationField;
20:
21: /**
22: *
23: * @author Brian S O'Neill
24: */
25: class TestGJMonthOfYearField extends TestGJDateTimeField {
26: public TestGJMonthOfYearField(TestGJChronology chrono) {
27: super (DateTimeFieldType.monthOfYear(), chrono.millisPerMonth(),
28: chrono);
29: }
30:
31: public int get(long millis) {
32: return iChronology.gjFromMillis(millis)[1];
33: }
34:
35: public long set(long millis, int value) {
36: long timeOnlyMillis = iChronology.getTimeOnlyMillis(millis);
37: int[] ymd = iChronology.gjFromMillis(millis);
38: // First set to start of month...
39: millis = iChronology.millisFromGJ(ymd[0], value, 1);
40: // ...and use dayOfMonth field to check range.
41: int maxDay = iChronology.dayOfMonth().getMaximumValue(millis);
42: if (ymd[2] > maxDay) {
43: ymd[2] = maxDay;
44: }
45: return timeOnlyMillis
46: + iChronology.millisFromGJ(ymd[0], value, ymd[2]);
47: }
48:
49: public long add(long millis, long value) {
50: int newYear = iChronology.year().get(millis)
51: + (int) TestGJChronology.div(value, 12);
52: int newMonth = get(millis)
53: + (int) TestGJChronology.mod(value, 12);
54: if (newMonth > 12) {
55: newYear++;
56: newMonth -= 12;
57: }
58: int newDay = iChronology.dayOfMonth().get(millis);
59: millis = iChronology.getTimeOnlyMillis(millis)
60: + iChronology.millisFromGJ(newYear, newMonth, newDay);
61: while (get(millis) != newMonth) {
62: millis = iChronology.dayOfYear().add(millis, -1);
63: }
64: return millis;
65: }
66:
67: public boolean isLeap(long millis) {
68: int[] ymd = iChronology.gjFromMillis(millis);
69: return ymd[1] == 2 && iChronology.isLeapYear(ymd[0]);
70: }
71:
72: public int getLeapAmount(long millis) {
73: return isLeap(millis) ? 1 : 0;
74: }
75:
76: public DurationField getLeapDurationField() {
77: return iChronology.days();
78: }
79:
80: public DurationField getRangeDurationField() {
81: return iChronology.years();
82: }
83:
84: public int getMinimumValue() {
85: return 1;
86: }
87:
88: public int getMaximumValue() {
89: return 12;
90: }
91:
92: public long roundFloor(long millis) {
93: int[] ymd = iChronology.gjFromMillis(millis);
94: return iChronology.millisFromGJ(ymd[0], ymd[1], 1);
95: }
96: }
|