Search in sources :

Example 1 with IllegalFieldValueException

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

the class TestISOChronology method testMaxYear.

public void testMaxYear() {
    final ISOChronology chrono = ISOChronology.getInstanceUTC();
    final int maxYear = chrono.year().getMaximumValue();
    DateTime start = new DateTime(maxYear, 1, 1, 0, 0, 0, 0, chrono);
    DateTime end = new DateTime(maxYear, 12, 31, 23, 59, 59, 999, chrono);
    assertTrue(start.getMillis() > 0);
    assertTrue(end.getMillis() > start.getMillis());
    assertEquals(maxYear, start.getYear());
    assertEquals(maxYear, end.getYear());
    long delta = end.getMillis() - start.getMillis();
    long expectedDelta = (start.year().isLeap() ? 366L : 365L) * DateTimeConstants.MILLIS_PER_DAY - 1;
    assertEquals(expectedDelta, delta);
    assertEquals(start, new DateTime(maxYear + "-01-01T00:00:00.000Z", chrono));
    assertEquals(end, new DateTime(maxYear + "-12-31T23:59:59.999Z", chrono));
    try {
        start.plusYears(1);
        fail();
    } catch (IllegalFieldValueException e) {
    }
    try {
        end.plusYears(1);
        fail();
    } catch (IllegalFieldValueException e) {
    }
    assertEquals(maxYear + 1, chrono.year().get(Long.MAX_VALUE));
}
Also used : IllegalFieldValueException(org.joda.time.IllegalFieldValueException) DateTime(org.joda.time.DateTime)

Example 2 with IllegalFieldValueException

use of org.joda.time.IllegalFieldValueException 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)

Example 3 with IllegalFieldValueException

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

the class GJChronology method getDateTimeMillis.

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);
    }
    // Assume date is Gregorian.
    long instant;
    try {
        instant = iGregorianChronology.getDateTimeMillis(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
    } catch (IllegalFieldValueException ex) {
        if (monthOfYear != 2 || dayOfMonth != 29) {
            throw ex;
        }
        instant = iGregorianChronology.getDateTimeMillis(year, monthOfYear, 28, hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
        if (instant >= iCutoverMillis) {
            throw ex;
        }
    }
    if (instant < iCutoverMillis) {
        // Maybe it's Julian.
        instant = iJulianChronology.getDateTimeMillis(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
        if (instant >= iCutoverMillis) {
            // Okay, it's in the illegal cutover gap.
            throw new IllegalArgumentException("Specified date does not exist");
        }
    }
    return instant;
}
Also used : IllegalFieldValueException(org.joda.time.IllegalFieldValueException) Chronology(org.joda.time.Chronology)

Example 4 with IllegalFieldValueException

use of org.joda.time.IllegalFieldValueException in project h2o-3 by h2oai.

the class AstMoment method exec.

@Override
protected ValFrame exec(Val[] args) {
    // Parse the input arguments, verifying their validity.
    boolean naResult = false;
    long numRows = -1;
    int[] timeparts = new int[7];
    ArrayList<Integer> chunksmap = new ArrayList<>(7);
    ArrayList<Vec> timevecs = new ArrayList<>(7);
    for (int i = 0; i < 7; i++) {
        Val vi = args[i + 1];
        if (vi.isFrame()) {
            Frame fr = vi.getFrame();
            if (fr.numCols() != 1)
                throw new IllegalArgumentException("Argument " + i + " is a frame with " + fr.numCols() + " columns");
            if (!fr.vec(0).isNumeric())
                throw new IllegalArgumentException("Argument " + i + " is not a numeric column");
            if (fr.numRows() == 0)
                throw new IllegalArgumentException("Column " + i + " has 0 rows");
            if (fr.numRows() == 1) {
                double d = fr.vec(0).at(0);
                if (Double.isNaN(d))
                    naResult = true;
                else
                    timeparts[i] = (int) d;
            } else {
                if (numRows == -1)
                    numRows = fr.numRows();
                if (fr.numRows() != numRows)
                    throw new IllegalArgumentException("Incompatible vec " + i + " having " + fr.numRows() + " rows, whereas " + "other vecs have " + numRows + " rows.");
                timevecs.add(fr.vec(0));
                chunksmap.add(i);
            }
        } else if (vi.isNum()) {
            double d = vi.getNum();
            if (Double.isNaN(d))
                naResult = true;
            else
                timeparts[i] = (int) d;
        } else {
            throw new IllegalArgumentException("Argument " + i + " is neither a number nor a frame");
        }
    }
    // If all arguments are scalars, return a 1x1 frame
    if (timevecs.isEmpty()) {
        double val = Double.NaN;
        if (!naResult) {
            try {
                val = ISOChronology.getInstanceUTC().getDateTimeMillis(timeparts[0], timeparts[1], timeparts[2], timeparts[3], timeparts[4], timeparts[5], timeparts[6]);
            } catch (IllegalFieldValueException ignored) {
            }
        }
        return make1x1Frame(val);
    }
    // If the result is all-NAs, make a constant NA vec
    if (naResult) {
        long n = timevecs.get(0).length();
        Vec v = Vec.makeCon(Double.NaN, n, Vec.T_TIME);
        Frame fr = new Frame(Key.<Frame>make(), new String[] { "time" }, new Vec[] { v });
        return new ValFrame(fr);
    }
    // Some arguments are vecs -- create a frame of the same size
    Vec[] vecs = timevecs.toArray(new Vec[timevecs.size()]);
    int[] cm = ArrayUtils.toPrimitive(chunksmap);
    Frame fr = new SetTimeTask(timeparts, cm).doAll(Vec.T_TIME, vecs).outputFrame(new String[] { "time" }, null);
    return new ValFrame(fr);
}
Also used : Val(water.rapids.Val) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) IllegalFieldValueException(org.joda.time.IllegalFieldValueException) ArrayList(java.util.ArrayList) ValFrame(water.rapids.vals.ValFrame) Vec(water.fvec.Vec)

Example 5 with IllegalFieldValueException

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

the class TestISOChronology method testMinYear.

public void testMinYear() {
    final ISOChronology chrono = ISOChronology.getInstanceUTC();
    final int minYear = chrono.year().getMinimumValue();
    DateTime start = new DateTime(minYear, 1, 1, 0, 0, 0, 0, chrono);
    DateTime end = new DateTime(minYear, 12, 31, 23, 59, 59, 999, chrono);
    assertTrue(start.getMillis() < 0);
    assertTrue(end.getMillis() > start.getMillis());
    assertEquals(minYear, start.getYear());
    assertEquals(minYear, end.getYear());
    long delta = end.getMillis() - start.getMillis();
    long expectedDelta = (start.year().isLeap() ? 366L : 365L) * DateTimeConstants.MILLIS_PER_DAY - 1;
    assertEquals(expectedDelta, delta);
    assertEquals(start, new DateTime(minYear + "-01-01T00:00:00.000Z", chrono));
    assertEquals(end, new DateTime(minYear + "-12-31T23:59:59.999Z", chrono));
    try {
        start.minusYears(1);
        fail();
    } catch (IllegalFieldValueException e) {
    }
    try {
        end.minusYears(1);
        fail();
    } catch (IllegalFieldValueException e) {
    }
    assertEquals(minYear - 1, chrono.year().get(Long.MIN_VALUE));
}
Also used : IllegalFieldValueException(org.joda.time.IllegalFieldValueException) DateTime(org.joda.time.DateTime)

Aggregations

IllegalFieldValueException (org.joda.time.IllegalFieldValueException)6 DateTime (org.joda.time.DateTime)2 ArrayList (java.util.ArrayList)1 Chronology (org.joda.time.Chronology)1 DateTimeField (org.joda.time.DateTimeField)1 DurationField (org.joda.time.DurationField)1 IllegalInstantException (org.joda.time.IllegalInstantException)1 UnsupportedDateTimeField (org.joda.time.field.UnsupportedDateTimeField)1 Frame (water.fvec.Frame)1 Vec (water.fvec.Vec)1 Val (water.rapids.Val)1 ValFrame (water.rapids.vals.ValFrame)1