Search in sources :

Example 6 with DateTimeField

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

the class TestUnsupportedDateTimeField method testAlwaysFalseReturnTypes.

/**
     * As this is an unsupported date/time field, some normal methods will
     * always return false, as they are not supported. Verify that each method
     * correctly returns null.
     */
public void testAlwaysFalseReturnTypes() {
    DateTimeField fieldOne = UnsupportedDateTimeField.getInstance(dateTimeFieldTypeOne, UnsupportedDurationField.getInstance(weeks));
    assertFalse(fieldOne.isLenient());
    assertFalse(fieldOne.isSupported());
}
Also used : DateTimeField(org.joda.time.DateTimeField)

Example 7 with DateTimeField

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

the class TestUnsupportedDateTimeField method testDelegatedMethods.

/**
     * As this is an unsupported date/time field, many normal methods are
     * unsupported. Some delegate and can possibly throw an
     * UnsupportedOperationException or have a valid return. Verify that each
     * method correctly throws this exception when appropriate and delegates
     * correctly based on the Duration used to get the instance.
     */
public void testDelegatedMethods() {
    DateTimeField fieldOne = UnsupportedDateTimeField.getInstance(dateTimeFieldTypeOne, UnsupportedDurationField.getInstance(weeks));
    PreciseDurationField hoursDuration = new PreciseDurationField(DurationFieldType.hours(), 10L);
    DateTimeField fieldTwo = UnsupportedDateTimeField.getInstance(dateTimeFieldTypeOne, hoursDuration);
    // try it with an UnsupportedDurationField, then a PreciseDurationField.
    try {
        fieldOne.add(System.currentTimeMillis(), 100);
        assertTrue(false);
    } catch (UnsupportedOperationException e) {
        assertTrue(true);
    }
    try {
        long currentTime = System.currentTimeMillis();
        long firstComputation = hoursDuration.add(currentTime, 100);
        long secondComputation = fieldTwo.add(currentTime, 100);
        assertEquals(firstComputation, secondComputation);
    } catch (UnsupportedOperationException e) {
        assertTrue(false);
    }
    // try it with an UnsupportedDurationField, then a PreciseDurationField.
    try {
        fieldOne.add(System.currentTimeMillis(), 1000L);
        assertTrue(false);
    } catch (UnsupportedOperationException e) {
        assertTrue(true);
    }
    try {
        long currentTime = System.currentTimeMillis();
        long firstComputation = hoursDuration.add(currentTime, 1000L);
        long secondComputation = fieldTwo.add(currentTime, 1000L);
        assertTrue(firstComputation == secondComputation);
        assertEquals(firstComputation, secondComputation);
    } catch (UnsupportedOperationException e) {
        assertTrue(false);
    }
    // delegated call.
    try {
        fieldOne.getDifference(100000L, 1000L);
        assertTrue(false);
    } catch (UnsupportedOperationException e) {
        assertTrue(true);
    }
    try {
        int firstDifference = hoursDuration.getDifference(100000L, 1000L);
        int secondDifference = fieldTwo.getDifference(100000L, 1000L);
        assertEquals(firstDifference, secondDifference);
    } catch (UnsupportedOperationException e) {
        assertTrue(false);
    }
    // delegated call.
    try {
        fieldOne.getDifferenceAsLong(100000L, 1000L);
        assertTrue(false);
    } catch (UnsupportedOperationException e) {
        assertTrue(true);
    }
    try {
        long firstDifference = hoursDuration.getDifference(100000L, 1000L);
        long secondDifference = fieldTwo.getDifference(100000L, 1000L);
        assertEquals(firstDifference, secondDifference);
    } catch (UnsupportedOperationException e) {
        assertTrue(false);
    }
}
Also used : DateTimeField(org.joda.time.DateTimeField)

Example 8 with DateTimeField

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

the class BaseDateTimeField method addWrapPartial.

/**
     * Adds a value (which may be negative) to the partial instant,
     * wrapping the whole partial if the maximum size of the partial is reached.
     * <p>
     * The value will be added to this field, overflowing into larger fields
     * if necessary. Smaller fields should be unaffected, except where the
     * result would be an invalid value for a smaller field. In this case the
     * smaller field is adjusted to be in range.
     * <p>
     * Partial instants only contain some fields. This may result in a maximum
     * possible value, such as TimeOfDay normally being limited to 23:59:59:999.
     * If this limit is reached by the addition, this method will wrap back to
     * 00:00:00.000. In fact, you would generally only use this method for
     * classes that have a limitation such as this.
     * <p>
     * For example, in the ISO chronology:<br>
     * 10:20:30 add 20 minutes is 10:40:30<br>
     * 10:20:30 add 45 minutes is 11:05:30<br>
     * 10:20:30 add 16 hours is 02:20:30<br>
     * 
     * @param instant  the partial instant
     * @param fieldIndex  the index of this field in the partial
     * @param values  the values of the partial instant which should be updated
     * @param valueToAdd  the value to add, in the units of the field
     * @return the passed in values
     * @throws IllegalArgumentException if the value is invalid or the maximum instant is reached
     */
public int[] addWrapPartial(ReadablePartial instant, int fieldIndex, int[] values, int valueToAdd) {
    if (valueToAdd == 0) {
        return values;
    }
    // there are more efficient algorithms than this (especially for time only fields)
    // trouble is when dealing with days and months, so we use this technique of
    // adding/removing one from the larger field at a time
    DateTimeField nextField = null;
    while (valueToAdd > 0) {
        int max = getMaximumValue(instant, values);
        long proposed = values[fieldIndex] + valueToAdd;
        if (proposed <= max) {
            values[fieldIndex] = (int) proposed;
            break;
        }
        if (nextField == null) {
            if (fieldIndex == 0) {
                valueToAdd -= (max + 1) - values[fieldIndex];
                values[fieldIndex] = getMinimumValue(instant, values);
                continue;
            }
            nextField = instant.getField(fieldIndex - 1);
            // test only works if this field is UTC (ie. local)
            if (getRangeDurationField().getType() != nextField.getDurationField().getType()) {
                throw new IllegalArgumentException("Fields invalid for add");
            }
        }
        // reduce the amount to add
        valueToAdd -= (max + 1) - values[fieldIndex];
        // add 1 to next bigger field
        values = nextField.addWrapPartial(instant, fieldIndex - 1, values, 1);
        // reset this field to zero
        values[fieldIndex] = getMinimumValue(instant, values);
    }
    while (valueToAdd < 0) {
        int min = getMinimumValue(instant, values);
        long proposed = values[fieldIndex] + valueToAdd;
        if (proposed >= min) {
            values[fieldIndex] = (int) proposed;
            break;
        }
        if (nextField == null) {
            if (fieldIndex == 0) {
                valueToAdd -= (min - 1) - values[fieldIndex];
                values[fieldIndex] = getMaximumValue(instant, values);
                continue;
            }
            nextField = instant.getField(fieldIndex - 1);
            if (getRangeDurationField().getType() != nextField.getDurationField().getType()) {
                throw new IllegalArgumentException("Fields invalid for add");
            }
        }
        // reduce the amount to add
        valueToAdd -= (min - 1) - values[fieldIndex];
        // subtract 1 from next bigger field
        values = nextField.addWrapPartial(instant, fieldIndex - 1, values, -1);
        // reset this field to max value
        values[fieldIndex] = getMaximumValue(instant, values);
    }
    // adjusts smaller fields
    return set(instant, fieldIndex, values, values[fieldIndex]);
}
Also used : DateTimeField(org.joda.time.DateTimeField)

Example 9 with DateTimeField

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

the class BaseDateTimeField method set.

/**
     * Sets a value using the specified partial instant.
     * <p>
     * The value of this field (specified by the index) will be set.
     * If the value is invalid, an exception if thrown.
     * <p>
     * If setting this field would make other fields invalid, then those fields
     * may be changed. For example if the current date is the 31st January, and
     * the month is set to February, the day would be invalid. Instead, the day
     * would be changed to the closest value - the 28th/29th February as appropriate.
     * 
     * @param partial  the partial instant
     * @param fieldIndex  the index of this field in the instant
     * @param values  the values to update
     * @param newValue  the value to set, in the units of the field
     * @return the updated values
     * @throws IllegalArgumentException if the value is invalid
     */
public int[] set(ReadablePartial partial, int fieldIndex, int[] values, int newValue) {
    FieldUtils.verifyValueBounds(this, newValue, getMinimumValue(partial, values), getMaximumValue(partial, values));
    values[fieldIndex] = newValue;
    // may need to adjust smaller fields
    for (int i = fieldIndex + 1; i < partial.size(); i++) {
        DateTimeField field = partial.getField(i);
        if (values[i] > field.getMaximumValue(partial, values)) {
            values[i] = field.getMaximumValue(partial, values);
        }
        if (values[i] < field.getMinimumValue(partial, values)) {
            values[i] = field.getMinimumValue(partial, values);
        }
    }
    return values;
}
Also used : DateTimeField(org.joda.time.DateTimeField)

Example 10 with DateTimeField

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

the class BuddhistChronology method assemble.

protected void assemble(Fields fields) {
    if (getParam() == null) {
        // force init as used below
        fields.eras = UnsupportedDurationField.getInstance(DurationFieldType.eras());
        // julian chrono removed zero, but we need to put it back
        DateTimeField field = fields.year;
        fields.year = new OffsetDateTimeField(new SkipUndoDateTimeField(this, field), BUDDHIST_OFFSET);
        // one era, so yearOfEra is the same
        field = fields.yearOfEra;
        fields.yearOfEra = new DelegatedDateTimeField(fields.year, fields.eras, DateTimeFieldType.yearOfEra());
        // julian chrono removed zero, but we need to put it back
        field = fields.weekyear;
        fields.weekyear = new OffsetDateTimeField(new SkipUndoDateTimeField(this, field), BUDDHIST_OFFSET);
        field = new OffsetDateTimeField(fields.yearOfEra, 99);
        fields.centuryOfEra = new DividedDateTimeField(field, fields.eras, DateTimeFieldType.centuryOfEra(), 100);
        fields.centuries = fields.centuryOfEra.getDurationField();
        field = new RemainderDateTimeField((DividedDateTimeField) fields.centuryOfEra);
        fields.yearOfCentury = new OffsetDateTimeField(field, DateTimeFieldType.yearOfCentury(), 1);
        field = new RemainderDateTimeField(fields.weekyear, fields.centuries, DateTimeFieldType.weekyearOfCentury(), 100);
        fields.weekyearOfCentury = new OffsetDateTimeField(field, DateTimeFieldType.weekyearOfCentury(), 1);
        fields.era = ERA_FIELD;
    }
}
Also used : OffsetDateTimeField(org.joda.time.field.OffsetDateTimeField) SkipUndoDateTimeField(org.joda.time.field.SkipUndoDateTimeField) RemainderDateTimeField(org.joda.time.field.RemainderDateTimeField) OffsetDateTimeField(org.joda.time.field.OffsetDateTimeField) SkipUndoDateTimeField(org.joda.time.field.SkipUndoDateTimeField) RemainderDateTimeField(org.joda.time.field.RemainderDateTimeField) DelegatedDateTimeField(org.joda.time.field.DelegatedDateTimeField) DateTimeField(org.joda.time.DateTimeField) DividedDateTimeField(org.joda.time.field.DividedDateTimeField) DelegatedDateTimeField(org.joda.time.field.DelegatedDateTimeField) DividedDateTimeField(org.joda.time.field.DividedDateTimeField)

Aggregations

DateTimeField (org.joda.time.DateTimeField)20 DateTime (org.joda.time.DateTime)5 DividedDateTimeField (org.joda.time.field.DividedDateTimeField)2 OffsetDateTimeField (org.joda.time.field.OffsetDateTimeField)2 RemainderDateTimeField (org.joda.time.field.RemainderDateTimeField)2 DateTimeFieldType (org.joda.time.DateTimeFieldType)1 IllegalFieldValueException (org.joda.time.IllegalFieldValueException)1 Interval (org.joda.time.Interval)1 Period (org.joda.time.Period)1 DelegatedDateTimeField (org.joda.time.field.DelegatedDateTimeField)1 PreciseDateTimeField (org.joda.time.field.PreciseDateTimeField)1 SkipUndoDateTimeField (org.joda.time.field.SkipUndoDateTimeField)1 UnsupportedDateTimeField (org.joda.time.field.UnsupportedDateTimeField)1 ZeroIsMaxDateTimeField (org.joda.time.field.ZeroIsMaxDateTimeField)1