Search in sources :

Example 1 with ReadableInstant

use of org.joda.time.ReadableInstant in project elasticsearch by elastic.

the class BaseXContentTestCase method testReadableInstant.

public void testReadableInstant() throws Exception {
    assertResult("{'instant':null}", () -> builder().startObject().field("instant", (ReadableInstant) null).endObject());
    assertResult("{'instant':null}", () -> builder().startObject().field("instant").value((ReadableInstant) null).endObject());
    final DateTime t1 = new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC);
    String expected = "{'t1':'2016-01-01T00:00:00.000Z'}";
    assertResult(expected, () -> builder().startObject().field("t1", t1).endObject());
    assertResult(expected, () -> builder().startObject().field("t1").value(t1).endObject());
    final DateTime t2 = new DateTime(2016, 12, 25, 7, 59, 42, 213, DateTimeZone.UTC);
    expected = "{'t2':'2016-12-25T07:59:42.213Z'}";
    assertResult(expected, () -> builder().startObject().field("t2", t2).endObject());
    assertResult(expected, () -> builder().startObject().field("t2").value(t2).endObject());
    final DateTimeFormatter formatter = randomFrom(ISODateTimeFormat.basicDate(), ISODateTimeFormat.dateTimeNoMillis());
    final DateTime t3 = DateTime.now();
    expected = "{'t3':'" + formatter.print(t3) + "'}";
    assertResult(expected, () -> builder().startObject().field("t3", t3, formatter).endObject());
    assertResult(expected, () -> builder().startObject().field("t3").value(t3, formatter).endObject());
    final DateTime t4 = new DateTime(randomDateTimeZone());
    expected = "{'t4':'" + formatter.print(t4) + "'}";
    assertResult(expected, () -> builder().startObject().field("t4", t4, formatter).endObject());
    assertResult(expected, () -> builder().startObject().field("t4").value(t4, formatter).endObject());
    // 1970-01-01T00:00:00Z - 2033-05-18T05:33:20.000+02:00
    long date = Math.abs(randomLong() % (2 * (long) 10e11));
    final DateTime t5 = new DateTime(date, randomDateTimeZone());
    expected = "{'t5':'" + XContentBuilder.DEFAULT_DATE_PRINTER.print(t5) + "'}";
    assertResult(expected, () -> builder().startObject().field("t5", t5).endObject());
    assertResult(expected, () -> builder().startObject().field("t5").value(t5).endObject());
    expected = "{'t5':'" + formatter.print(t5) + "'}";
    assertResult(expected, () -> builder().startObject().field("t5", t5, formatter).endObject());
    assertResult(expected, () -> builder().startObject().field("t5").value(t5, formatter).endObject());
    // 2016-01-01T00:00:00.000Z
    Instant i1 = new Instant(1451606400000L);
    expected = "{'i1':'2016-01-01T00:00:00.000Z'}";
    assertResult(expected, () -> builder().startObject().field("i1", i1).endObject());
    assertResult(expected, () -> builder().startObject().field("i1").value(i1).endObject());
    // 2016-12-25T07:59:42.213Z
    Instant i2 = new Instant(1482652782213L);
    expected = "{'i2':'" + formatter.print(i2) + "'}";
    assertResult(expected, () -> builder().startObject().field("i2", i2, formatter).endObject());
    assertResult(expected, () -> builder().startObject().field("i2").value(i2, formatter).endObject());
    expectNonNullFormatterException(() -> builder().startObject().field("t3", t3, null).endObject());
    expectNonNullFormatterException(() -> builder().startObject().field("t3").value(t3, null).endObject());
}
Also used : ReadableInstant(org.joda.time.ReadableInstant) Instant(org.joda.time.Instant) Matchers.containsString(org.hamcrest.Matchers.containsString) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTime(org.joda.time.DateTime)

Example 2 with ReadableInstant

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

the class ReadableInstantConverter method getChronology.

//-----------------------------------------------------------------------
/**
     * Gets the chronology, which is taken from the ReadableInstant.
     * If the chronology on the instant is null, the ISOChronology in the
     * specified time zone is used.
     * If the chronology on the instant is not in the specified zone, it is
     * adapted.
     * 
     * @param object  the ReadableInstant to convert, must not be null
     * @param zone  the specified zone to use, null means default zone
     * @return the chronology, never null
     */
public Chronology getChronology(Object object, DateTimeZone zone) {
    Chronology chrono = ((ReadableInstant) object).getChronology();
    if (chrono == null) {
        return ISOChronology.getInstance(zone);
    }
    DateTimeZone chronoZone = chrono.getZone();
    if (chronoZone != zone) {
        chrono = chrono.withZone(zone);
        if (chrono == null) {
            return ISOChronology.getInstance(zone);
        }
    }
    return chrono;
}
Also used : ReadableInstant(org.joda.time.ReadableInstant) Chronology(org.joda.time.Chronology) ISOChronology(org.joda.time.chrono.ISOChronology) DateTimeZone(org.joda.time.DateTimeZone)

Example 3 with ReadableInstant

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

the class GJChronology method getInstance.

/**
     * Factory method returns instances of the GJ cutover chronology. Any
     * cutover date may be specified.
     *
     * @param zone  the time zone to use, null is default
     * @param gregorianCutover  the cutover to use, null means default
     * @param minDaysInFirstWeek  minimum number of days in first week of the year; default is 4
     */
public static GJChronology getInstance(DateTimeZone zone, ReadableInstant gregorianCutover, int minDaysInFirstWeek) {
    zone = DateTimeUtils.getZone(zone);
    Instant cutoverInstant;
    if (gregorianCutover == null) {
        cutoverInstant = DEFAULT_CUTOVER;
    } else {
        cutoverInstant = gregorianCutover.toInstant();
        LocalDate cutoverDate = new LocalDate(cutoverInstant.getMillis(), GregorianChronology.getInstance(zone));
        if (cutoverDate.getYear() <= 0) {
            throw new IllegalArgumentException("Cutover too early. Must be on or after 0001-01-01.");
        }
    }
    GJCacheKey cacheKey = new GJCacheKey(zone, cutoverInstant, minDaysInFirstWeek);
    GJChronology chrono = cCache.get(cacheKey);
    if (chrono == null) {
        if (zone == DateTimeZone.UTC) {
            chrono = new GJChronology(JulianChronology.getInstance(zone, minDaysInFirstWeek), GregorianChronology.getInstance(zone, minDaysInFirstWeek), cutoverInstant);
        } else {
            chrono = getInstance(DateTimeZone.UTC, cutoverInstant, minDaysInFirstWeek);
            chrono = new GJChronology(ZonedChronology.getInstance(chrono, zone), chrono.iJulianChronology, chrono.iGregorianChronology, chrono.iCutoverInstant);
        }
        GJChronology oldChrono = cCache.putIfAbsent(cacheKey, chrono);
        if (oldChrono != null) {
            chrono = oldChrono;
        }
    }
    return chrono;
}
Also used : ReadableInstant(org.joda.time.ReadableInstant) Instant(org.joda.time.Instant) LocalDate(org.joda.time.LocalDate)

Example 4 with ReadableInstant

use of org.joda.time.ReadableInstant in project beam by apache.

the class LateDataUtilsTest method garbageCollectionTimeAfterEndOfGlobalWindow.

@Test
public void garbageCollectionTimeAfterEndOfGlobalWindow() {
    FixedWindows windowFn = FixedWindows.of(Duration.standardMinutes(5));
    WindowingStrategy<?, ?> strategy = WindowingStrategy.globalDefault().withWindowFn(windowFn);
    IntervalWindow window = windowFn.assignWindow(new Instant(BoundedWindow.TIMESTAMP_MAX_VALUE));
    assertThat(window.maxTimestamp(), Matchers.<ReadableInstant>greaterThan(GlobalWindow.INSTANCE.maxTimestamp()));
    assertThat(LateDataUtils.garbageCollectionTime(window, strategy), equalTo(GlobalWindow.INSTANCE.maxTimestamp()));
}
Also used : FixedWindows(org.apache.beam.sdk.transforms.windowing.FixedWindows) ReadableInstant(org.joda.time.ReadableInstant) Instant(org.joda.time.Instant) IntervalWindow(org.apache.beam.sdk.transforms.windowing.IntervalWindow) Test(org.junit.Test)

Example 5 with ReadableInstant

use of org.joda.time.ReadableInstant in project elasticsearch by elastic.

the class StreamOutput method writeGenericValue.

/**
     * Notice: when serialization a map, the stream out map with the stream in map maybe have the
     * different key-value orders, they will maybe have different stream order.
     * If want to keep stream out map and stream in map have the same stream order when stream,
     * can use {@code writeMapWithConsistentOrder}
     */
public void writeGenericValue(@Nullable Object value) throws IOException {
    if (value == null) {
        writeByte((byte) -1);
        return;
    }
    final Class type;
    if (value instanceof List) {
        type = List.class;
    } else if (value instanceof Object[]) {
        type = Object[].class;
    } else if (value instanceof Map) {
        type = Map.class;
    } else if (value instanceof ReadableInstant) {
        type = ReadableInstant.class;
    } else if (value instanceof BytesReference) {
        type = BytesReference.class;
    } else {
        type = value.getClass();
    }
    final Writer writer = WRITERS.get(type);
    if (writer != null) {
        writer.write(this, value);
    } else {
        throw new IOException("can not write type [" + type + "]");
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) ReadableInstant(org.joda.time.ReadableInstant) List(java.util.List) IOException(java.io.IOException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Writer(org.elasticsearch.common.io.stream.Writeable.Writer)

Aggregations

ReadableInstant (org.joda.time.ReadableInstant)7 Instant (org.joda.time.Instant)5 FixedWindows (org.apache.beam.sdk.transforms.windowing.FixedWindows)2 IntervalWindow (org.apache.beam.sdk.transforms.windowing.IntervalWindow)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Nullable (javax.annotation.Nullable)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1 Writer (org.elasticsearch.common.io.stream.Writeable.Writer)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Chronology (org.joda.time.Chronology)1 DateTime (org.joda.time.DateTime)1 DateTimeZone (org.joda.time.DateTimeZone)1