in reply to time passed calculation
I would recommend you don't mix POSIX's strftime, Date::Parse, and DateTime. Since you're already using DateTime, you can use its friends DateTime::Format::Strptime for parsing and DateTime::Format::Human::Duration for output.
use warnings; use strict; use DateTime; use DateTime::Format::Strptime; use DateTime::Format::Human::Duration; my $strp = DateTime::Format::Strptime->new( pattern => '%m/%d/%Y %H:%M:%S', on_error=>'croak', time_zone=>'UTC' ); my $durfmt = DateTime::Format::Human::Duration->new(); my $now = $strp->parse_datetime('01/27/2022 07:40:03'); my @tests = ( '01/26/2021 07:40:00', '03/20/2021 07:40:00', '12/20/2021 07:40:00', '01/23/2022 07:40:00', '01/26/2022 07:40:00', '01/27/2022 06:40:00', '01/27/2022 07:15:00', '01/27/2022 07:39:00', '01/27/2022 07:40:03', '01/27/2022 07:40:06', ); for my $t (@tests) { my $seen = $strp->parse_datetime($t); print $now->strftime('%m/%d/%Y %H:%M:%S'), " -> ", $seen->strftime('%m/%d/%Y %H:%M:%S'), " = ", $durfmt->format_duration_between($now, $seen, significant_units => 1, future => 'in %s', past => '%s ago', no_time => 'online now', ), "\n"; } __END__ 01/27/2022 07:40:03 -> 01/26/2021 07:40:00 = 1 year ago 01/27/2022 07:40:03 -> 03/20/2021 07:40:00 = 10 months ago 01/27/2022 07:40:03 -> 12/20/2021 07:40:00 = 1 month ago 01/27/2022 07:40:03 -> 01/23/2022 07:40:00 = 4 days ago 01/27/2022 07:40:03 -> 01/26/2022 07:40:00 = 1 day ago 01/27/2022 07:40:03 -> 01/27/2022 06:40:00 = 1 hour ago 01/27/2022 07:40:03 -> 01/27/2022 07:15:00 = 25 minutes ago 01/27/2022 07:40:03 -> 01/27/2022 07:39:00 = 1 minute ago 01/27/2022 07:40:03 -> 01/27/2022 07:40:03 = online now 01/27/2022 07:40:03 -> 01/27/2022 07:40:06 = in 3 seconds
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: time passed calculation
by frank1 (Monk) on Jan 28, 2022 at 18:42 UTC |