-
-
Save Grinnz/34a635c072ab913b33360cf65714eeac to your computer and use it in GitHub Desktop.
perl version of fromNow from moment.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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