Get Time From String - Perl - perl

In php you could just do
strtotime("+1 days");
And get the machine time for the next day.
I want to try the same with Perl, I'm going to be doing a sort of cron job to execute certain methods.
I know you could it use str2time from the module http://search.cpan.org/~gaas/HTTP-Date-6.02/lib/HTTP/Date.pm I just can't seem to figure it out.
I tried the following, but I'm unsure if I did it right
use HTTP::Date qw(str2time);
use Time::Piece;
use Time::Seconds;
my $lol = localtime();
my $time = $lol - ONE_HOUR*($lol->hour + 24);
print "Time: " . str2time($time);

use Time::Piece;
use Time::Seconds;
my $t1 = localtime() + ONE_DAY;
print $t1->epoch;

Tool for the job here is Time::Piece.
#!/usr/bin/perl
use strict;
use warnings;
use Time::Piece;
my $this_time = localtime();
print $this_time + 60*60*24,"\n";

Related

Get previous hour of UTC/GMT time using Perl

I have a script which will print Start & End time of previous hour of UTC/GMT.
#!/usr/local/bin/perl
use strict;
use warnings;
use POSIX qw(strftime);
my ($tmp_date, $tmp_hour, $Start, $End);
my $date = strftime '%Y-%m-%d', gmtime();
print "Date:$date\n";
my $hour = strftime '%H', gmtime();
print "Hour:$hour\n";
if ($hour == "00"){
$tmp_date = $date-1;
$tmp_hour = "23";
} else {
$tmp_hour = $hour-1;
$tmp_date = $date;
}
$a = length($tmp_hour);
if ($a == 1 ){
$tmp_hour="0".$tmp_hour;
}
$Start = $tmp_date.".".$tmp_hour."00";
$End = $tmp_date.".".$hour."05";
if ($End =~ /0005/){
$tmp_date = `TZ=GMT-12 date +%Y%m%d`;
$End =$tmp_date.".".$hour."05";
}
print "Start:$Start, End:$End\n";
For example, lets say now UTC time is: Wed Jun 10 10:18:57 UTC 2020
This should print Start & End time as 2020-06-10.0900 2020-06-10.1005 respectively.
This script is working as expected. But when Daylight savings happens will there be any impact on fetching Start & End time?
I want experts suggestions how can I avoid unnecessary if statements and achieve it by the use of Perl module itself.
PS: Perl version: v5.10.1. Please suggest Perl modules which comes with standard Perl installation (Ex: POSIX, Time::Local etc.) for solution of above problem.
As you're using gmtime(), any DST changes will have no effect at all.
I'm not sure why your end time ends with '05', I would have thought that the end of the hour comes at '00'.
Here's how I'd write it with Time::Piece and Time::Seconds.
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use Time::Piece;
use Time::Seconds;
my $end = gmtime->truncate(to => 'hour');
my $start = $end - ONE_HOUR;
my $format = '%Y-%m-%d %H:%M:%S';
say 'Start: ', $start->strftime($format);
say 'End: ', $end->strftime($format);
If you really want the end time to be five past the hour, then add this line after the ONE_HOUR line:
$end += (5 * ONE_MINUTE);
You can, of course, use any of the standard strftime() sequences to change the format of the output.

program to calculate the time

I would like to calculate the the current time and add 2 minutes to it and print the output in the following format. HH:MM . I searched online and came to know that there are lot of CPAN modules that can be used to implement this. But I'd like to do it without cpan modules.
$current_time = time();
$new_time = $current_time + (2*60); // adding two minutes
print( ' the time is ' . $ new_time ) ;
Output : the time is 1424906904
I searched online and came to know that we need to use POSIX perl interface to print the time in the appropriate format. However i'd like to know if there is a way to do this without using any cpan modules.
You can use localtime:
print scalar localtime($current_time);
Or you can run localtime's return values through POSIX::strftime (which is distributed with Perl as a core module):
use POSIX qw(strftime);
print strftime('%Y-%m-%d %H:%M:%S', localtime $current_time);
That's easy to do with localtime. Hours, minutes, and seconds are the 2nd, 1st, and 0th values returned. For example:
my ($sec, $min, $hours) = localtime(time()+120); # add 120 seconds
printf "%02d:%02d:%02d\n", $hours, $min, $sec;
Time::Piece and Time::Seconds have been included with all Perl installations since 2007.
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Time::Piece;
use Time::Seconds;
my $time = localtime;
$time += 2 * ONE_MINUTE;
say $time->strftime('%H:%M');

Date::Manip::Delta - the number of seconds [duplicate]

The code below only expresses the difference in months and days like so:
0:2:0:5:0:0:0
So it works, but I want to know the total number of days given that $ADDate can vary quite a bit. Hopefully this is simple, and I just completely missed how to do it.
#!/usr/bin/perl
use Date::Manip 6.42;
my $ADDate = "20131211000820.0Z";
my $var;
my #val;
my $diff;
calc_period($ADDate = "20131211000820.0Z");
sub calc_period
{
$ADDate =~ s/^([\d][\d][\d][\d])([\d][\d])([\d][\d])/$1-$2-$3/gs;
$ADDate =~ s/.........$//gs;
$today = ParseDate("today");
$beginning = ParseDate($ADDate);
$end = ParseDate($today);
$delta = DateCalc($beginning,$end,\$err,1);
#$delta =~ s/([\d+][:][\d+]):.*$/$1/gs;
print "$delta\n";
print "$ADDate\n";
}
I'm not familiar with Date::Manip, but I think another way to do this is to use Time::Piece to parse your string and do whatever you like with that since taking the difference of two Time::Piece object returns a Time::Seconds object.
The following example will show the difference of the current time and the hardcoded time and show it in days.
#!/usr/bin/perl
use strict;
use warnings;
use Time::Piece;
use Time::Seconds;
my $d = "20131211000820.0Z";
my $t = Time::Piece->strptime($d, "%Y%m%d%H%M%S.0Z");
my $now = Time::Piece->localtime();
my $diff = Time::Seconds->new($now - $t);
print $diff->days, "\n";
NigoroJr has already given you an answer. However, just as an FYI, the following is how I would clean up the code you originally provided:
#!/usr/bin/perl
use Date::Manip 6.42;
use strict;
use warnings;
calc_period("20131211000820.0Z");
sub calc_period {
my $date = shift;
$date =~ s/^(\d{4})(\d{2})(\d{2}).*/$1-$2-$3/;
my $beginning = ParseDate($date);
my $end = ParseDate("today");
my $delta = DateCalc($beginning, $end, \my $err, 1);
#$delta =~ s/([\d+][:][\d+]):.*$/$1/gs;
print "$delta\n";
print "$date\n";
}
Biggest differences being the proper use of a function and scoped variables, and a simplification of your regex.
I was unable to find a clean way to get Date::Manip to output a strict delta in days though, so the other module is the way to go.

How do I get last year date and current date using perl?

I want to fetch current date and exactly last year date using perl in the format of 140220 and 130220.
Perhaps the following will help:
use strict;
use warnings;
use Time::Piece;
my $time = Time::Piece->new;
my $currDate = $time->strftime('%y%m%d');
print $currDate, "\n";
my $lastYear = $time->add_years(-1)->strftime('%y%m%d');
print $lastYear;
Output:
140219
130219
Here is a sample using DateTime.
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw/say/;
use DateTime;
my $dt = DateTime->now();
my $year_ago = DateTime->now()->subtract(years => 1);
say $dt->strftime("%y%m%d");
say $year_ago->strftime("%y%m%d");

Perl Test::MockTime + Time::localtime

I'm trying to replace current time with Test::MockTime module. It works fine:
use Test::MockTime qw(:all);
use Time::Local;
my $sec = 0;
my $min = 0;
my $hour = 14;
my $mday = 1; #1-31
my $mon = 1; #1-12
my $year = 2013; #1970-...
set_fixed_time(timelocal($sec,$min,$hour,$mday,$mon-1,$year-1900));
print join "\n", localtime;
But, when I use Time::localtime, nothing happens:
use Time::localtime;
use Time::Local;
use Test::MockTime qw(:all);
my ($sec,$min,$hour,$mday,$mon,$year)=(0,0,14,1,1,2013);
set_fixed_time(timelocal($sec,$min,$hour,$mday,$mon-1,$year-1900));
my $t=localtime();
my $xmon=$t->mon;
my $xyear=$t->year;
my $xday=$t->mday;
my $xmon_now=$xmon+1;
my $xyear_now=$xyear+1900;
print "$xmon_now $xyear_now\n";
The output will be "12 2012"
How can I change time in my tests when Time::localtime is used?
Thanks and sorry for my English
UPD:
use Time::localtime;
use Test::MockTime qw(:all);
didnt work
use Test::MockTime qw(:all);
use Time::localtime;
works fine) perl magic
Test::MockTime overrides localtime, but Time::localtime uses CORE::localtime (the unoverrided version of localtime). You'd have to rewrite Time::localtime's localtime.