Search in sources :

Example 1 with IllegalInstantException

use of org.joda.time.IllegalInstantException in project killbill by killbill.

the class TestWithTimeZones method testReferenceTimeInDSTGap.

@Test(groups = "slow")
public void testReferenceTimeInDSTGap() throws Exception {
    final DateTimeZone tz = DateTimeZone.forID("America/Los_Angeles");
    clock.setTime(new DateTime(2015, 3, 7, 2, 0, 0, tz));
    final AccountData accountData = new MockAccountBuilder().currency(Currency.USD).timeZone(tz).build();
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    Assert.assertEquals(account.getTimeZone(), tz);
    Assert.assertEquals(account.getFixedOffsetTimeZone(), DateTimeZone.forOffsetHours(-8));
    // Note the gap: 2015-03-07T02:00:00.000-08:00 to 2015-03-08T03:00:00.000-07:00
    clock.addDays(1);
    try {
        // See TimeAwareContext#toUTCDateTime (which uses account.getFixedOffsetTimeZone() instead)
        new DateTime(clock.getUTCToday().getYear(), clock.getUTCToday().getMonthOfYear(), clock.getUTCToday().getDayOfMonth(), account.getReferenceTime().toDateTime(tz).getHourOfDay(), account.getReferenceTime().toDateTime(tz).getMinuteOfHour(), account.getReferenceTime().toDateTime(tz).getSecondOfMinute(), account.getTimeZone());
        Assert.fail();
    } catch (final IllegalInstantException e) {
    // Illegal instant due to time zone offset transition (daylight savings time 'gap'): 2015-03-08T10:00:00.000 (America/Los_Angeles)
    }
    busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Blowdart", BillingPeriod.MONTHLY, "notrial", null);
    // Pass a date of today, to trigger TimeAwareContext#toUTCDateTime
    final Entitlement entitlement = entitlementApi.createBaseEntitlement(account.getId(), spec, "Something", ImmutableList.<PlanPhasePriceOverride>of(), clock.getUTCToday(), clock.getUTCToday(), false, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    Assert.assertEquals(entitlement.getEffectiveStartDate().compareTo(new LocalDate("2015-03-08")), 0);
    Assert.assertEquals(((DefaultEntitlement) entitlement).getBasePlanSubscriptionBase().getStartDate().compareTo(new DateTime("2015-03-08T02:00:00.000-08:00")), 0);
    invoiceChecker.checkChargedThroughDate(entitlement.getId(), new LocalDate("2015-04-08"), callContext);
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addDays(31);
    assertListenerStatus();
    invoiceChecker.checkChargedThroughDate(entitlement.getId(), new LocalDate("2015-05-08"), callContext);
    for (int i = 0; i < 25; i++) {
        busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
        clock.addMonths(1);
        assertListenerStatus();
        invoiceChecker.checkChargedThroughDate(entitlement.getId(), new LocalDate("2015-03-08").plusMonths(3 + i), callContext);
    }
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) MockAccountBuilder(org.killbill.billing.mock.MockAccountBuilder) AccountData(org.killbill.billing.account.api.AccountData) IllegalInstantException(org.joda.time.IllegalInstantException) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Entitlement(org.killbill.billing.entitlement.api.Entitlement) LocalDate(org.joda.time.LocalDate) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 2 with IllegalInstantException

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

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

the class ZonedChronology method localToUTC.

/**
     * @param localInstant  the instant from 1970-01-01T00:00:00 local time
     * @return the instant from 1970-01-01T00:00:00Z
     */
private long localToUTC(long localInstant) {
    if (localInstant == Long.MAX_VALUE) {
        return Long.MAX_VALUE;
    } else if (localInstant == Long.MIN_VALUE) {
        return Long.MIN_VALUE;
    }
    DateTimeZone zone = getZone();
    int offset = zone.getOffsetFromLocal(localInstant);
    long utcInstant = localInstant - offset;
    if (localInstant > NEAR_ZERO && utcInstant < 0) {
        return Long.MAX_VALUE;
    } else if (localInstant < -NEAR_ZERO && utcInstant > 0) {
        return Long.MIN_VALUE;
    }
    int offsetBasedOnUtc = zone.getOffset(utcInstant);
    if (offset != offsetBasedOnUtc) {
        throw new IllegalInstantException(localInstant, zone.getID());
    }
    return utcInstant;
}
Also used : IllegalInstantException(org.joda.time.IllegalInstantException) DateTimeZone(org.joda.time.DateTimeZone)

Aggregations

IllegalInstantException (org.joda.time.IllegalInstantException)3 DateTimeZone (org.joda.time.DateTimeZone)2 DateTime (org.joda.time.DateTime)1 DurationField (org.joda.time.DurationField)1 IllegalFieldValueException (org.joda.time.IllegalFieldValueException)1 LocalDate (org.joda.time.LocalDate)1 Account (org.killbill.billing.account.api.Account)1 AccountData (org.killbill.billing.account.api.AccountData)1 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)1 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)1 Entitlement (org.killbill.billing.entitlement.api.Entitlement)1 MockAccountBuilder (org.killbill.billing.mock.MockAccountBuilder)1 Test (org.testng.annotations.Test)1