Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

execute if date is greater then 2hours

by snehit.ar (Beadle)
on Jun 28, 2017 at 07:11 UTC ( [id://1193762]=perlquestion: print w/replies, xml ) Need Help??

snehit.ar has asked for the wisdom of the Perl Monks concerning the following question:

Want to compare 2 date and if hour is more then 2 hours then its should enter if condition and perform the action .
#!/usr/bin/perl use warnings; use strict; use DateTime; use DateTime::Duration; # my $file = 'C:/Users/snehit.rahate/Perl/events.xml'; # #my $xpath = q{//custom_attribute_list/custom_attribute[normalize-sp +ace(name)='SLB_SSRID']/value}; # my $xp = XML::XPath->new(filename=>$file); # my $nodeset = $xp->find("//custom_attribute_list/custom_attribute[no +rmalize-space(name)='SLB_SSRID']/value"); # for my $node ($nodeset->get_nodelist) { # print " XML::XPath: ",$node->string_value,"\n"; # } use DateTime::Format::Duration; my $dtnow = DateTime->now; $dtnow =~ s/T/ /; $dtnow =~ m/(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/gi; my $now_hour = $4; my $now_minute = $5; my $now_second = $6; my $dtevent = '2017-06-15 13:14:31'; $dtevent =~ m/(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/gi; my $event_hour = $4; my $event_minute = $5; my $event_second = $6; my $diffhour = $now_hour - $event_hour; my $diffminute = $now_minute - $event_minute; my $diffsecond = $now_second - $event_second; my $event_time = $diffhour.":".$diffminute.":".$diffsecond; print $event_time; if ($event_time > "2:00:00") { print "Start the service if time is more then 2 hours"; }
Error :Argument "2:00:00" isn't numeric in numeric gt (>) at test.pl line 37. Argument "-6:-8:15" isn't numeric in numeric gt (>) at test.pl line 37. -6:-8:15

Replies are listed 'Best First'.
Re: execute if date is greater then 2hours
by haukex (Archbishop) on Jun 28, 2017 at 08:01 UTC

    If you've already got DateTime objects, then you should use their methods to do all your date/time math, and not be working with regexes or manual date/time math. To turn a string into a DateTime object, I like DateTime::Format::Strptime. Note that you need to supply the correct time zone to DateTime::Format::Strptime for the comparison to work, and also that subtract_datetime_absolute is "the only way to accurately measure the absolute amount of time between two datetimes, since units larger than a second do not represent a fixed number of seconds." (Although in this case that level of accuracy might not be needed, and regular DateTime::Duration calculations might be enough.)

    use warnings; use strict; use DateTime::Format::Strptime; my $now = DateTime->now; print "$now\n"; my $strp = DateTime::Format::Strptime->new(on_error=>'croak', pattern => '%Y-%m-%d %H:%M:%S', time_zone=>'UTC'); my $dtevent = $strp->parse_datetime('2017-06-28 05:30:31'); print "$dtevent\n"; my $diff_sec = $now->subtract_datetime_absolute($dtevent) ->in_units('seconds'); my $diff_hours = $diff_sec/(60*60); print "$diff_sec s / $diff_hours h\n"; if ($diff_hours>2) { print "$dtevent was more than 2 hours ago\n"; }
      Thank you. @haukex , not only share the solution but also explain well about the cause . Appreciate you help.
Re: execute if date is greater then 2hours
by 1nickt (Canon) on Jun 28, 2017 at 07:57 UTC

    (Edit: As haukex mentions below, DateTime has date/time "mathematics" methods, if you need them. It wasn't clear to me from your question whether you need that or whether you are wanting to know the difference between the hour components of your dates, which is what I show here.)

    Try something simpler.

    use strict; use warnings; use feature 'say'; use DateTime::Format::Strptime; my $parser = DateTime::Format::Strptime->new( pattern => '%F %T', ); my $now = DateTime->now->hour; my $evt = $parser->parse_datetime('2017-06-15 13:14:31')->hour; my $diff = $now - $evt; say "now: $now"; say "evt: $evt"; say "diff: $diff"; __END__
    Output:
    $ perl 1193762.pl now: 7 evt: 13 diff: -6

    Hope this helps!


    The way forward always starts with a minimal test.
      I really appreciated your help ! Thanks.

Log In?
Username:
Password:

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

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

    No recent polls found