Search in sources :

Example 11 with DurationField

use of org.joda.time.DurationField 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);
}
Also used : Chronology(org.joda.time.Chronology) ISOChronology(org.joda.time.chrono.ISOChronology) DurationField(org.joda.time.DurationField)

Example 12 with DurationField

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

the class TestPreciseDurationField method test_hashCode.

public void test_hashCode() {
    assertEquals(true, iField.hashCode() == iField.hashCode());
    assertEquals(false, iField.hashCode() == ISOChronology.getInstance().minutes().hashCode());
    DurationField dummy = new PreciseDurationField(DurationFieldType.seconds(), 0);
    assertEquals(false, iField.hashCode() == dummy.hashCode());
    dummy = new PreciseDurationField(DurationFieldType.seconds(), 1000);
    assertEquals(true, iField.hashCode() == dummy.hashCode());
    dummy = new PreciseDurationField(DurationFieldType.millis(), 1000);
    assertEquals(false, iField.hashCode() == dummy.hashCode());
}
Also used : DurationField(org.joda.time.DurationField)

Example 13 with DurationField

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

the class TestPreciseDurationField method test_equals.

//-----------------------------------------------------------------------
public void test_equals() {
    assertEquals(true, iField.equals(iField));
    assertEquals(false, iField.equals(ISOChronology.getInstance().minutes()));
    DurationField dummy = new PreciseDurationField(DurationFieldType.seconds(), 0);
    assertEquals(false, iField.equals(dummy));
    dummy = new PreciseDurationField(DurationFieldType.seconds(), 1000);
    assertEquals(true, iField.equals(dummy));
    dummy = new PreciseDurationField(DurationFieldType.millis(), 1000);
    assertEquals(false, iField.equals(dummy));
    assertEquals(false, iField.equals(""));
    assertEquals(false, iField.equals(null));
}
Also used : DurationField(org.joda.time.DurationField)

Example 14 with DurationField

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

the class TestPreciseDurationField method testSerialization.

//-----------------------------------------------------------------------
public void testSerialization() throws Exception {
    DurationField test = iField;
    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();
    assertEquals(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 15 with DurationField

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

the class TestPreciseDurationField method test_compareTo.

//-----------------------------------------------------------------------
public void test_compareTo() {
    assertEquals(0, iField.compareTo(iField));
    assertEquals(-1, iField.compareTo(ISOChronology.getInstance().minutes()));
    DurationField dummy = new PreciseDurationField(DurationFieldType.seconds(), 0);
    assertEquals(1, iField.compareTo(dummy));
    //        } catch (ClassCastException ex) {}
    try {
        iField.compareTo(null);
        fail();
    } catch (NullPointerException ex) {
    }
}
Also used : 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