1 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 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 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 59 public int get(long instant) { 60 return iChronology.getWeekyear(instant); 61 } 62 63 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 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 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 127 public long set(long instant, int year) { 128 FieldUtils.verifyValueBounds(this, Math.abs(year), 129 iChronology.getMinYear(), iChronology.getMaxYear()); 130 int thisWeekyear = get( instant ); 134 if ( thisWeekyear == year ) { 135 return instant; 136 } 137 int thisDow = iChronology.getDayOfWeek(instant); 141 int weeksInFromYear = iChronology.getWeeksInYear( thisWeekyear ); 145 int weeksInToYear = iChronology.getWeeksInYear( year ); 146 int maxOutWeeks = (weeksInToYear < weeksInFromYear) ? 147 weeksInToYear : weeksInFromYear; 148 int setToWeek = iChronology.getWeekOfWeekyear(instant); 155 if ( setToWeek > maxOutWeeks ) { 156 setToWeek = maxOutWeeks; 157 } 158 long workInstant = instant; workInstant = iChronology.setYear( workInstant, year ); 169 int workWoyYear = get( workInstant ); 174 175 if ( workWoyYear < year ) { 180 workInstant += DateTimeConstants.MILLIS_PER_WEEK; 181 } else if ( workWoyYear > year ) { 182 workInstant -= DateTimeConstants.MILLIS_PER_WEEK; 183 } 184 188 int currentWoyWeek = iChronology.getWeekOfWeekyear(workInstant); 190 workInstant = workInstant + (setToWeek - currentWoyWeek) 192 * (long)DateTimeConstants.MILLIS_PER_WEEK; 193 195 workInstant = iChronology.dayOfWeek().set( workInstant, thisDow ); 201 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 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 249 private Object readResolve() { 250 return iChronology.weekyear(); 251 } 252 } 253 | Popular Tags |