From cb443658b5b4f1e880130ae59887d646c0297f74 Mon Sep 17 00:00:00 2001 From: arisnguyenit97 Date: Sat, 6 Jul 2024 23:10:34 +0700 Subject: [PATCH] :sparkles: feat: add unify functions for Time4j #4 --- .../groovy/org/unify4j/common/Time4j.java | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) diff --git a/plugin/src/main/groovy/org/unify4j/common/Time4j.java b/plugin/src/main/groovy/org/unify4j/common/Time4j.java index d406c6d..fcc1d45 100644 --- a/plugin/src/main/groovy/org/unify4j/common/Time4j.java +++ b/plugin/src/main/groovy/org/unify4j/common/Time4j.java @@ -1218,6 +1218,132 @@ public static String since(Date source, Date target) { } } + /** + * Provides the difference in milliseconds between the given dates. + * + * @param source The reference date to calculate the duration from. Typically, this would be the current date. + * @param target The date from which the elapsed time is calculated. + * @return The difference in milliseconds between the two dates. + */ + public static long sinceMilliseconds(Date source, Date target) { + if (source == null || target == null) { + return 0; + } + LocalDateTime now = transform(source); + LocalDateTime then = LocalDateTime.ofInstant(target.toInstant(), ZoneId.systemDefault()); + Duration duration = Duration.between(then, now); + return duration.toMillis(); + } + + /** + * Provides the difference in seconds between the given dates. + * + * @param source The reference date to calculate the duration from. Typically, this would be the current date. + * @param target The date from which the elapsed time is calculated. + * @return The difference in seconds between the two dates. + */ + public static long sinceSeconds(Date source, Date target) { + if (source == null || target == null) { + return 0; + } + LocalDateTime now = transform(source); + LocalDateTime then = LocalDateTime.ofInstant(target.toInstant(), ZoneId.systemDefault()); + Duration duration = Duration.between(then, now); + return duration.getSeconds(); + } + + /** + * Provides the difference in minutes between the given dates. + * + * @param source The reference date to calculate the duration from. Typically, this would be the current date. + * @param target The date from which the elapsed time is calculated. + * @return The difference in minutes between the two dates. + */ + public static long sinceMinutes(Date source, Date target) { + if (source == null || target == null) { + return 0; + } + LocalDateTime now = transform(source); + LocalDateTime then = LocalDateTime.ofInstant(target.toInstant(), ZoneId.systemDefault()); + Duration duration = Duration.between(then, now); + return duration.toMinutes(); + } + + /** + * Provides the difference in hours between the given dates. + * + * @param source The reference date to calculate the duration from. Typically, this would be the current date. + * @param target The date from which the elapsed time is calculated. + * @return The difference in hours between the two dates. + */ + public static long sinceHours(Date source, Date target) { + if (source == null || target == null) { + return 0; + } + LocalDateTime now = transform(source); + LocalDateTime then = LocalDateTime.ofInstant(target.toInstant(), ZoneId.systemDefault()); + Duration duration = Duration.between(then, now); + return duration.toHours(); + } + + /** + * Provides the difference in days between the given dates. + * + * @param source The reference date to calculate the duration from. Typically, this would be the current date. + * @param target The date from which the elapsed time is calculated. + * @return The difference in days between the two dates. + */ + public static long sinceDays(Date source, Date target) { + if (source == null || target == null) { + return 0; + } + LocalDateTime now = transform(source); + LocalDateTime then = LocalDateTime.ofInstant(target.toInstant(), ZoneId.systemDefault()); + Duration duration = Duration.between(then, now); + return duration.toDays(); + } + + /** + * Provides the difference human-readable between the given dates. + * + * @param source The reference date to calculate the duration from. Typically, this would be the current date. + * @param target The date from which the elapsed time is calculated. + * @return The difference in human-readable between the two dates. + */ + public static String sinceSmallRecently(Date source, Date target) { + if (source == null || target == null) { + return "unknown"; + } + long days = sinceDays(source, target); + if (days > 0) { + return String.format("%d (d)", days); + } + long hours = sinceHours(source, target); + if (hours > 0) { + return String.format("%d (h)", hours); + } + long minutes = sinceMinutes(source, target); + if (minutes > 0) { + return String.format("%d (m)", minutes); + } + long seconds = sinceSeconds(source, target); + if (seconds > 0) { + return String.format("%d (s)", seconds); + } + long mills = sinceMilliseconds(source, target); + return String.format("%d (ms)", mills); + } + + /** + * Provides the difference human-readable between the given dates. + * + * @param target The date from which the elapsed time is calculated. + * @return The difference in human-readable between the two dates. + */ + public static String sinceSmallRecently(Date target) { + return sinceSmallRecently(new Date(), target); + } + /** * Retrieves the ZoneId corresponding to the given time zone identifier. * If the provided time zone identifier is empty or null, the system default ZoneId is returned.