@Test
  public void testIsoDateFormatDateOptionalTimeUTC() {
    DateTimeFormatter formatter =
        ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC);
    long millis = formatter.parseMillis("1970-01-01T00:00:00Z");
    assertThat(millis, equalTo(0l));
    millis = formatter.parseMillis("1970-01-01T00:00:00.001Z");
    assertThat(millis, equalTo(1l));
    millis = formatter.parseMillis("1970-01-01T00:00:00.1Z");
    assertThat(millis, equalTo(100l));
    millis = formatter.parseMillis("1970-01-01T00:00:00.1");
    assertThat(millis, equalTo(100l));
    millis = formatter.parseMillis("1970-01-01T00:00:00");
    assertThat(millis, equalTo(0l));
    millis = formatter.parseMillis("1970-01-01");
    assertThat(millis, equalTo(0l));

    millis = formatter.parseMillis("1970");
    assertThat(millis, equalTo(0l));

    try {
      formatter.parseMillis("1970 kuku");
      assert false : "formatting should fail";
    } catch (IllegalArgumentException e) {
      // all is well
    }

    // test offset in format
    millis = formatter.parseMillis("1970-01-01T00:00:00-02:00");
    assertThat(millis, equalTo(TimeValue.timeValueHours(2).millis()));
  }
  @Test
  public void testIsoVsCustom() {
    DateTimeFormatter formatter =
        ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC);
    long millis = formatter.parseMillis("1970-01-01T00:00:00");
    assertThat(millis, equalTo(0l));

    formatter = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss").withZone(DateTimeZone.UTC);
    millis = formatter.parseMillis("1970/01/01 00:00:00");
    assertThat(millis, equalTo(0l));

    FormatDateTimeFormatter formatter2 = Joda.forPattern("yyyy/MM/dd HH:mm:ss");
    millis = formatter2.parser().parseMillis("1970/01/01 00:00:00");
    assertThat(millis, equalTo(0l));
  }
Ejemplo n.º 3
0
  private long parseDateTime(String value, DateTimeZone timeZone) {

    // first check for timestamp
    if (value.length() > 4 && StringUtils.isNumeric(value)) {
      try {
        long time = Long.parseLong(value);
        return timeUnit.toMillis(time);
      } catch (NumberFormatException e) {
        throw new ElasticsearchParseException(
            "failed to parse date field [" + value + "] as timestamp", e);
      }
    }

    DateTimeFormatter parser = dateTimeFormatter.parser();
    if (timeZone != null) {
      parser = parser.withZone(timeZone);
    }
    try {
      return parser.parseMillis(value);
    } catch (IllegalArgumentException e) {

      throw new ElasticsearchParseException(
          "failed to parse date field ["
              + value
              + "] with format ["
              + dateTimeFormatter.format()
              + "]",
          e);
    }
  }
  @Test
  public void testIsoDateFormatDateTimeNoMillisUTC() {
    DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis().withZone(DateTimeZone.UTC);
    long millis = formatter.parseMillis("1970-01-01T00:00:00Z");

    assertThat(millis, equalTo(0l));
  }
 @Test
 public void testWriteAndParse() {
   DateTimeFormatter dateTimeWriter = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC);
   DateTimeFormatter formatter =
       ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC);
   Date date = new Date();
   assertThat(
       formatter.parseMillis(dateTimeWriter.print(date.getTime())), equalTo(date.getTime()));
 }
    @Override
    public long getLong() {
      if (isNull()) {
        return 0L;
      }

      if (value.canConvertToLong()) {
        return value.asLong();
      }

      String textValue = value.isValueNode() ? value.asText() : value.toString();
      return FORMATTER.parseMillis(textValue);
    }
  @Test
  public void testMultiParsers() {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    DateTimeParser[] parsers = new DateTimeParser[3];
    parsers[0] = DateTimeFormat.forPattern("MM/dd/yyyy").withZone(DateTimeZone.UTC).getParser();
    parsers[1] = DateTimeFormat.forPattern("MM-dd-yyyy").withZone(DateTimeZone.UTC).getParser();
    parsers[2] =
        DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").withZone(DateTimeZone.UTC).getParser();
    builder.append(
        DateTimeFormat.forPattern("MM/dd/yyyy").withZone(DateTimeZone.UTC).getPrinter(), parsers);

    DateTimeFormatter formatter = builder.toFormatter();

    formatter.parseMillis("2009-11-15 14:12:12");
  }
Ejemplo n.º 8
0
  private ObjectNode fileAsJson(String path, org.tmatesoft.svn.core.io.SVNRepository repository)
      throws SVNException, IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    SVNProperties prop = new SVNProperties();
    repository.getFile(path, -1l, prop, baos);
    SVNDirEntry entry = repository.info(path, -1l);
    long size = entry.getSize();
    boolean isBinary;
    String mimeType;
    String data = null;

    if (size > MAX_FILE_SIZE_CAN_BE_VIEWED) {
      isBinary = true;
      mimeType = "application/octet-stream";
    } else {
      byte[] bytes = baos.toByteArray();
      isBinary = RawText.isBinary(bytes);
      if (!isBinary) {
        data = new String(bytes, FileUtil.detectCharset(bytes));
      }
      mimeType = new Tika().detect(bytes, path);
    }

    String author = prop.getStringValue(SVNProperty.LAST_AUTHOR);
    User user = User.findByLoginId(author);

    String commitDate = prop.getStringValue(SVNProperty.COMMITTED_DATE);
    DateTimeFormatter dateFormatter = ISODateTimeFormat.dateTime();
    Long commitTime = dateFormatter.parseMillis(commitDate);

    ObjectNode result = Json.newObject();
    result.put("type", "file");
    result.put("revisionNo", prop.getStringValue(SVNProperty.COMMITTED_REVISION));
    result.put("author", author);
    result.put("avatar", getAvatar(user));
    result.put("userName", user.name);
    result.put("userLoginId", user.loginId);
    result.put("createdDate", commitTime);
    result.put("commitMessage", entry.getCommitMessage());
    result.put("commiter", author);
    result.put("size", size);
    result.put("isBinary", isBinary);
    result.put("mimeType", mimeType);
    result.put("data", data);

    return result;
  }
Ejemplo n.º 9
0
  @ScalarFunction
  @SqlType(TimestampType.class)
  public static long dateParse(
      ConnectorSession session,
      @SqlType(VarcharType.class) Slice dateTime,
      @SqlType(VarcharType.class) Slice formatString) {
    DateTimeFormatter formatter =
        DATETIME_FORMATTER_CACHE
            .get(formatString)
            .withChronology(getChronology(session.getTimeZoneKey()))
            .withLocale(session.getLocale());

    try {
      return formatter.parseMillis(dateTime.toString(Charsets.UTF_8));
    } catch (IllegalArgumentException e) {
      throw new PrestoException(StandardErrorCode.INVALID_FUNCTION_ARGUMENT.toErrorCode(), e);
    }
  }
Ejemplo n.º 10
0
    public Date convertToObject(String value, Locale locale) {
      if (Strings.isEmpty(value)) {
        return null;
      }

      // Convert String to Calendar or sql.Date/Time/Timestamp
      // First convert it to a milliseconds. We think it's GMT because no TZ is specified in the
      // String.
      long date;
      try {
        DateTimeFormatter dateFmt = getFormat().withZone(DateTimeZone.UTC);
        date = dateFmt.parseMillis(value);
      } catch (IllegalArgumentException e) {
        throw new ConversionException("Cannot convert '" + value + "' to a Date.")
            .setSourceValue(value)
            .setTargetType(type)
            .setConverter(this)
            .setLocale(locale);
      }

      if (Timestamp.class == type) {
        return new Timestamp(date);
      }

      if (java.sql.Date.class == type) {
        return new java.sql.Date(date);
      }

      if (Time.class == type) {
        return new Time(date);
      }

      if (Date.class == type) {
        return new Date(date);
      }

      if (Calendar.class.isAssignableFrom(type)) {
        Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
        cal.setTimeInMillis(date);
        return cal.getTime();
      }

      throw new RuntimeException("Don't know how to convert a String to " + type);
    }
Ejemplo n.º 11
0
 public static long parseHiveDate(String value) {
   long millis = HIVE_DATE_PARSER.parseMillis(value);
   return TimeUnit.MILLISECONDS.toDays(millis);
 }