use of org.joda.time.Chronology in project joda-time by JodaOrg.
the class BasePeriod method toDurationFrom.
//-----------------------------------------------------------------------
/**
* Gets the total millisecond duration of this period relative to a start instant.
* <p>
* This method adds the period to the specified instant in order to
* calculate the duration.
* <p>
* An instant must be supplied as the duration of a period varies.
* For example, a period of 1 month could vary between the equivalent of
* 28 and 31 days in milliseconds due to different length months.
* Similarly, a day can vary at Daylight Savings cutover, typically between
* 23 and 25 hours.
*
* @param startInstant the instant to add the period to, thus obtaining the duration
* @return the total length of the period as a duration relative to the start instant
* @throws ArithmeticException if the millis exceeds the capacity of the duration
*/
public Duration toDurationFrom(ReadableInstant startInstant) {
long startMillis = DateTimeUtils.getInstantMillis(startInstant);
Chronology chrono = DateTimeUtils.getInstantChronology(startInstant);
long endMillis = chrono.add(this, startMillis, 1);
return new Duration(startMillis, endMillis);
}
use of org.joda.time.Chronology in project joda-time by JodaOrg.
the class BaseSingleFieldPeriod method between.
//-----------------------------------------------------------------------
/**
* Calculates the number of whole units between the two specified datetimes.
*
* @param start the start instant, validated to not be null
* @param end the end instant, validated to not be null
* @param field the field type to use, must not be null
* @return the period
* @throws IllegalArgumentException if the instants are null or invalid
*/
protected static int between(ReadableInstant start, ReadableInstant end, DurationFieldType field) {
if (start == null || end == null) {
throw new IllegalArgumentException("ReadableInstant objects must not be null");
}
Chronology chrono = DateTimeUtils.getInstantChronology(start);
int amount = field.getField(chrono).getDifference(end.getMillis(), start.getMillis());
return amount;
}
use of org.joda.time.Chronology in project joda-time by JodaOrg.
the class BaseSingleFieldPeriod method standardPeriodIn.
/**
* Creates a new instance representing the number of complete standard length units
* in the specified period.
* <p>
* This factory method converts all fields from the period to hours using standardised
* durations for each field. Only those fields which have a precise duration in
* the ISO UTC chronology can be converted.
* <ul>
* <li>One week consists of 7 days.
* <li>One day consists of 24 hours.
* <li>One hour consists of 60 minutes.
* <li>One minute consists of 60 seconds.
* <li>One second consists of 1000 milliseconds.
* </ul>
* Months and Years are imprecise and periods containing these values cannot be converted.
*
* @param period the period to get the number of hours from, must not be null
* @param millisPerUnit the number of milliseconds in one standard unit of this period
* @throws IllegalArgumentException if the period contains imprecise duration values
*/
protected static int standardPeriodIn(ReadablePeriod period, long millisPerUnit) {
if (period == null) {
return 0;
}
Chronology iso = ISOChronology.getInstanceUTC();
long duration = 0L;
for (int i = 0; i < period.size(); i++) {
int value = period.getValue(i);
if (value != 0) {
DurationField field = period.getFieldType(i).getField(iso);
if (field.isPrecise() == false) {
throw new IllegalArgumentException("Cannot convert period to duration as " + field.getName() + " is not precise in the period " + period);
}
duration = FieldUtils.safeAdd(duration, FieldUtils.safeMultiply(field.getUnitMillis(), value));
}
}
return FieldUtils.safeToInt(duration / millisPerUnit);
}
use of org.joda.time.Chronology in project joda-time by JodaOrg.
the class StringConverter method setInto.
//-----------------------------------------------------------------------
/**
* Sets the value of the mutable interval from the string.
*
* @param writableInterval the interval to set
* @param object the String to convert, must not be null
* @param chrono the chronology to use, may be null
*/
public void setInto(ReadWritableInterval writableInterval, Object object, Chronology chrono) {
String str = (String) object;
int separator = str.indexOf('/');
if (separator < 0) {
throw new IllegalArgumentException("Format requires a '/' separator: " + str);
}
String leftStr = str.substring(0, separator);
if (leftStr.length() <= 0) {
throw new IllegalArgumentException("Format invalid: " + str);
}
String rightStr = str.substring(separator + 1);
if (rightStr.length() <= 0) {
throw new IllegalArgumentException("Format invalid: " + str);
}
DateTimeFormatter dateTimeParser = ISODateTimeFormat.dateTimeParser();
dateTimeParser = dateTimeParser.withChronology(chrono);
PeriodFormatter periodParser = ISOPeriodFormat.standard();
long startInstant = 0, endInstant = 0;
Period period = null;
Chronology parsedChrono = null;
// before slash
char c = leftStr.charAt(0);
if (c == 'P' || c == 'p') {
period = periodParser.withParseType(getPeriodType(leftStr)).parsePeriod(leftStr);
} else {
DateTime start = dateTimeParser.parseDateTime(leftStr);
startInstant = start.getMillis();
parsedChrono = start.getChronology();
}
// after slash
c = rightStr.charAt(0);
if (c == 'P' || c == 'p') {
if (period != null) {
throw new IllegalArgumentException("Interval composed of two durations: " + str);
}
period = periodParser.withParseType(getPeriodType(rightStr)).parsePeriod(rightStr);
chrono = (chrono != null ? chrono : parsedChrono);
endInstant = chrono.add(period, startInstant, 1);
} else {
DateTime end = dateTimeParser.parseDateTime(rightStr);
endInstant = end.getMillis();
parsedChrono = (parsedChrono != null ? parsedChrono : end.getChronology());
chrono = (chrono != null ? chrono : parsedChrono);
if (period != null) {
startInstant = chrono.add(period, endInstant, -1);
}
}
writableInterval.setInterval(startInstant, endInstant);
writableInterval.setChronology(chrono);
}
use of org.joda.time.Chronology in project joda-time by JodaOrg.
the class BasicChronology method getDateTimeMillis.
@Override
public long getDateTimeMillis(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond) throws IllegalArgumentException {
Chronology base;
if ((base = getBase()) != null) {
return base.getDateTimeMillis(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
}
FieldUtils.verifyValueBounds(DateTimeFieldType.hourOfDay(), hourOfDay, 0, 23);
FieldUtils.verifyValueBounds(DateTimeFieldType.minuteOfHour(), minuteOfHour, 0, 59);
FieldUtils.verifyValueBounds(DateTimeFieldType.secondOfMinute(), secondOfMinute, 0, 59);
FieldUtils.verifyValueBounds(DateTimeFieldType.millisOfSecond(), millisOfSecond, 0, 999);
long millisOfDay = hourOfDay * DateTimeConstants.MILLIS_PER_HOUR + minuteOfHour * DateTimeConstants.MILLIS_PER_MINUTE + secondOfMinute * DateTimeConstants.MILLIS_PER_SECOND + millisOfSecond;
return getDateTimeMillis0(year, monthOfYear, dayOfMonth, (int) millisOfDay);
}
Aggregations