Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

time passed calculation

by frank1 (Beadle)
on Jan 27, 2022 at 17:22 UTC ( [id://11140904]=perlquestion: print w/replies, xml ) Need Help??

frank1 has asked for the wisdom of the Perl Monks concerning the following question:

Am having a problem with my last seen time script. its outputting is not normal.

it outputs wrong results, let me say i can set Seen time to current one, it says seen 1 hour ago. yet its just 2 minutes ago

Thanks i appreciate for help.

#!/usr/bin/perl use strict; use warnings; use Date::Parse; use DateTime; use POSIX qw/strftime/; my $Datetime = DateTime->now; my $Seen = '01/27/2022 07:40:03'; my $CurrentDateTime = strftime('%m/%d/%Y %H:%M:%S',localtime); my $SeenDateTime = DateTime->from_epoch( epoch => str2time( $Seen ) ); my $CurrentDate_AndTime = DateTime->from_epoch( epoch => str2time( $Cu +rrentDateTime ) ); my $diff = $CurrentDate_AndTime->subtract_datetime( $SeenDateTime ); my ( $years, $months, $weeks, $days, $hours, $minutes ) = $diff->in_units(qw( years months weeks days hours minutes )); my $SeenTime =''; if ( $years >= 1 ) { $SeenTime = "Seen $years Years Ago"; } elsif ( $months > 1 ) { $SeenTime = "Seen $months Month Ago"; } elsif ( $months == 1 ) { $SeenTime = "Seen $months Month Ago"; } elsif ( $weeks > 1 ) { $SeenTime = "Seen $weeks Weeks Ago"; } elsif ( $weeks == 1 ) { $SeenTime = "Seen $weeks Weeks Ago"; } elsif ( $days > 1 ) { $SeenTime = "Seen $days Days Ago"; } elsif ( $days == 1 ) { $SeenTime = "Seen $days Days Ago"; } elsif ( $hours > 1 ) { $SeenTime = "Seen $hours Hours Ago"; } elsif ( $hours == 1 ) { $SeenTime = "Seen $hours Hours Ago"; } elsif ( $minutes > 1 ) { $SeenTime = "Seen $minutes Minutes Ago"; } elsif ( $minutes == 1 ) { $SeenTime = "Seen $minutes Minutes Ago"; } elsif ( $Seen == $CurrentDateTime) { $SeenTime = "Online"; } print $SeenTime;

Replies are listed 'Best First'.
Re: time passed calculation
by haukex (Archbishop) on Jan 27, 2022 at 20:04 UTC

    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

      thank you

Re: time passed calculation
by choroba (Cardinal) on Jan 27, 2022 at 17:37 UTC
    I can't reproduce your problem.

    But the last comparison is definitely not doing what you think:

    $Seen == $CurrentDateTime
    Both the values are strings, so the numeric equality will interpret them in numeric context, leaving just the month. So, if you run the check around the midnight of a month break, there will be no output at all.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      after uploading the script this is the error i get, how can i fix it

      [Thu Jan 27 13:40:19 2022] [error] [pid 21201] mod_cgi.c(175): [client + 197.234.242.191:44580] AH01215: Use of uninitialized value in lc at +(eval 9) line 15.: /usr/home/file.pl [Thu Jan 27 13:40:19 2022] [error] [pid 21201] mod_cgi.c(175): [clien +t 197.234.242.191:44580] AH01215: Validation failed for type named Nu +m declared in package Specio::Library::Builtins (/usr/local/share/per +l/5.26.1/Specio/Library/Builtins.pm) at line 139 in sub named (eval) +with value undef: /usr/home/file.pl [Thu Jan 27 13:40:19 2022] [error] [pid 21201] mod_cgi.c(175): [clien +t 197.234.242.191:44580] AH01215: Trace begun at Specio::Exception->n +ew line 57: /usr/home/file.pl [Thu Jan 27 13:40:19 2022] [error] [pid 21201] mod_cgi.c(175): [clien +t 197.234.242.191:44580] AH01215: Specio::Exception::throw('Specio::E +xception', 'message', 'Validation failed for type named Num declared +in package Specio::Library::Builtins (/usr/local/share/perl/5.26.1/Sp +ecio/Library/Builtins.pm) at line 139 in sub named (eval) with value +undef', 'type', 'Specio::Constraint::Simple=HASH(0x55cf92bf0450)', 'v +alue', undef) called at (eval 226) line 88: /usr/home/file.pl [Thu Jan 27 13:40:19 2022] [error] [pid 21201] mod_cgi.c(175): [clien +t 197.234.242.191:44580] AH01215: DateTime::_check_from_epoch_params( +'epoch', undef) called at /usr/local/lib/x86_64-linux-gnu/perl/5.26.1 +/DateTime.pm line 481: /usr/home/file.pl [Thu Jan 27 13:40:19 2022] [error] [pid 21201] mod_cgi.c(175): [clien +t 197.234.242.191:44580] AH01215: DateTime::from_epoch('DateTime', 'e +poch', undef) called at file.pl line 63: /usr/home/file.pl

        The last line of output seems to say that you are passing undef as the epoch. You need an actual number there, so you need to figure out why you do not have one. Can you duplicate the problem by running file.pl interactively? See the from_epoch() documentation if there is any question about how to call it.

        its funny that it works in chrome, but not working in Fire Fox mozila and Microsoft edage

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11140904]
Approved by choroba
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-28 13:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found