@Test public void commandLineDatetime() throws Exception { Map<String, LocalDateTime> stringToDatetime = new HashMap<>(); stringToDatetime.put("2020-06-27 11:58:53,084", LocalDateTime.of(2020, 6, 27, 11, 58, 53, 84 * MILLISEC_TO_NANOSEC)); stringToDatetime.put("2020-06-27 11:58:53", LocalDateTime.of(2020, 6, 27, 11, 58, 53)); stringToDatetime.put("2020-06-27 11:58", LocalDateTime.of(2020, 6, 27, 11, 58)); stringToDatetime.put("20/06/27 11:58:53", LocalDateTime.of(2020, 6, 27, 11, 58, 53)); stringToDatetime.put("20/06/27 11:58", LocalDateTime.of(2020, 6, 27, 11, 58)); stringToDatetime.put("2020-06-27T11:58:53.084+0800", LocalDateTime.of(2020, 6, 27, 11, 58, 53, 84 * MILLISEC_TO_NANOSEC)); stringToDatetime.put("2020-06-27T11:58:53", LocalDateTime.of(2020, 6, 27, 11, 58, 53)); stringToDatetime.put("2020-06-27T11:58", LocalDateTime.of(2020, 6, 27, 11, 58)); for (Map.Entry<String, LocalDateTime> entry : stringToDatetime.entrySet()) { String key = entry.getKey(); LocalDateTime expectedDatetime = entry.getValue(); LocalDateTime parsedDatetime = CollectLogCommand.parseDateTime(key); assertThat(String.format("Expected datetime is %s but parsed %s from string %s%n", expectedDatetime, parsedDatetime, key), parsedDatetime, new DatetimeMatcher().setDatetime(expectedDatetime)); } }
@Nullable public static LocalDateTime parseDateTime(String s) { for (Map.Entry<DateTimeFormatter, Integer> entry : FORMATTERS.entrySet()) { DateTimeFormatter fmt = entry.getKey(); int len = entry.getValue(); try { if (s.length() < len) { continue; } String datePart = s.substring(0, len); LocalDateTime datetime = LocalDateTime.parse(datePart, fmt); return datetime; } catch (DateTimeParseException e) { continue; } } LOG.warn("Unknown date format in {}", s.length() > 50 ? s.substring(0, 50) : s); return null; }
CollectLogCommand extends AbstractCollectInfoCommand { @Nullable public static LocalDateTime parseDateTime(String s) { for (Map.Entry<DateTimeFormatter, Integer> entry : FORMATTERS.entrySet()) { DateTimeFormatter fmt = entry.getKey(); int len = entry.getValue(); try { if (s.length() < len) { continue; } String datePart = s.substring(0, len); LocalDateTime datetime = LocalDateTime.parse(datePart, fmt); return datetime; } catch (DateTimeParseException e) { continue; } } LOG.warn("Unknown date format in {}", s.length() > 50 ? s.substring(0, 50) : s); return null; } }
CollectLogCommand extends AbstractCollectInfoCommand { @Nullable public static LocalDateTime parseDateTime(String s) { for (Map.Entry<DateTimeFormatter, Integer> entry : FORMATTERS.entrySet()) { DateTimeFormatter fmt = entry.getKey(); int len = entry.getValue(); try { if (s.length() < len) { continue; } String datePart = s.substring(0, len); LocalDateTime datetime = LocalDateTime.parse(datePart, fmt); return datetime; } catch (DateTimeParseException e) { continue; } } LOG.warn("Unknown date format in {}", s.length() > 50 ? s.substring(0, 50) : s); return null; } CollectLogCommand(FileSystemContext fsContext); }
CollectLogCommand extends AbstractCollectInfoCommand { @Nullable public static LocalDateTime parseDateTime(String s) { for (Map.Entry<DateTimeFormatter, Integer> entry : FORMATTERS.entrySet()) { DateTimeFormatter fmt = entry.getKey(); int len = entry.getValue(); try { if (s.length() < len) { continue; } String datePart = s.substring(0, len); LocalDateTime datetime = LocalDateTime.parse(datePart, fmt); return datetime; } catch (DateTimeParseException e) { continue; } } LOG.warn("Unknown date format in {}", s.length() > 50 ? s.substring(0, 50) : s); return null; } CollectLogCommand(FileSystemContext fsContext); @Override String getCommandName(); @Override boolean hasSubCommand(); @Override int run(CommandLine cl); static LocalDateTime inferFileStartTime(File f); @Override String getUsage(); @Override String getDescription(); @Nullable static LocalDateTime parseDateTime(String s); }
CollectLogCommand extends AbstractCollectInfoCommand { @Nullable public static LocalDateTime parseDateTime(String s) { for (Map.Entry<DateTimeFormatter, Integer> entry : FORMATTERS.entrySet()) { DateTimeFormatter fmt = entry.getKey(); int len = entry.getValue(); try { if (s.length() < len) { continue; } String datePart = s.substring(0, len); LocalDateTime datetime = LocalDateTime.parse(datePart, fmt); return datetime; } catch (DateTimeParseException e) { continue; } } LOG.warn("Unknown date format in {}", s.length() > 50 ? s.substring(0, 50) : s); return null; } CollectLogCommand(FileSystemContext fsContext); @Override String getCommandName(); @Override boolean hasSubCommand(); @Override int run(CommandLine cl); static LocalDateTime inferFileStartTime(File f); @Override String getUsage(); @Override String getDescription(); @Nullable static LocalDateTime parseDateTime(String s); static final String COMMAND_NAME; static final Set<String> FILE_NAMES_PREFIXES; static final String INCLUDE_OPTION_NAME; static final String EXCLUDE_OPTION_NAME; static final String ADDITIONAL_OPTION_NAME; static final Options OPTIONS; }