Search in sources :

Example 1 with DurationField

use of org.joda.time.DurationField in project joda-time by JodaOrg.

the class TestCopticChronology method testDurationYear.

public void testDurationYear() {
    // Leap 1723
    DateTime dt20 = new DateTime(1720, 10, 2, 0, 0, 0, 0, COPTIC_UTC);
    DateTime dt21 = new DateTime(1721, 10, 2, 0, 0, 0, 0, COPTIC_UTC);
    DateTime dt22 = new DateTime(1722, 10, 2, 0, 0, 0, 0, COPTIC_UTC);
    DateTime dt23 = new DateTime(1723, 10, 2, 0, 0, 0, 0, COPTIC_UTC);
    DateTime dt24 = new DateTime(1724, 10, 2, 0, 0, 0, 0, COPTIC_UTC);
    DurationField fld = dt20.year().getDurationField();
    assertEquals(COPTIC_UTC.years(), fld);
    assertEquals(1L * 365L * MILLIS_PER_DAY, fld.getMillis(1, dt20.getMillis()));
    assertEquals(2L * 365L * MILLIS_PER_DAY, fld.getMillis(2, dt20.getMillis()));
    assertEquals(3L * 365L * MILLIS_PER_DAY, fld.getMillis(3, dt20.getMillis()));
    assertEquals((4L * 365L + 1L) * MILLIS_PER_DAY, fld.getMillis(4, dt20.getMillis()));
    assertEquals(((4L * 365L + 1L) * MILLIS_PER_DAY) / 4, fld.getMillis(1));
    assertEquals(((4L * 365L + 1L) * MILLIS_PER_DAY) / 2, fld.getMillis(2));
    assertEquals(1L * 365L * MILLIS_PER_DAY, fld.getMillis(1L, dt20.getMillis()));
    assertEquals(2L * 365L * MILLIS_PER_DAY, fld.getMillis(2L, dt20.getMillis()));
    assertEquals(3L * 365L * MILLIS_PER_DAY, fld.getMillis(3L, dt20.getMillis()));
    assertEquals((4L * 365L + 1L) * MILLIS_PER_DAY, fld.getMillis(4L, dt20.getMillis()));
    assertEquals(((4L * 365L + 1L) * MILLIS_PER_DAY) / 4, fld.getMillis(1L));
    assertEquals(((4L * 365L + 1L) * MILLIS_PER_DAY) / 2, fld.getMillis(2L));
    assertEquals(((4L * 365L + 1L) * MILLIS_PER_DAY) / 4, fld.getUnitMillis());
    assertEquals(0, fld.getValue(1L * 365L * MILLIS_PER_DAY - 1L, dt20.getMillis()));
    assertEquals(1, fld.getValue(1L * 365L * MILLIS_PER_DAY, dt20.getMillis()));
    assertEquals(1, fld.getValue(1L * 365L * MILLIS_PER_DAY + 1L, dt20.getMillis()));
    assertEquals(1, fld.getValue(2L * 365L * MILLIS_PER_DAY - 1L, dt20.getMillis()));
    assertEquals(2, fld.getValue(2L * 365L * MILLIS_PER_DAY, dt20.getMillis()));
    assertEquals(2, fld.getValue(2L * 365L * MILLIS_PER_DAY + 1L, dt20.getMillis()));
    assertEquals(2, fld.getValue(3L * 365L * MILLIS_PER_DAY - 1L, dt20.getMillis()));
    assertEquals(3, fld.getValue(3L * 365L * MILLIS_PER_DAY, dt20.getMillis()));
    assertEquals(3, fld.getValue(3L * 365L * MILLIS_PER_DAY + 1L, dt20.getMillis()));
    assertEquals(3, fld.getValue((4L * 365L + 1L) * MILLIS_PER_DAY - 1L, dt20.getMillis()));
    assertEquals(4, fld.getValue((4L * 365L + 1L) * MILLIS_PER_DAY, dt20.getMillis()));
    assertEquals(4, fld.getValue((4L * 365L + 1L) * MILLIS_PER_DAY + 1L, dt20.getMillis()));
    assertEquals(dt21.getMillis(), fld.add(dt20.getMillis(), 1));
    assertEquals(dt22.getMillis(), fld.add(dt20.getMillis(), 2));
    assertEquals(dt23.getMillis(), fld.add(dt20.getMillis(), 3));
    assertEquals(dt24.getMillis(), fld.add(dt20.getMillis(), 4));
    assertEquals(dt21.getMillis(), fld.add(dt20.getMillis(), 1L));
    assertEquals(dt22.getMillis(), fld.add(dt20.getMillis(), 2L));
    assertEquals(dt23.getMillis(), fld.add(dt20.getMillis(), 3L));
    assertEquals(dt24.getMillis(), fld.add(dt20.getMillis(), 4L));
}
Also used : DurationField(org.joda.time.DurationField) DateTime(org.joda.time.DateTime)

Example 2 with DurationField

use of org.joda.time.DurationField in project joda-time by JodaOrg.

the class TestGJChronology method testAdd.

private void testAdd(String start, DurationFieldType type, int amt, String end) {
    DateTime dtStart = new DateTime(start, GJChronology.getInstance(DateTimeZone.UTC));
    DateTime dtEnd = new DateTime(end, GJChronology.getInstance(DateTimeZone.UTC));
    assertEquals(dtEnd, dtStart.withFieldAdded(type, amt));
    assertEquals(dtStart, dtEnd.withFieldAdded(type, -amt));
    DurationField field = type.getField(GJChronology.getInstance(DateTimeZone.UTC));
    int diff = field.getDifference(dtEnd.getMillis(), dtStart.getMillis());
    assertEquals(amt, diff);
    if (type == DurationFieldType.years() || type == DurationFieldType.months() || type == DurationFieldType.days()) {
        YearMonthDay ymdStart = new YearMonthDay(start, GJChronology.getInstance(DateTimeZone.UTC));
        YearMonthDay ymdEnd = new YearMonthDay(end, GJChronology.getInstance(DateTimeZone.UTC));
        assertEquals(ymdEnd, ymdStart.withFieldAdded(type, amt));
        assertEquals(ymdStart, ymdEnd.withFieldAdded(type, -amt));
    }
}
Also used : YearMonthDay(org.joda.time.YearMonthDay) DurationField(org.joda.time.DurationField) DateTime(org.joda.time.DateTime)

Example 3 with DurationField

use of org.joda.time.DurationField in project joda-time by JodaOrg.

the class TestMillisDurationField method testSerialization.

//-----------------------------------------------------------------------
public void testSerialization() throws Exception {
    DurationField test = MillisDurationField.INSTANCE;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(test);
    oos.close();
    byte[] bytes = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ObjectInputStream ois = new ObjectInputStream(bais);
    DurationField result = (DurationField) ois.readObject();
    ois.close();
    assertSame(test, result);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) DurationField(org.joda.time.DurationField) ObjectInputStream(java.io.ObjectInputStream)

Example 4 with DurationField

use of org.joda.time.DurationField in project joda-time by JodaOrg.

the class TestScaledDurationField method setUp.

protected void setUp() throws Exception {
    DurationField base = MillisDurationField.INSTANCE;
    iField = new ScaledDurationField(base, DurationFieldType.minutes(), 90);
}
Also used : DurationField(org.joda.time.DurationField)

Example 5 with DurationField

use of org.joda.time.DurationField in project joda-time by JodaOrg.

the class DateTimeParserBucket method computeMillis.

/**
     * Computes the parsed datetime by setting the saved fields.
     * This method is idempotent, but it is not thread-safe.
     *
     * @param resetFields false by default, but when true, unsaved field values are cleared
     * @param text optional text being parsed, to be included in any error message
     * @return milliseconds since 1970-01-01T00:00:00Z
     * @throws IllegalArgumentException if any field is out of range
     * @since 2.4
     */
public long computeMillis(boolean resetFields, CharSequence text) {
    SavedField[] savedFields = iSavedFields;
    int count = iSavedFieldsCount;
    if (iSavedFieldsShared) {
        // clone so that sort does not affect saved state
        iSavedFields = savedFields = (SavedField[]) iSavedFields.clone();
        iSavedFieldsShared = false;
    }
    sort(savedFields, count);
    if (count > 0) {
        // alter base year for parsing if first field is month or day
        DurationField months = DurationFieldType.months().getField(iChrono);
        DurationField days = DurationFieldType.days().getField(iChrono);
        DurationField first = savedFields[0].iField.getDurationField();
        if (compareReverse(first, months) >= 0 && compareReverse(first, days) <= 0) {
            saveField(DateTimeFieldType.year(), iDefaultYear);
            return computeMillis(resetFields, text);
        }
    }
    long millis = iMillis;
    try {
        for (int i = 0; i < count; i++) {
            millis = savedFields[i].set(millis, resetFields);
        }
        if (resetFields) {
            for (int i = 0; i < count; i++) {
                millis = savedFields[i].set(millis, i == (count - 1));
            }
        }
    } catch (IllegalFieldValueException e) {
        if (text != null) {
            e.prependMessage("Cannot parse \"" + text + '"');
        }
        throw e;
    }
    if (iOffset != null) {
        millis -= iOffset;
    } else if (iZone != null) {
        int offset = iZone.getOffsetFromLocal(millis);
        millis -= offset;
        if (offset != iZone.getOffset(millis)) {
            String message = "Illegal instant due to time zone offset transition (" + iZone + ')';
            if (text != null) {
                message = "Cannot parse \"" + text + "\": " + message;
            }
            throw new IllegalInstantException(message);
        }
    }
    return millis;
}
Also used : IllegalFieldValueException(org.joda.time.IllegalFieldValueException) IllegalInstantException(org.joda.time.IllegalInstantException) DurationField(org.joda.time.DurationField)

Aggregations

DurationField (org.joda.time.DurationField)20 DateTime (org.joda.time.DateTime)6 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ObjectInputStream (java.io.ObjectInputStream)3 ObjectOutputStream (java.io.ObjectOutputStream)3 YearMonthDay (org.joda.time.YearMonthDay)2 Chronology (org.joda.time.Chronology)1 IllegalFieldValueException (org.joda.time.IllegalFieldValueException)1 IllegalInstantException (org.joda.time.IllegalInstantException)1 ISOChronology (org.joda.time.chrono.ISOChronology)1 UnsupportedDurationField (org.joda.time.field.UnsupportedDurationField)1