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.
Related
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.
I am fairly new in Perl.
I am trying to subtract two dates in this format
15.07.16 23:13:34
15.07.16 20:04:24
I know that I have to convert this string in a date object. My problem is I am restricted to the basic perl without installing extra packages. Is there a way to do it?
My Version is v5.8.4 and the output should be 03:09:10.
You say that you're using Perl 5.8.4. You really need to get that updated and get the ability to install CPAN modules.
But, here's a way to do what you want using only core Perl functionality that was available in 5.8.4.
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
my $date1 = '15.07.16 23:13:34';
my $date2 = '15.07.16 20:04:24';
my $diff = date2sec($date1) - date2sec($date2);
print secs2duration($diff);
sub date2sec {
my ($date) = #_;
my ($day, $mon, $yr, $hr, $min, $sec) = split(/[. :]/, $date);
# I've assumed that your timestamps are in your local timezone,
# so I've used timelocal() here. If your timestamps are actually
# UTC, you should use timegm() instead.
return timelocal($sec, $min, $hr, $day, $mon-1, 2000 + $yr);
}
sub secs2duration {
my ($secs) = #_;
my $hours = int($secs / (60*60));
$secs %= (60*60);
my $mins = int($secs / 60);
$secs %= 60;
return sprintf '%02d:%02d:%02d', $hours, $mins, $secs;
}
I have a variable which contain value "20140720". I need to change it to the format "20/07".
My code is shown below.
#!/usr/bin/perl
use strict;
use warnings;
use Time::Piece;
my $date = '20140720';
my $date_format = Time::Piece->strptime($date, '%d/%m');
my $new_date = $date_format->strftime('%d/%m');
print $new_date;
I get following error during execution.
Error parsing time at /usr/lib/perl5/5.10.0/x86_64-linux-thread-multi/Time/Piece.pm line 470.
In this line — Time::Piece->strptime($date, '%d/%m'); — you specified the format that $date is currently in incorrectly. The second argument describes how the string should be parsed, not the format you want it to be in (which is what the following line is for).
Use '%Y%m%d' instead.
With a fixed string, you should use the pack/unpack function:
use strict;
use warnings;
my $date = '20140720';
my (undef, $m, $d) = unpack 'A4A2A2', $date;
print "$d/$m";
If you don't need further date processing, using a simple regular expression may be simpler:
use strict;
use warnings;
my $date = '20140720';
my $new_date = $date;
$new_date =~ s!\d{4}(\d{2})(\d{2})$!$2/$1!;
print $new_date, "\n";
I'm working on a Perl program at work and stuck on (what I think is) a trivial problem. I simply need to build a string in the format '06/13/2012' (always 10 characters, so 0's for numbers less than 10).
Here's what I have so far:
use Time::localtime;
$tm=localtime;
my ($day,$month,$year)=($tm->mday,$tm->month,$tm->year);
You can do it fast, only using one POSIX function. If you have bunch of tasks with dates, see the module DateTime.
use POSIX qw(strftime);
my $date = strftime "%m/%d/%Y", localtime;
print $date;
You can use Time::Piece, which shouldn't need installing as it is a core module and has been distributed with Perl 5 since version 10.
use Time::Piece;
my $date = localtime->strftime('%m/%d/%Y');
print $date;
output
06/13/2012
Update
You may prefer to use the dmy method, which takes a single parameter which is the separator to be used between the fields of the result, and avoids having to specify a full date/time format
my $date = localtime->dmy('/');
This produces an identical result to that of my original solution
use DateTime qw();
DateTime->now->strftime('%m/%d/%Y')
expression returns 06/13/2012
If you like doing things the hard way:
my (undef,undef,undef,$mday,$mon,$year) = localtime;
$year = $year+1900;
$mon += 1;
if (length($mon) == 1) {$mon = "0$mon";}
if (length($mday) == 1) {$mday = "0$mday";}
my $today = "$mon/$mday/$year";
use Time::Piece;
...
my $t = localtime;
print $t->mdy("/");# 02/29/2000
Perl Code for Unix systems:
# Capture date from shell
my $current_date = `date +"%m/%d/%Y"`;
# Remove newline character
$current_date = substr($current_date,0,-1);
print $current_date, "\n";
Formating numbers with leading zero is done easily with "sprintf", a built-in function in perl (documentation with: perldoc perlfunc)
use strict;
use warnings;
use Date::Calc qw();
my ($y, $m, $d) = Date::Calc::Today();
my $ddmmyyyy = sprintf '%02d.%02d.%d', $d, $m, $y;
print $ddmmyyyy . "\n";
This gives you:
14.05.2014
I am trying to convert given dates to the corresponding days of the week using Perl with Date::Day.
The input string is in the format: October 24, 2011; March 12, 1989; November 26, 1940.
I have written a script which will parse the above input and convert each date to a format which will be accepted by Date::Day::day().
This subroutine accepts the input in format, mm,dd,yyyy. I have done this using hashes. Recently posted a thread on this query on stackoverflow and with the help of other members, was able to do it.
Here's my script and it returns me ERR for each of the dates instead of returning the Day of Week corresponding to the date.
There seems to be something wrong with the input format of the parameter passed to day() subroutine.
Here's more documentation on the Perl Module I am using:
http://helpspy.com/c.m/programming/lang/perl/cpan/c06/Date/Day/d_1/
I am interested to know, where exactly I am going wrong. Do I have to make some modifications to the date before passing it as a parameter to the day() subroutine?
#!/usr/bin/perl
use Date::Day;
use strict;
use warnings;
my #arr;
print "Enter the string: ";
my $str=<>;
chomp $str;
my #dates= split /; /,$str;
my %days= ("January",1,"February",2,"March",3,"April",4,"May",5,"June",6,"July",7,"August",8,"September",9,"October",10,"November",11,"December",12);
my #output = map {
my $pattern=$_;
$pattern =~ s/(\S*)\s/$days{$1}, /;
$pattern =~ s/\s//g;
$pattern
} #dates;
print &day(11,9,1987); # for test purpose and it returns correct value
foreach $output (#output)
{
chomp $output;
my $result=&day($output);
push(#arr,$result);
}
foreach my $arr (#arr)
{
print $arr."; ";
}
The output of the above script is: ERR; ERR; ERR;
Date::Day looks like a rather old module. It was last updated almost nine years ago. That's not to say that it's broken, but these days the DateTime family of modules handle pretty much any date/time processing that you want. I'd strongly recommend taking a look at those instead.
Here's an example solving your problem using DateTime::Format::Strptime.
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use DateTime::Format::Strptime;
my $dt_parser = DateTime::Format::Strptime->new(
pattern => '%B %d, %Y'
);
while (<DATA>) {
chomp;
my $dt = $dt_parser->parse_datetime($_);
say $dt->day_name;
}
__END__
October 24, 2011
March 12, 1989
November 26, 1940
You're passing a string to the sub &day.
Here is a rewrite :
#!/usr/local/bin/perl
use Data::Dump qw(dump);
use strict;
use warnings;
use Date::Day;
print "Enter the string: ";
my $str=<>;
chomp $str;
my #dates= split /; /,$str;
my %days = ("January",1,"February",2,"March",3,"April",4,"May",5,"June",6,"July",7,"August",8,"September",9,"October",10,"November",11,"December",12);
my #output = map {
my #l = split/[ ,]+/;
$l[0] = $days{$l[0]};
[#l];
} #dates;
my #arr;
foreach my $date(#output) {
push #arr, &day(#$date);
}
dump#arr;
output:
("MON", "SUN", "TUE")