Search in sources :

Example 6 with DurationField

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

the class BaseChronology method get.

//-----------------------------------------------------------------------
/**
     * Gets the values of a period from an interval.
     *
     * @param period  the period instant to use
     * @param startInstant  the start instant of an interval to query
     * @param endInstant  the start instant of an interval to query
     * @return the values of the period extracted from the interval
     */
public int[] get(ReadablePeriod period, long startInstant, long endInstant) {
    int size = period.size();
    int[] values = new int[size];
    if (startInstant != endInstant) {
        for (int i = 0; i < size; i++) {
            DurationField field = period.getFieldType(i).getField(this);
            int value = field.getDifference(endInstant, startInstant);
            if (value != 0) {
                startInstant = field.add(startInstant, value);
            }
            values[i] = value;
        }
    }
    return values;
}
Also used : DurationField(org.joda.time.DurationField) UnsupportedDurationField(org.joda.time.field.UnsupportedDurationField)

Example 7 with DurationField

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

the class TestISOChronology method testAdd.

private void testAdd(String start, DurationFieldType type, int amt, String end) {
    DateTime dtStart = new DateTime(start, ISOChronology.getInstanceUTC());
    DateTime dtEnd = new DateTime(end, ISOChronology.getInstanceUTC());
    assertEquals(dtEnd, dtStart.withFieldAdded(type, amt));
    assertEquals(dtStart, dtEnd.withFieldAdded(type, -amt));
    DurationField field = type.getField(ISOChronology.getInstanceUTC());
    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, ISOChronology.getInstanceUTC());
        YearMonthDay ymdEnd = new YearMonthDay(end, ISOChronology.getInstanceUTC());
        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 8 with DurationField

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

the class TestMillisDurationField method test_compareTo.

//-----------------------------------------------------------------------
public void test_compareTo() {
    assertEquals(0, MillisDurationField.INSTANCE.compareTo(MillisDurationField.INSTANCE));
    assertEquals(-1, MillisDurationField.INSTANCE.compareTo(ISOChronology.getInstance().seconds()));
    DurationField dummy = new PreciseDurationField(DurationFieldType.seconds(), 0);
    assertEquals(1, MillisDurationField.INSTANCE.compareTo(dummy));
    //        } catch (ClassCastException ex) {}
    try {
        MillisDurationField.INSTANCE.compareTo(null);
        fail();
    } catch (NullPointerException ex) {
    }
}
Also used : DurationField(org.joda.time.DurationField)

Example 9 with DurationField

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

the class TestEthiopicChronology method testDurationMonth.

public void testDurationMonth() {
    // Leap 1999, NotLeap 1996,97,98
    DateTime dt11 = new DateTime(1999, 11, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
    DateTime dt12 = new DateTime(1999, 12, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
    DateTime dt13 = new DateTime(1999, 13, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
    DateTime dt01 = new DateTime(2000, 1, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
    DurationField fld = dt11.monthOfYear().getDurationField();
    assertEquals(ETHIOPIC_UTC.months(), fld);
    assertEquals(1L * 30L * MILLIS_PER_DAY, fld.getMillis(1, dt11.getMillis()));
    assertEquals(2L * 30L * MILLIS_PER_DAY, fld.getMillis(2, dt11.getMillis()));
    assertEquals((2L * 30L + 6L) * MILLIS_PER_DAY, fld.getMillis(3, dt11.getMillis()));
    assertEquals((3L * 30L + 6L) * MILLIS_PER_DAY, fld.getMillis(4, dt11.getMillis()));
    assertEquals(1L * 30L * MILLIS_PER_DAY, fld.getMillis(1));
    assertEquals(2L * 30L * MILLIS_PER_DAY, fld.getMillis(2));
    assertEquals(13L * 30L * MILLIS_PER_DAY, fld.getMillis(13));
    assertEquals(1L * 30L * MILLIS_PER_DAY, fld.getMillis(1L, dt11.getMillis()));
    assertEquals(2L * 30L * MILLIS_PER_DAY, fld.getMillis(2L, dt11.getMillis()));
    assertEquals((2L * 30L + 6L) * MILLIS_PER_DAY, fld.getMillis(3L, dt11.getMillis()));
    assertEquals((3L * 30L + 6L) * MILLIS_PER_DAY, fld.getMillis(4L, dt11.getMillis()));
    assertEquals(1L * 30L * MILLIS_PER_DAY, fld.getMillis(1L));
    assertEquals(2L * 30L * MILLIS_PER_DAY, fld.getMillis(2L));
    assertEquals(13L * 30L * MILLIS_PER_DAY, fld.getMillis(13L));
    assertEquals(0, fld.getValue(1L * 30L * MILLIS_PER_DAY - 1L, dt11.getMillis()));
    assertEquals(1, fld.getValue(1L * 30L * MILLIS_PER_DAY, dt11.getMillis()));
    assertEquals(1, fld.getValue(1L * 30L * MILLIS_PER_DAY + 1L, dt11.getMillis()));
    assertEquals(1, fld.getValue(2L * 30L * MILLIS_PER_DAY - 1L, dt11.getMillis()));
    assertEquals(2, fld.getValue(2L * 30L * MILLIS_PER_DAY, dt11.getMillis()));
    assertEquals(2, fld.getValue(2L * 30L * MILLIS_PER_DAY + 1L, dt11.getMillis()));
    assertEquals(2, fld.getValue((2L * 30L + 6L) * MILLIS_PER_DAY - 1L, dt11.getMillis()));
    assertEquals(3, fld.getValue((2L * 30L + 6L) * MILLIS_PER_DAY, dt11.getMillis()));
    assertEquals(3, fld.getValue((2L * 30L + 6L) * MILLIS_PER_DAY + 1L, dt11.getMillis()));
    assertEquals(3, fld.getValue((3L * 30L + 6L) * MILLIS_PER_DAY - 1L, dt11.getMillis()));
    assertEquals(4, fld.getValue((3L * 30L + 6L) * MILLIS_PER_DAY, dt11.getMillis()));
    assertEquals(4, fld.getValue((3L * 30L + 6L) * MILLIS_PER_DAY + 1L, dt11.getMillis()));
    assertEquals(dt12.getMillis(), fld.add(dt11.getMillis(), 1));
    assertEquals(dt13.getMillis(), fld.add(dt11.getMillis(), 2));
    assertEquals(dt01.getMillis(), fld.add(dt11.getMillis(), 3));
    assertEquals(dt12.getMillis(), fld.add(dt11.getMillis(), 1L));
    assertEquals(dt13.getMillis(), fld.add(dt11.getMillis(), 2L));
    assertEquals(dt01.getMillis(), fld.add(dt11.getMillis(), 3L));
}
Also used : DurationField(org.joda.time.DurationField) DateTime(org.joda.time.DateTime)

Example 10 with DurationField

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

the class TestEthiopicChronology method testDurationYear.

public void testDurationYear() {
    // Leap 1999, NotLeap 1996,97,98
    DateTime dt96 = new DateTime(1996, 10, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
    DateTime dt97 = new DateTime(1997, 10, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
    DateTime dt98 = new DateTime(1998, 10, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
    DateTime dt99 = new DateTime(1999, 10, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
    DateTime dt00 = new DateTime(2000, 10, 2, 0, 0, 0, 0, ETHIOPIC_UTC);
    DurationField fld = dt96.year().getDurationField();
    assertEquals(ETHIOPIC_UTC.years(), fld);
    assertEquals(1L * 365L * MILLIS_PER_DAY, fld.getMillis(1, dt96.getMillis()));
    assertEquals(2L * 365L * MILLIS_PER_DAY, fld.getMillis(2, dt96.getMillis()));
    assertEquals(3L * 365L * MILLIS_PER_DAY, fld.getMillis(3, dt96.getMillis()));
    assertEquals((4L * 365L + 1L) * MILLIS_PER_DAY, fld.getMillis(4, dt96.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, dt96.getMillis()));
    assertEquals(2L * 365L * MILLIS_PER_DAY, fld.getMillis(2L, dt96.getMillis()));
    assertEquals(3L * 365L * MILLIS_PER_DAY, fld.getMillis(3L, dt96.getMillis()));
    assertEquals((4L * 365L + 1L) * MILLIS_PER_DAY, fld.getMillis(4L, dt96.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, dt96.getMillis()));
    assertEquals(1, fld.getValue(1L * 365L * MILLIS_PER_DAY, dt96.getMillis()));
    assertEquals(1, fld.getValue(1L * 365L * MILLIS_PER_DAY + 1L, dt96.getMillis()));
    assertEquals(1, fld.getValue(2L * 365L * MILLIS_PER_DAY - 1L, dt96.getMillis()));
    assertEquals(2, fld.getValue(2L * 365L * MILLIS_PER_DAY, dt96.getMillis()));
    assertEquals(2, fld.getValue(2L * 365L * MILLIS_PER_DAY + 1L, dt96.getMillis()));
    assertEquals(2, fld.getValue(3L * 365L * MILLIS_PER_DAY - 1L, dt96.getMillis()));
    assertEquals(3, fld.getValue(3L * 365L * MILLIS_PER_DAY, dt96.getMillis()));
    assertEquals(3, fld.getValue(3L * 365L * MILLIS_PER_DAY + 1L, dt96.getMillis()));
    assertEquals(3, fld.getValue((4L * 365L + 1L) * MILLIS_PER_DAY - 1L, dt96.getMillis()));
    assertEquals(4, fld.getValue((4L * 365L + 1L) * MILLIS_PER_DAY, dt96.getMillis()));
    assertEquals(4, fld.getValue((4L * 365L + 1L) * MILLIS_PER_DAY + 1L, dt96.getMillis()));
    assertEquals(dt97.getMillis(), fld.add(dt96.getMillis(), 1));
    assertEquals(dt98.getMillis(), fld.add(dt96.getMillis(), 2));
    assertEquals(dt99.getMillis(), fld.add(dt96.getMillis(), 3));
    assertEquals(dt00.getMillis(), fld.add(dt96.getMillis(), 4));
    assertEquals(dt97.getMillis(), fld.add(dt96.getMillis(), 1L));
    assertEquals(dt98.getMillis(), fld.add(dt96.getMillis(), 2L));
    assertEquals(dt99.getMillis(), fld.add(dt96.getMillis(), 3L));
    assertEquals(dt00.getMillis(), fld.add(dt96.getMillis(), 4L));
}
Also used : DurationField(org.joda.time.DurationField) DateTime(org.joda.time.DateTime)

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