Skip to content

Commit

Permalink
✨ feat: add constant time format #3
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Oct 4, 2024
1 parent 8b19370 commit db60401
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
41 changes: 41 additions & 0 deletions plugin/src/main/groovy/org/unify4j/common/Time4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,47 @@ public static String since(Date source, Date target) {
}
}

/**
* Formats the given epoch time into a human-readable "time ago" format.
* Examples:
* - "Just now" for times within a minute.
* - "X seconds ago," "X minutes ago," "X hours ago," "X days ago."
*
* @param epochMilli The epoch time in milliseconds.
* @return A string representing the time difference.
*/
public static String since(long epochMilli) {
Instant now = Instant.now();
Instant instant = Instant.ofEpochMilli(epochMilli);
LocalDate _now = LocalDate.now();
LocalDate local = Instant.ofEpochMilli(epochMilli).atZone(ZoneId.systemDefault()).toLocalDate();
// Calculate duration for seconds, minutes, hours, days
Duration duration = Duration.between(instant, now);
long seconds = duration.getSeconds();
long minutes = duration.toMinutes();
long hours = duration.toHours();
long days = duration.toDays();

// Calculate period for months and years
Period period = Period.between(local, _now);
int months = period.getMonths();
int years = period.getYears();

if (seconds < 60) {
return seconds <= 1 ? "Just now" : seconds + " seconds ago";
} else if (minutes < 60) {
return minutes == 1 ? "1 minute ago" : minutes + " minutes ago";
} else if (hours < 24) {
return hours == 1 ? "1 hour ago" : hours + " hours ago";
} else if (days < 30) {
return days == 1 ? "1 day ago" : days + " days ago";
} else if (months < 12) {
return months == 1 ? "1 month ago" : months + " months ago";
} else {
return years == 1 ? "1 year ago" : years + " years ago";
}
}

/**
* Provides the difference in milliseconds between the given dates.
*
Expand Down
43 changes: 42 additions & 1 deletion plugin/src/main/groovy/org/unify4j/text/TimeFormatText.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,46 @@ public class TimeFormatText {
* Description: Pattern for date and time including year, month, day, hour, minutes, seconds, milliseconds, and timezone offset.
*/
public static final String ISO_DATE_TIME_WITH_TIMEZONE_OFFSET = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
}

/**
* Format: dd.MM.yyyy
* Description: Custom pattern for date only including day, month, and year with period as separator.
*/
public static final String CUSTOM_DATE_DOT_PATTERN = "dd.MM.yyyy";

/**
* Format: HH.mm
* Description: Custom short pattern for time only including hour and minutes with period as separator.
*/
public static final String CUSTOM_TIME_HH_MM_DOT_PATTERN = "HH.mm";

/**
* Format: HH.mm.ss
* Description: Custom short pattern for time including hour, minutes, and seconds with period as separator.
*/
public static final String CUSTOM_TIME_HH_MM_SS_DOT_PATTERN = "HH.mm.ss";

/**
* Format: yyyy.MM.dd
* Description: Custom pattern for date only including year, month, and day with period as separator.
*/
public static final String CUSTOM_DATE_YEAR_FIRST_DOT_PATTERN = "yyyy.MM.dd";

/**
* Format: dd/MM/yyyy
* Description: Custom pattern for date only including day, month, and year with slashes as separator.
*/
public static final String CUSTOM_DATE_DD_MM_YYYY_SLASH_PATTERN = "dd/MM/yyyy";

/**
* Format: yyyy/MM/dd
* Description: Custom pattern for date only including year, month, and day with slashes as separator.
*/
public static final String CUSTOM_DATE_YYYY_MM_DD_SLASH_PATTERN = "yyyy/MM/dd";

/**
* Format: yyyyMMddHHmmss.SSSX
* Description: Compact pattern for date and time including year, month, day, hour, minutes, seconds, milliseconds, and timezone.
*/
public static final String CUSTOM_DATE_TIME_COMPACT_WITH_MILLISECONDS_PATTERN = "yyyyMMddHHmmss.SSSX";
}

0 comments on commit db60401

Please sign in to comment.