Skip to content
This repository has been archived by the owner on Mar 20, 2018. It is now read-only.

Commit

Permalink
Progress on epoch-secs-nanos branch
Browse files Browse the repository at this point in the history
  • Loading branch information
jodastephen committed Dec 2, 2011
1 parent 5cbba6c commit a3dcfb9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 35 deletions.
2 changes: 1 addition & 1 deletion checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
<module name="CovariantEquals"/>
<module name="DefaultComesLast"/>
<module name="ExplicitInitialization"/>
<module name="FallThrough"/>
<!--module name="FallThrough"/-->
<module name="JUnitTestCase"/>
<module name="FinalClass"/>
<module name="HideUtilityClassConstructor"/>
Expand Down
82 changes: 48 additions & 34 deletions src/main/java/javax/time/calendar/ISODateTimeRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import static javax.time.calendar.ISOChronology.SECONDS_PER_DAY;
import static javax.time.calendar.ISOChronology.SECONDS_PER_HOUR;
import static javax.time.calendar.ISOChronology.SECONDS_PER_MINUTE;
import static javax.time.calendar.ISODateTimeRule.DAY_OF_YEAR;
import static javax.time.calendar.ISOPeriodUnit.DAYS;
import static javax.time.calendar.ISOPeriodUnit.HOURS;
import static javax.time.calendar.ISOPeriodUnit.MILLIS;
Expand Down Expand Up @@ -353,31 +354,31 @@ protected boolean doIsExtractableFrom(DateTimeRule parentRule) {
protected long doSetIntoDateTime(long newValue, DateTimeRule baseRule, long basePackedDateTime, int baseNanoOfSecond) {
switch (ordinal) {
case NANO_OF_SECOND_ORDINAL: return basePackedDateTime;
case NANO_OF_DAY_ORDINAL: newValue /= NANOS_PER_SECOND; // drop through
case SECOND_OF_MINUTE_ORDINAL:
case SECOND_OF_HOUR_ORDINAL:
case NANO_OF_DAY_ORDINAL: newValue /= NANOS_PER_SECOND; // fall through
case SECOND_OF_MINUTE_ORDINAL: // fall through
case SECOND_OF_HOUR_ORDINAL: // fall through
case SECOND_OF_DAY_ORDINAL:
return simpleSetPdt(newValue, baseRule, basePackedDateTime, baseNanoOfSecond, 1);
case MINUTE_OF_HOUR_ORDINAL:
return simpleSetPdt(newValue, baseRule, basePackedDateTime, baseNanoOfSecond, 1);
case MINUTE_OF_HOUR_ORDINAL: // fall through
case MINUTE_OF_DAY_ORDINAL:
return simpleSetPdt(newValue, baseRule, basePackedDateTime, baseNanoOfSecond, SECONDS_PER_MINUTE);
return simpleSetPdt(newValue, baseRule, basePackedDateTime, baseNanoOfSecond, SECONDS_PER_MINUTE);
case CLOCK_HOUR_OF_AMPM_ORDINAL: return simpleSetPdt((newValue == 12 ? 0 : newValue), baseRule, basePackedDateTime, baseNanoOfSecond, SECONDS_PER_HOUR);
case CLOCK_HOUR_OF_DAY_ORDINAL: return simpleSetPdt((newValue == 24 ? 0 : newValue), baseRule, basePackedDateTime, baseNanoOfSecond, SECONDS_PER_HOUR);
case HOUR_OF_AMPM_ORDINAL:
case HOUR_OF_AMPM_ORDINAL: // fall through
case HOUR_OF_DAY_ORDINAL:
return simpleSetPdt(newValue, baseRule, basePackedDateTime, baseNanoOfSecond, SECONDS_PER_HOUR);
return simpleSetPdt(newValue, baseRule, basePackedDateTime, baseNanoOfSecond, SECONDS_PER_HOUR);
case AMPM_OF_DAY_ORDINAL:
return simpleSetPdt(newValue, baseRule, basePackedDateTime, baseNanoOfSecond, SECONDS_PER_HOUR * 12);
case DAY_OF_WEEK_ORDINAL: return 0;
return simpleSetPdt(newValue, baseRule, basePackedDateTime, baseNanoOfSecond, SECONDS_PER_HOUR * 12);
case DAY_OF_WEEK_ORDINAL: return setDow(newValue, baseRule, basePackedDateTime);
case EPOCH_DAY_ORDINAL: return pdtFromEd(newValue) + SECOND_OF_DAY.extractFrom(baseRule, basePackedDateTime, baseNanoOfSecond);
case DAY_OF_MONTH_ORDINAL: return simpleSetPdt(newValue, baseRule, basePackedDateTime, baseNanoOfSecond, SECONDS_PER_DAY);
case DAY_OF_YEAR_ORDINAL: return 0;
case ALIGNED_WEEK_OF_MONTH_ORDINAL: return simpleSetPdt(newValue, baseRule, basePackedDateTime, baseNanoOfSecond, 7 * SECONDS_PER_DAY);
case ALIGNED_WEEK_OF_YEAR_ORDINAL: return 0;
case MONTH_OF_QUARTER_ORDINAL:
case MONTH_OF_YEAR_ORDINAL:
case MONTH_OF_QUARTER_ORDINAL: // fall through
case MONTH_OF_YEAR_ORDINAL: // fall through
case EPOCH_MONTH_ORDINAL:
return simpleSetPdt(newValue, baseRule, basePackedDateTime, baseNanoOfSecond, 1 * (PDT_MASK + 1));
return simpleSetPdt(newValue, baseRule, basePackedDateTime, baseNanoOfSecond, 1 * (PDT_MASK + 1));
case QUARTER_OF_YEAR_ORDINAL: return simpleSetPdt(newValue, baseRule, basePackedDateTime, baseNanoOfSecond, 3 * (PDT_MASK + 1));
case YEAR_ORDINAL: return simpleSetPdt(newValue, baseRule, basePackedDateTime, baseNanoOfSecond, 12 * (PDT_MASK + 1));
case PACKED_DATE_TIME_ORDINAL: return newValue;
Expand All @@ -390,16 +391,47 @@ protected int doSetIntoNanoOfSecond(long newValue, DateTimeRule baseRule, long b
switch (ordinal) {
case NANO_OF_SECOND_ORDINAL:
case NANO_OF_DAY_ORDINAL:
return (int) (newValue % NANOS_PER_SECOND);
default:
return baseNanoOfSecond;
return (int) (newValue % NANOS_PER_SECOND);
default:
return baseNanoOfSecond;
}
}

private long simpleSetPdt(long newValue, DateTimeRule baseRule, long basePdt, int baseNos, long multiplier) {
return basePdt + (newValue - doExtractFrom(baseRule, basePdt, baseNos)) * multiplier;
}

private long setDow(long newValue, DateTimeRule baseRule, long basePackedDateTime) {
long baseEd = edFromPdt(basePackedDateTime);
long newEd = baseEd + (newValue - dowFromEd(baseEd));
return pdtFromEd(newEd) + simpleGet(basePackedDateTime, 1, SECONDS_PER_DAY);
}

private long setDoy(long newValue, DateTimeRule baseRule, long basePackedDateTime) {
long dom = domFromPdt(basePackedDateTime);
long em = emFromPdt(basePackedDateTime);
long moy = moyFromEm(em);
long year = yFromEm(em);
return MonthOfYear.of((int) moy).getMonthStartDayOfYear(ISOChronology.isLeapYear(year)) + dom - 1;

//
// boolean leap = ISOChronology.isLeapYear(year);
// if (dayOfYear == 366 && leap == false) {
// throw new InvalidCalendarFieldException("DayOfYear 366 is invalid for year " + year, DAY_OF_YEAR);
// }
// MonthOfYear moy = MonthOfYear.of((dayOfYear - 1) / 31 + 1);
// int monthEnd = moy.getMonthEndDayOfYear(leap);
// if (dayOfYear > monthEnd) {
// moy = moy.next();
// }
// int dom = dayOfYear - moy.getMonthStartDayOfYear(leap) + 1;
// return LocalDate.of(year, moy, dom);
//
// long baseEd = doyFromPdt(basePackedDateTime);
// long newEd = baseEd + (newValue - dowFromEd(baseEd));
// return pdtFromEd(newEd) + simpleGet(basePackedDateTime, 1, SECONDS_PER_DAY);
}

//-----------------------------------------------------------------------
static long pdtFromEd(long ed) {
// find the march-based year
Expand Down Expand Up @@ -436,24 +468,6 @@ private static long dowFromEd(long ed) {
}

//-----------------------------------------------------------------------
// private long setIntoPemd(long newValue, long basePemd) {
// // TODO
// switch (ordinal) {
// case DAY_OF_WEEK_ORDINAL: return 0;
// case DAY_OF_MONTH_ORDINAL: return basePemd + (newValue - extractFromPdt(basePemd));
// case DAY_OF_YEAR_ORDINAL: return 0;
// case EPOCH_DAY_ORDINAL: return pemdFromEd(newValue);
// case ALIGNED_WEEK_OF_MONTH_ORDINAL: return 0;
// case ALIGNED_WEEK_OF_YEAR_ORDINAL: return 0;
// case MONTH_OF_QUARTER_ORDINAL: return basePemd + (newValue - extractFromPdt(basePemd)) * 32;
// case MONTH_OF_YEAR_ORDINAL: return basePemd + (newValue - extractFromPdt(basePemd)) * 32;
// case QUARTER_OF_YEAR_ORDINAL: return basePemd + (newValue - extractFromPdt(basePemd)) * 32 * 3;
// case EPOCH_MONTH_ORDINAL: return basePemd + (newValue - extractFromPdt(basePemd)) * 32; // TODO: overflow?
// case YEAR_ORDINAL: return basePemd + (newValue - extractFromPdt(basePemd)) * 32; // TODO: overflow?
// }
// return Long.MIN_VALUE;
// }

private static long edFromPdt(long packedDateTime) {
return epochDaysFromPackedDateTime(packedDateTime);
}
Expand Down

0 comments on commit a3dcfb9

Please sign in to comment.