Skip to content

Commit

Permalink
Allow date time input to be yyyy-mm-dd
Browse files Browse the repository at this point in the history
  • Loading branch information
germainelee02 committed Jun 17, 2023
1 parent 3878389 commit 070fd0a
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/main/java/reposense/util/TimeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ public class TimeUtil {
private static final String DATE_FORMAT_REGEX =
"^((0?[1-9]|[12][0-9]|3[01])\\/(0?[1-9]|1[012])\\/(19|2[0-9])[0-9]{2})";


private static final String EXPERIMENTAL_DATE_FORMAT_REGEX =
"^((19|2[0-9])[0-9]{2}\\-(0?[1-9]|1[012])\\-(0?[1-9]|[12][0-9]|3[01]))";

// "uuuu" is used for year since "yyyy" does not work with ResolverStyle.STRICT
private static final DateTimeFormatter CLI_ARGS_DATE_FORMAT = DateTimeFormatter.ofPattern("d/M/uuuu HH:mm:ss");
private static final DateTimeFormatter CLI_ARGS_DATE_FORMAT =
DateTimeFormatter.ofPattern("d/M/uuuu HH:mm:ss");
private static final DateTimeFormatter CLI_ARGS_DATE_FORMAT_EXPERIMENTAL = DateTimeFormatter
.ofPattern("uuuu-M-d HH:mm:ss");
private static final String MESSAGE_SINCE_DATE_LATER_THAN_UNTIL_DATE =
"\"Since Date\" cannot be later than \"Until Date\".";
private static final String MESSAGE_SINCE_DATE_LATER_THAN_TODAY_DATE =
Expand All @@ -39,6 +46,8 @@ public class TimeUtil {

private static final Logger logger = LogsManager.getLogger(TimeUtil.class);

private static boolean isDateExperimental;

/**
* Sets the {@code startTime} to be the current time.
*/
Expand Down Expand Up @@ -193,26 +202,42 @@ public static void verifySinceDateIsValid(LocalDateTime sinceDate, LocalDateTime
}

/**
* Extracts the first substring of {@code date} string that matches the {@code DATE_FORMAT_REGEX}.
* Extracts the first substring of {@code date} string that matches the
* {@code DATE_FORMAT_REGEX}.
*/
public static String extractDate(String date) {
Matcher matcher = Pattern.compile(DATE_FORMAT_REGEX).matcher(date);
String extractedDate = date;
if (matcher.find()) {
extractedDate = matcher.group(1);
isDateExperimental = false;
}
// second try if date format doesn't match
Matcher experimentalMatcher = Pattern.compile(EXPERIMENTAL_DATE_FORMAT_REGEX).matcher(date);
if (experimentalMatcher.find()) {
extractedDate = experimentalMatcher.group(1);
isDateExperimental = true;
}

return extractedDate;
}

/**
* Parses the given {@code date} string as a {@link LocalDateTime} based on the {@code CLI_ARGS_DATE_FORMAT}.
* Parses the given {@code date} string as a {@link LocalDateTime} based on the
* {@code CLI_ARGS_DATE_FORMAT}.
* Uses {@link ResolverStyle#STRICT} to avoid unexpected dates like 31/02/2020.
*
* @throws java.text.ParseException if date cannot be parsed by the required format.
* @throws java.text.ParseException if date cannot be parsed by the required
* format.
*/
public static LocalDateTime parseDate(String date) throws java.text.ParseException {
try {
return LocalDateTime.parse(date, CLI_ARGS_DATE_FORMAT.withResolverStyle(ResolverStyle.STRICT));
if (isDateExperimental) {
return LocalDateTime.parse(date,
CLI_ARGS_DATE_FORMAT_EXPERIMENTAL.withResolverStyle(ResolverStyle.STRICT));
} else {
return LocalDateTime.parse(date, CLI_ARGS_DATE_FORMAT.withResolverStyle(ResolverStyle.STRICT));
}
} catch (DateTimeParseException e) {
throw new java.text.ParseException(String.format(
"Exception message: %s\n", e.getMessage()), e.getErrorIndex());
Expand Down

0 comments on commit 070fd0a

Please sign in to comment.