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());
}
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;
}
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;
}
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()));
}
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 + "]");
}
}
Aggregations