@Override Object convertField(JsonElement value) { List<String> patterns = Arrays.asList(this.inputPatterns.split(",")); int patternFailCount = 0; Object formattedDate = null; for (String pattern : patterns) { DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern).withZone(this.timeZone); try { formattedDate = dtf.parseDateTime(value.getAsString()).withZone(DateTimeZone.forID("UTC")).getMillis(); if (Boolean.valueOf(this.state.getProp(ConfigurationKeys.CONVERTER_IS_EPOCH_TIME_IN_SECONDS))) { formattedDate = (Long) formattedDate / 1000; } break; } catch (Exception e) { patternFailCount++; } } if (patternFailCount == patterns.size()) { throw new RuntimeException("Failed to parse the date"); } return formattedDate; }
protected Calendar parseDate(String date) throws Exception { Calendar dateCalendar = null; try { dateCalendar = ISODateTimeFormat.dateTimeParser().withZone(DateTimeZone.forTimeZone( clockReader.getCurrentTimeZone())).parseDateTime(date).toCalendar(null); } catch (IllegalArgumentException e) { // try to parse a java.util.date to string back to a java.util.date dateCalendar = new GregorianCalendar(); DateFormat DATE_FORMAT = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH); dateCalendar.setTime(DATE_FORMAT.parse(date)); } return dateCalendar; }
private DateTime parseDateTime(final String scheduleDate, final String scheduleTime) { // scheduleTime: 12,00,pm,PDT final String[] parts = scheduleTime.split(",", -1); int hour = Integer.parseInt(parts[0]); final int minutes = Integer.parseInt(parts[1]); final boolean isPm = parts[2].equalsIgnoreCase("pm"); final DateTimeZone timezone = parts[3].equals("UTC") ? DateTimeZone.UTC : DateTimeZone.getDefault(); // scheduleDate: 02/10/2013 DateTime day = null; if (scheduleDate == null || scheduleDate.trim().length() == 0) { day = new LocalDateTime().toDateTime(); } else { day = DateTimeFormat.forPattern("MM/dd/yyyy") .withZone(timezone).parseDateTime(scheduleDate); } hour %= 12; if (isPm) { hour += 12; } final DateTime firstSchedTime = day.withHourOfDay(hour).withMinuteOfHour(minutes).withSecondOfMinute(0); return firstSchedTime; }
private void verifyJvmTimeZone() { if (!allowCorruptWritesForTesting && !timeZone.equals(DateTimeZone.getDefault())) { throw new PrestoException(HIVE_TIMEZONE_MISMATCH, format( "To write Hive data, your JVM timezone must match the Hive storage timezone. Add -Duser.timezone=%s to your JVM arguments.", timeZone.getID())); } }
@SuppressForbidden(reason = "DateTimeZone#forID") public static DateTimeZone inferTzFromString(String tzId) { try { return DateTimeZone.forID(tzId); } catch (IllegalArgumentException e) { // also support Java timezone strings return DateTimeZone.forTimeZone(TimeZone.getTimeZone(tzId)); } }
static DateTimeZone nonDefaultTimeZone() { String defaultId = DateTimeZone.getDefault().getID(); for (String id : DateTimeZone.getAvailableIDs()) { if (!id.equals(defaultId)) { DateTimeZone zone = DateTimeZone.forID(id); if (zone.getStandardOffset(0) != 0) { return zone; } } } throw new IllegalStateException("no non-default timezone"); } }
@Test public void testCastToTimestampWithTimeZone() { assertFunction("cast(TIMESTAMP '2001-1-22 03:04:05.321' as timestamp with time zone)", TIMESTAMP_WITH_TIME_ZONE, new SqlTimestampWithTimeZone(new DateTime(2001, 1, 22, 3, 4, 5, 321, DATE_TIME_ZONE).getMillis(), DATE_TIME_ZONE.toTimeZone())); functionAssertions.assertFunctionString("cast(TIMESTAMP '2001-1-22 03:04:05.321' as timestamp with time zone)", TIMESTAMP_WITH_TIME_ZONE, "2001-01-22 03:04:05.321 " + DATE_TIME_ZONE.getID()); }
@Test(groups = "hive") public abstract class AbstractTestHiveFileFormats private static final long DATE_MILLIS_UTC = new DateTime(2011, 5, 6, 0, 0, UTC).getMillis(); private static final long DATE_DAYS = TimeUnit.MILLISECONDS.toDays(DATE_MILLIS_UTC); private static final String DATE_STRING = DateTimeFormat.forPattern("yyyy-MM-dd").withZoneUTC().print(DATE_MILLIS_UTC); private static final Date SQL_DATE = new Date(UTC.getMillisKeepLocal(DateTimeZone.getDefault(), DATE_MILLIS_UTC)); private static final long TIMESTAMP = new DateTime(2011, 5, 6, 7, 8, 9, 123).getMillis(); private static final String TIMESTAMP_STRING = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS").print(TIMESTAMP);
@Test public void testFromUnixTimeWithTimeZone() { String zoneId = "Asia/Shanghai"; DateTime expected = new DateTime(1970, 1, 1, 10, 0, 0, DateTimeZone.forID(zoneId)); assertFunction(format("from_unixtime(7200, '%s')", zoneId), TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(expected)); zoneId = "Asia/Tokyo"; expected = new DateTime(1970, 1, 1, 11, 0, 0, DateTimeZone.forID(zoneId)); assertFunction(format("from_unixtime(7200, '%s')", zoneId), TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(expected)); zoneId = "Europe/Moscow"; expected = new DateTime(1970, 1, 1, 5, 0, 0, DateTimeZone.forID(zoneId)); assertFunction(format("from_unixtime(7200, '%s')", zoneId), TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(expected)); zoneId = "America/New_York"; expected = new DateTime(1969, 12, 31, 21, 0, 0, DateTimeZone.forID(zoneId)); assertFunction(format("from_unixtime(7200, '%s')", zoneId), TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(expected)); zoneId = "America/Chicago"; expected = new DateTime(1969, 12, 31, 20, 0, 0, DateTimeZone.forID(zoneId)); assertFunction(format("from_unixtime(7200, '%s')", zoneId), TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(expected)); zoneId = "America/Los_Angeles"; expected = new DateTime(1969, 12, 31, 18, 0, 0, DateTimeZone.forID(zoneId)); assertFunction(format("from_unixtime(7200, '%s')", zoneId), TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(expected)); }
@Test public void testConverter() throws Exception { .withZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone("PST"))); new DateTime(record.get("LastModifiedDate")).toString(format)); Assert.assertEquals(jsonRecord.get("date_type").getAsString(), new DateTime(record.get("date_type")).toString(format)); format = DateTimeFormat.forPattern("HH:mm:ss").withZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone("PST"))); Assert.assertEquals(jsonRecord.get("time_type").getAsString(), new DateTime(record.get("time_type")).toString(format));
@Test public void testParseDatetime() { assertFunction("parse_datetime('1960/01/22 03:04', 'YYYY/MM/DD HH:mm')", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(new DateTime(1960, 1, 22, 3, 4, 0, 0, DATE_TIME_ZONE))); assertFunction("parse_datetime('1960/01/22 03:04 Asia/Oral', 'YYYY/MM/DD HH:mm ZZZZZ')", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(new DateTime(1960, 1, 22, 3, 4, 0, 0, DateTimeZone.forID("Asia/Oral")))); assertFunction("parse_datetime('1960/01/22 03:04 +0500', 'YYYY/MM/DD HH:mm Z')", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(new DateTime(1960, 1, 22, 3, 4, 0, 0, DateTimeZone.forOffsetHours(5)))); }
@Override @Nullable public Object convert(String value) { if (value == null || value.isEmpty()) { return null; } final Parser parser = new Parser(timeZone.toTimeZone()); final List<DateGroup> r = parser.parse(value); if (r.isEmpty() || r.get(0).getDates().isEmpty()) { return null; } return new DateTime(r.get(0).getDates().get(0), timeZone); } }
@Test public void createDateTimeFormatterWithTimeZone() { factory.setPattern("yyyyMMddHHmmss Z"); factory.setTimeZone(TEST_TIMEZONE); DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(TEST_TIMEZONE); DateTime dateTime = new DateTime(2009, 10, 21, 12, 10, 00, 00, dateTimeZone); String offset = (TEST_TIMEZONE.equals(NEW_YORK) ? "-0400" : "+0200"); assertThat(factory.createDateTimeFormatter().print(dateTime), is("20091021121000 " + offset)); }
this.wuState = wuState; this.googleAnalyticsFormatter = DateTimeFormat.forPattern(DATE_FORMAT) .withZone(DateTimeZone.forID(wuState.getProp(SOURCE_TIMEZONE, DEFAULT_SOURCE_TIMEZONE))); this.watermarkFormatter = DateTimeFormat.forPattern(WATERMARK_INPUTFORMAT) .withZone(DateTimeZone.forID(wuState.getProp(SOURCE_TIMEZONE, DEFAULT_SOURCE_TIMEZONE))); DateTime nextWatermarkDateTime = googleAnalyticsFormatter.parseDateTime(createdReport.getEndDate()).plusDays(1); nextWatermark = Long.parseLong(watermarkFormatter.print(nextWatermarkDateTime));
/** * Converts dateUnit to Joda-Time DateTime with a specific chronology. * * @param chronology Chronology to use * @return Populated DateTime object */ public DateTime toJodaDateTime( Chronology chronology ) { try { return new DateTime( year, month, day, hour, minute, second, millis, chronology.withZone( DateTimeZone.forTimeZone( timeZone ) ) ); } catch ( IllegalInstantException ex ) { LocalDateTime localDateTime = new LocalDateTime( year, month, day, hour, minute, second, millis, chronology.withZone( DateTimeZone.forTimeZone( timeZone ) ) ); return localDateTime.toLocalDate().toDateTimeAtStartOfDay(); } }
public Object fromString(final String str) { for (int i = 0; i < formattersUTC.length ; ++i) { final DateTimeFormatter formatter = formattersUTC[i]; try { final DateTime dt = formatter.parseDateTime(str); final Calendar calendar = dt.toGregorianCalendar(); calendar.setTimeZone(TimeZone.getDefault()); return calendar; } catch (final IllegalArgumentException e) { // try with next formatter } } final DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(TimeZone.getDefault()); for (int i = 0; i < formattersNoUTC.length ; ++i) { final DateTimeFormatter element = formattersNoUTC[i]; try { final DateTimeFormatter formatter = element.withZone(dateTimeZone); final DateTime dt = formatter.parseDateTime(str); final Calendar calendar = dt.toGregorianCalendar(); calendar.setTimeZone(TimeZone.getDefault()); return calendar; } catch (final IllegalArgumentException e) { // try with next formatter } } final ConversionException exception = new ConversionException("Cannot parse date"); exception.add("date", str); throw exception; }
private static SqlTimestampWithTimeZone toTimestampWithTimeZone(DateTime dateTime) { return new SqlTimestampWithTimeZone(dateTime.getMillis(), dateTime.getZone().toTimeZone()); } }
InternalParser parser = requireParser(); if (instant == null) { throw new IllegalArgumentException("Instant must not be null"); int defaultYear = DateTimeUtils.getChronology(chrono).year().get(instantMillis); long instantLocal = instantMillis + chrono.getZone().getOffset(instantMillis); chrono = selectChronology(chrono); if (iOffsetParsed && bucket.getOffsetInteger() != null) { int parsedOffset = bucket.getOffsetInteger(); DateTimeZone parsedZone = DateTimeZone.forOffsetMillis(parsedOffset); chrono = chrono.withZone(parsedZone); } else if (bucket.getZone() != null) { chrono = chrono.withZone(bucket.getZone());