Skip to content

Instantly share code, notes, and snippets.

@Grinnz
Last active December 4, 2017 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Grinnz/34a635c072ab913b33360cf65714eeac to your computer and use it in GitHub Desktop.
Save Grinnz/34a635c072ab913b33360cf65714eeac to your computer and use it in GitHub Desktop.
perl version of fromNow from moment.js
use Math::Round 'round';
use Time::Seconds;
sub fromNow {
my $seconds = abs(shift() // 0);
if ($seconds == 0) {
return '0 seconds';
} elsif ($seconds < 45) {
return 'a few seconds';
} elsif ($seconds < 90) {
return 'a minute';
} elsif ($seconds < 45 * ONE_MINUTE) {
return round($seconds / ONE_MINUTE) . ' minutes';
} elsif ($seconds < 90 * ONE_MINUTE) {
return 'an hour';
} elsif ($seconds < 22 * ONE_HOUR) {
return round($seconds / ONE_HOUR) . ' hours';
} elsif ($seconds < 36 * ONE_HOUR) {
return 'a day';
} elsif ($seconds < 25 * ONE_DAY) {
return round($seconds / ONE_DAY) . ' days';
} elsif ($seconds < 46 * ONE_DAY) {
return 'a month';
} elsif ($seconds < 345 * ONE_DAY) {
return round($seconds / ONE_MONTH) . ' months';
} elsif ($seconds < 548 * ONE_DAY) {
return 'a year';
} else {
return round($seconds / ONE_YEAR) . ' years';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment