/** * Calcite expects "TIMESTAMP" types to be an instant that has the expected local time fields if printed as UTC. * * @param dateTime joda timestamp * @param timeZone session time zone * * @return Calcite style millis */ public static long jodaToCalciteTimestamp(final DateTime dateTime, final DateTimeZone timeZone) { return dateTime.withZone(timeZone).withZoneRetainFields(DateTimeZone.UTC).getMillis(); }
DateTimeZone timeZoneLondon = DateTimeZone.forID( "Europe/London" ); DateTimeZone timeZoneKolkata = DateTimeZone.forID( "Asia/Kolkata" ); DateTimeZone timeZoneNewYork = DateTimeZone.forID( "America/New_York" ); DateTime nowLondon = DateTime.now( timeZoneLondon ); // Assign a time zone rather than rely on implicit default time zone. DateTime nowKolkata = nowLondon.withZone( timeZoneKolkata ); DateTime nowNewYork = nowLondon.withZone( timeZoneNewYork ); DateTime nowUtc = nowLondon.withZone( DateTimeZone.UTC ); // Built-in constant for UTC.
@Override public void serialize(DateTime dateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { final Date date = dateTime.withZone(DateTimeZone.UTC).toDate(); jsonGenerator.writeObject(date); } }
/** * Calcite expects "DATE" types to be number of days from the epoch to the UTC date matching the local time fields. * * @param dateTime joda timestamp * @param timeZone session time zone * * @return Calcite style date */ public static int jodaToCalciteDate(final DateTime dateTime, final DateTimeZone timeZone) { final DateTime date = dateTime.withZone(timeZone).dayOfMonth().roundFloorCopy(); return Days.daysBetween(DateTimes.EPOCH, date.withZoneRetainFields(DateTimeZone.UTC)).getDays(); }
String input = "2014-01-02T03:04:05"; DateTimeZone timeZone = DateTimeZone.forID( "Asia/Kolkata" ); DateTime dateTimeIndia = new DateTime( input, timeZone ); DateTime dateTimeUtcGmt = dateTimeIndia.withZone( DateTimeZone.UTC );
/** * Calcite expects DATE literals to be represented by DateStrings in the local time zone. * * @param dateTime joda timestamp * @param timeZone session time zone * * @return Calcite style Calendar, appropriate for literals */ public static DateString jodaToCalciteDateString(final DateTime dateTime, final DateTimeZone timeZone) { return new DateString(CALCITE_DATE_PRINTER.print(dateTime.withZone(timeZone))); }
/** * Calcite expects TIMESTAMP literals to be represented by TimestampStrings in the local time zone. * * @param dateTime joda timestamp * @param timeZone session time zone * * @return Calcite style Calendar, appropriate for literals */ public static TimestampString jodaToCalciteTimestampString(final DateTime dateTime, final DateTimeZone timeZone) { // The replaceAll is because Calcite doesn't like trailing zeroes in its fractional seconds part. String timestampString = TRAILING_ZEROS .matcher(CALCITE_TIMESTAMP_PRINTER.print(dateTime.withZone(timeZone))) .replaceAll(""); return new TimestampString(timestampString); }
/** * Calcite expects TIME literals to be represented by TimeStrings in the local time zone. * * @param dateTime joda timestamp * @param timeZone session time zone * * @return Calcite style Calendar, appropriate for literals */ public static TimeString jodaToCalciteTimeString(final DateTime dateTime, final DateTimeZone timeZone) { // The replaceAll is because Calcite doesn't like trailing zeroes in its fractional seconds part. String timeString = TRAILING_ZEROS .matcher(CALCITE_TIME_PRINTER.print(dateTime.withZone(timeZone))) .replaceAll(""); return new TimeString(timeString); }
public DateTime getTimestamp() { return getFieldAs(DateTime.class, FIELD_TIMESTAMP).withZone(UTC); }
private DateTime getNextRuntime(final long scheduleTime, final DateTimeZone timezone, final ReadablePeriod period) { final DateTime now = new DateTime(); DateTime date = new DateTime(scheduleTime).withZone(timezone); int count = 0; while (!now.isBefore(date)) { if (count > 100000) { throw new IllegalStateException( "100000 increments of period did not get to present time."); } if (period == null) { break; } else { date = date.plus(period); } count += 1; } return date; }
/** * @param scheduleTime represents the time when Schedule Servlet receives the Cron Schedule API * call. * @param timezone is always UTC (after 3.1.0) * @return the First Scheduled DateTime to run this flow. */ private DateTime getNextCronRuntime(final long scheduleTime, final DateTimeZone timezone, final CronExpression ce) { Date date = new DateTime(scheduleTime).withZone(timezone).toDate(); if (ce != null) { date = ce.getNextValidTimeAfter(date); } return new DateTime(date); }
/** * Convert timestamp in a string format to joda time * @param input timestamp * @param format timestamp format * @param timezone time zone of timestamp * @return joda time */ public static DateTime toDateTime(String input, String format, String timezone) { String tz = StringUtils.defaultString(timezone, ConfigurationKeys.DEFAULT_SOURCE_TIMEZONE); DateTimeZone dateTimeZone = getTimeZone(tz); DateTimeFormatter inputDtFormat = DateTimeFormat.forPattern(format).withZone(dateTimeZone); DateTime outputDateTime = inputDtFormat.parseDateTime(input).withZone(dateTimeZone); return outputDateTime; }
private long calculateNextCheckTime() { DateTime date = new DateTime(this.nextCheckTime).withZone(this.timezone); int count = 0; while (!date.isAfterNow()) { if (count > 100000) { throw new IllegalStateException( "100000 increments of period did not get to present time."); } if (this.period == null && this.cronExpression == null) { break; } else if (this.cronExecutionTime != null) { final Date nextDate = this.cronExecutionTime.getNextValidTimeAfter(date.toDate()); // Some Cron Expressions possibly do not have follow-up occurrences if (nextDate != null) { date = new DateTime(nextDate); } else { break; } } else { date = date.plus(this.period); } count += 1; } return date.getMillis(); }
@Override public DateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { switch (jsonParser.currentToken()) { case VALUE_EMBEDDED_OBJECT: final Object embeddedObject = jsonParser.getEmbeddedObject(); if (embeddedObject instanceof Date) { final Date date = (Date) embeddedObject; return new DateTime(date, DateTimeZone.UTC); } else { throw new IllegalStateException("Unsupported token: " + jsonParser.currentToken()); } case VALUE_STRING: final String text = jsonParser.getText(); return DateTime.parse(text, FORMATTER).withZone(DateTimeZone.UTC); default: throw new IllegalStateException("Unsupported token: " + jsonParser.currentToken()); } } }
macroTable, plannerConfig.withOverrides(queryContext), utcNow.withZone(timeZone), queryContext, authenticationResult
@Test(expected = MissingSchemaException.class) public void testSpecificByteArrayIncompatibleWithoutLogicalTypes() throws IOException { TestRecordWithLogicalTypes withLogicalTypes = new TestRecordWithLogicalTypes( true, 34, 35L, 3.14F, 3019.34, null, LocalDate.now(), LocalTime.now(), DateTime.now().withZone(DateTimeZone.UTC), new BigDecimal("123.45") ); ByteBuffer b = withLogicalTypes.toByteBuffer(); TestRecordWithoutLogicalTypes.fromByteBuffer(b); } }
@Test public void testSpecificToFromByteBufferWithLogicalTypes() throws IOException { TestRecordWithLogicalTypes record = new TestRecordWithLogicalTypes( true, 34, 35L, 3.14F, 3019.34, null, LocalDate.now(), LocalTime.now(), DateTime.now().withZone(DateTimeZone.UTC), new BigDecimal("123.45") ); ByteBuffer b = record.toByteBuffer(); TestRecordWithLogicalTypes copy = TestRecordWithLogicalTypes.fromByteBuffer(b); assertEquals(record, copy); }
@Test public void testRecordWithLogicalTypes() throws IOException { TestRecordWithLogicalTypes record = new TestRecordWithLogicalTypes( true, 34, 35L, 3.14F, 3019.34, null, LocalDate.now(), LocalTime.now(), DateTime.now().withZone(DateTimeZone.UTC), new BigDecimal(123.45f).setScale(2, RoundingMode.HALF_DOWN) ); File data = write(TestRecordWithLogicalTypes.getClassSchema(), record); List<TestRecordWithLogicalTypes> actual = read( TestRecordWithLogicalTypes.getClassSchema(), data); Assert.assertEquals("Should match written record", record, actual.get(0)); } @Test
@Test(expected = MissingSchemaException.class) public void testSpecificByteArrayIncompatibleWithLogicalTypes() throws IOException { TestRecordWithoutLogicalTypes withoutLogicalTypes = new TestRecordWithoutLogicalTypes( true, 34, 35L, 3.14F, 3019.34, null, new TimeConversions.DateConversion().toInt(LocalDate.now(), null, null), new TimeConversions.TimeConversion().toInt(LocalTime.now(), null, null), new TimeConversions.TimestampConversion().toLong( DateTime.now().withZone(DateTimeZone.UTC), null, null), new Conversions.DecimalConversion().toBytes( new BigDecimal("123.45"), null, LogicalTypes.decimal(9, 2)) ); ByteBuffer b = withoutLogicalTypes.toByteBuffer(); TestRecordWithLogicalTypes.fromByteBuffer(b); }
@Test public void testSpecificToFromByteBufferWithoutLogicalTypes() throws IOException { TestRecordWithoutLogicalTypes record = new TestRecordWithoutLogicalTypes( true, 34, 35L, 3.14F, 3019.34, null, new TimeConversions.DateConversion().toInt(LocalDate.now(), null, null), new TimeConversions.TimeConversion().toInt(LocalTime.now(), null, null), new TimeConversions.TimestampConversion().toLong( DateTime.now().withZone(DateTimeZone.UTC), null, null), new Conversions.DecimalConversion().toBytes( new BigDecimal("123.45"), null, LogicalTypes.decimal(9, 2)) ); ByteBuffer b = record.toByteBuffer(); TestRecordWithoutLogicalTypes copy = TestRecordWithoutLogicalTypes.fromByteBuffer(b); assertEquals(record, copy); }