Reputation: 57
I have a task in perl in which I need to execute 'some_code' but only if date is older than 24 hours counting from now. I'm trying the below code but it doesn't seems to be working.
sub function {
use Date::Manip::Date
use Date::Parse
use Date::Format;
my $yesterday = time() - 60*60*24;
my $x = shift;
my $env = shift;
$env->{some_code} = 1 if $x < $yesterday;
return $x;
}
Upvotes: 0
Views: 356
Reputation: 290
#! /usr/bin/env perl
use Modern::Perl;
use Data::Dumper;
use DateTime;
my $now = DateTime->new(
year => 2012, month => 10, day => 18,
hour => 17, minute => 30,
time_zone => 'UTC'
);
# my $now = DateTime->now(time_zone => 'UTC');
my $last_run = DateTime->new(
year => 2012, month => 10, day => 17,
hour => 19, minute => 30,
time_zone => 'UTC'
);
my $duration= $now->subtract_datetime($last_run);
say "hours: " . $duration->hours;
Result:
hours: 22
see also:
Upvotes: 1
Reputation: 3465
You can do it easily, only using core functions.
#!/usr/bin/perl
use strict;
my $new_time = 1350570164; # 2012-10-18 14:22:44
my $older_time = 1350450164; # 2012-10-17 05:02:44
printf "time in sec: %d older that 24 hours: %d\n", $new_time, is_time_older_24($new_time);
printf "time in sec: %d older than 24 hours: %d\n", $older_time, is_time_older_24($older_time);
sub is_time_older_24 {
my $given_time = shift;
my $yesterday_time = time() - 60 * 60 * 24;
return $given_time <= $yesterday_time
? 1
: 0;
}
Output:
time in sec: 1350570164 older that 24 hours: 0
time in sec: 1350450164 older than 24 hours: 1
Upvotes: 1