Add API to display relative dates

This commit is contained in:
Ilia Ross
2025-08-29 15:13:27 +03:00
parent 16110e3e3e
commit 3be8a327ad
3 changed files with 72 additions and 1 deletions

File diff suppressed because one or more lines are too long

30
lang/en
View File

@@ -438,6 +438,36 @@ file_truncated_message=fetched $1 of data, truncated $2 out of $3
file_truncated_message_head=fetched beginning $1 of data, truncated $2 out of $3
file_truncated_message_tail=fetched ending $1 of data, truncated $2 out of $3
time_ago_year=$1 year ago
time_ago_years=$1 years ago
time_ago_month=$1 month ago
time_ago_months=$1 months ago
time_ago_week=$1 week ago
time_ago_weeks=$1 weeks ago
time_ago_day=$1 day ago
time_ago_days=$1 days ago
time_ago_hour=$1 hour ago
time_ago_hours=$1 hours ago
time_ago_min=$1 minute ago
time_ago_mins=$1 minutes ago
time_ago_sec=$1 second ago
time_ago_secs=$1 seconds ago
time_in_year=In $1 year
time_in_years=In $1 years
time_in_month=In $1 month
time_in_months=In $1 months
time_in_week=In $1 week
time_in_weeks=In $1 weeks
time_in_day=In $1 day
time_in_days=In $1 days
time_in_hour=In $1 hour
time_in_hours=In $1 hours
time_in_min=In $1 minute
time_in_mins=In $1 minutes
time_in_sec=In $1 second
time_in_secs=In $1 seconds
time_now=Just now
defcert_error=Default $1 bundled SSL certificate is being used. It is highly advised to update default <tt>$2</tt> certificate before proceeding with login.
main_error_details=Error details

View File

@@ -2409,6 +2409,47 @@ if (ref($only) || !$only) {
return $date;
}
# make_date_relative(timestamp)
# Return localized relative time from now or to the future
# for a given timestamp, or undef if not a valid timestamp
sub make_date_relative
{
my $ts = shift;
return undef unless(defined($ts) && $ts =~ /^-?\d+(?:\.\d+)?$/);
$ts = 0 + $ts;
my $last_login;
my $now = time();
my $diff = $ts - $now; # >0 future, <0 past
my $abs = abs($diff);
return $text{'time_now'} if ($abs < 10); # within ±10 seconds is now
my @units = (
[ 31536000, 'year', 'years' ],
[ 2592000, 'month', 'months' ],
[ 604800, 'week', 'weeks' ],
[ 86400, 'day', 'days' ],
[ 3600, 'hour', 'hours' ],
[ 60, 'min', 'mins' ],
[ 1, 'sec', 'secs' ],
);
my ($val, $sing, $plur);
foreach my $unit (@units) {
my ($secs, $s, $p) = @$unit;
next if ($abs < $secs);
$val = int($abs / $secs);
($sing, $plur) = ($s, $p);
last;
}
if (defined($val) && $val > 0) {
my $key = ($diff > 0 ? 'time_in_' : 'time_ago_').
($val == 1 ? $sing : $plur);
$last_login = &text($key, $val);
}
else {
$last_login = $text{'time_now'};
}
return $last_login;
}
=head2 file_chooser_button(input, type, [form], [chroot], [addmode])
Return HTML for a button that pops up a file chooser when clicked, and places