src/main/java/org/embulk/input/zendesk/utils/ZendeskDateUtils.java in embulk-input-zendesk-0.3.4 vs src/main/java/org/embulk/input/zendesk/utils/ZendeskDateUtils.java in embulk-input-zendesk-0.3.5

- old
+ new

@@ -1,52 +1,63 @@ package org.embulk.input.zendesk.utils; -import com.google.common.base.Joiner; import org.embulk.spi.DataException; +import java.time.Instant; import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; -import java.util.Arrays; -import java.util.List; import java.util.Optional; public class ZendeskDateUtils { private ZendeskDateUtils() { } - private static final List<String> supportedFormats = Arrays.asList(ZendeskConstants.Misc.ISO_INSTANT, ZendeskConstants.Misc.RUBY_TIMESTAMP_FORMAT_INPUT, - ZendeskConstants.Misc.JAVA_TIMESTAMP_FORMAT, ZendeskConstants.Misc.ISO_TIMESTAMP_FORMAT, - ZendeskConstants.Misc.RUBY_TIMESTAMP_FORMAT_INPUT_NO_SPACE); - public static long isoToEpochSecond(final String time) { - Optional<String> pattern = supportedTimeFormat(time, supportedFormats); + final Optional<String> pattern = supportedTimeFormat(time); if (pattern.isPresent()) { final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern.get()).withZone(ZoneOffset.UTC); final OffsetDateTime offsetDateTime = LocalDateTime.parse(time, formatter).atOffset(ZoneOffset.UTC); return offsetDateTime.toInstant().getEpochSecond(); } - throw new DataException("Fail to parse value '" + time + "' follow formats " + "[ " + Joiner.on(",").join(supportedFormats) + "]"); + throw new DataException("Fail to parse value '" + time + "' follow formats " + ZendeskConstants.Misc.SUPPORT_DATE_TIME_FORMAT.toString()); } - private static Optional<String> supportedTimeFormat(final String value, final List<String> supportedFormats) + public static Optional<String> supportedTimeFormat(final String value) { - for (final String fmt : supportedFormats) { + for (final String fmt : ZendeskConstants.Misc.SUPPORT_DATE_TIME_FORMAT) { final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(fmt); try { formatter.parse(value); return Optional.of(fmt); } catch (final DateTimeParseException e) { // Do nothing } } return Optional.empty(); + } + + public static String convertToDateTimeFormat(String datetime, String dateTimeFormat) + { + return OffsetDateTime.ofInstant(Instant.ofEpochSecond(ZendeskDateUtils.isoToEpochSecond(datetime)), ZoneOffset.UTC) + .format(DateTimeFormatter.ofPattern(dateTimeFormat)); + } + + // start_time should be start from 0 + public static long getStartTime(final String time) + { + try { + return isoToEpochSecond(time); + } + catch (DataException ex) { + return 0; + } } }