I need some help trying to figure out how to format dates in perl. I have a working perl script, with a regular expression, that works fine if I use hard coded date strings like this:
my $mon = 'Aug';
my $day = '05';
my $year = '2010';
These vars are used in a regular expression like this:
if ($line =~ m/(.* $mon $day) \d{2}:\d{2}:\d{2} $year: ([^:]+):backup:/)
Now, I need to automate this date portion of the code and use current date systematically.
I looked into perl localtime and tried using unix date and throw them into variables.
I need to have the days of the week, single digit, be padded with '0'. As in today, 'Aug' '05'
'2010' because the input file I am using for the regex has dates like this.
My 2nd try with the unix and formatting is returning numbers, but I need to have them be strings:
my $mon2=`date '+%b'`;
my $day2=`date '+%d'`;
my $year2=`date '+%Y'`;
My test code for playing with date formats:
#!/usr/local/bin/perl -w
use strict;
my $mon = 'Aug';
my $day = '05';
my $year = '2010';
my $mon2=`date '+%b'`;
my $day2=`date '+%d'`;
my $year2=`date '+%Y'`;
print "$mon";
print "$day";
print "$year";
print "$mon2";
print "$day2";
print "$year2";
My Output:
Aug052010Aug
05
2010
I hate to break it to you, but you're reinventing the wheel. All this is implemented quite comprehensively in the DateTime distribution and the DateTime::Format:: family of classes:
use DateTime;
my $dt = DateTime->now;
print 'It is currently ', $dt->strftime('%b %d %H:%M:%S'), ".\n";
prints:
It is currently Aug 05 23:54:01.
Related
I want to accept a user date on the command line in format
dd/mm/yyyy
then print the date out to the user in
yyyy/mm/dd
I am trying to use the Date::Parse module to parse into a date to be reprinted.
The Date:Parse docs show that I should be able to get $day, $month and $year from user input.
use Date::Parse;
$time = str2time($date);
($ss,$mm,$hh,$day,$month,$year,$zone) = strptime($date);
This is my current code:
use strict;
use Date::Parse;
print "Enter a date in dd/mm/yyy format: ";
my $user_date = <STDIN>;
my #date = strptime($user_date);
# ( $day, $month, $year ) = strptime($user_date);
# my $user_day = ( ($day) = strptime($user_date) );
print "%Y/%m/%d", #date;
However the print fails and it appears from output that entered 10 of 10 is 9 in output.
Output
Enter a date in dd/mm/yyy format: 16/10/1952
%Y/%m/%d1952916s
What should I do?
The documentation for Date::Parse isn't clear, but it looks like you get the values back in the format that localtime() would expect. The year, for example, seems to be the year minus 1900. This means that the month number will be 0 to 11 rather than 1 to 12.
Date::Parse hasn't been updated for over five years. I'd suggest that it should best be avoided these days. There are much better options to choose from. These include Time::Piece that has been included as a standard part of the Perl distribution since version 5.10.0. You can use its strptime() (string parse time) method to parse your string and its strftime() (string format time) method to format the date object as you like.
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use Time::Piece;
print "Enter a date in dd/mm/yyy format: ";
chomp(my $user_date = <STDIN>);
my $tp = Time::Piece->strptime($user_date, '%d/%m/%Y');
say $tp->strftime('%Y/%m/%d');
Update:
Also, it's really not clear what this line is supposed to do:
print "%Y/%m/%d", #date;
I think you were thinking of using the strftime() method from POSIX.pm.
print strftime "%Y/%m/%d", #date;
But with use warnings this generates warnings because of all the undefined values in #data (that's a rather bizarre design decision in that module and, in my opinion, another reason to avoid it). You can fix that by replacing:
my #date = strptime($user_date);
With:
my #date = map { $_ // 0 } strptime($user_date);
This question already has answers here:
How can I parse dates and convert time zones in Perl?
(5 answers)
Closed 7 years ago.
I have a date/time like this: 2015-07-31T13:30:00.000+01:00
And I want to convert it to normal date and time using Perl and Time::Piece->strptime
Here is my code:
sub changeDateFormat {
my ($date, $fromFormat, $toFormat) = (#_);
return Time::Piece->strptime($date, $fromFormat)->strftime($toFormat);
}
The call:
print changeDateFormat($that_date, '%Y-%m-%dT%H:%M:%S.%N+%z', '%Y:%m:%d');
I think that .000 are nano seconds and +01.00 stands for time zone.
But the given code gives this:
Error parsing time at /usr/lib64/perl5/Time/Piece.pm line 470
Any help is appreciated.
There's a couple of problems I think.
%N isn't in my strftime manpage. So that might well not work.
And %z - I'm pretty sure +01:00 isn't valid.
%z The +hhmm or -hhmm numeric timezone (that is, the hour and
minute offset from UTC). (SU)
This works though:
my $date = '2015-07-31T13:30:00+0100';
my $fromFormat = '%Y-%m-%dT%H:%M:%S%z';
print Time::Piece->strptime($date, $fromFormat);
So I'd suggest - unless your milliseconds are important - you could just strip those via a regex, and likewise the timezone. (And it they are important, I don't think Time::Piece does ms resolution anyway)
You can probably use a regular expression to 'correct' your input date if you were so inclined. I'm unsure if fits your use case but:
$date =~ s/\+(\d{2}):(\d{2})$/+$1$2/;
$date =~ s/\.\d{3}+/+/;
You can use strptime in Time::Piece and adding the time zone manually as shown in this answer, or you could try using DateTime::Format::Strptime instead:
use feature qw(say);
use strict;
use warnings;
use DateTime::Format::Strptime;
my $timestamp = '2015-07-31T13:30:00.000+0100';
my $strp = DateTime::Format::Strptime->new(
pattern => '%Y-%m-%dT%H:%M:%S.%N%z'
);
my $dt = $strp->parse_datetime( $timestamp );
say $dt->strftime('%Y:%m:%d');
Output:
2015:07:31
use DateTime;
use DateTime::Format::ISO8601;
use DateTime::Format::Strptime;
my $string = '2015-07-31T13:30:00.000+01:00';
my $date = DateTime::Format::ISO8601->parse_datetime( $string );
die "Error" unless $date;
my $formatter = new DateTime::Format::Strptime(pattern => '%Y-%m-%d %T');
$date->set_formatter($formatter);
print "$date\n";
My platform is Linux. I want the printed date to be formatted as Apr 3, 2014, 5:28 PM when given input such as 2014-04-03T17:28:54.864Z.
My current Perl script.
#!/usr/bin/perl
use lib '/tmp/DateTime/Format';
use DateTime::Format::ISO8601;
my $date = "2014-04-03T17:28:54.864Z";
my $iso8601 = DateTime::Format::ISO8601 -> new;
my $dt = $iso8601->parse_datetime( $date );
print "Date before conversion *******-> \"$date\"\n";
my $s6 = $dt->strftime("%b %-d, %Y, %I:%M %p");
print "Date after conversion *******-> \"$s6\"\n";
It gives the output Apr %-d, 2014, 05:28 PM. When the day of the month or hour is a single digit, I do not want zero or space padding. I want the output as Apr 3, 2014, 5:28 PM when day and hour are each a single digit.
I see no %-d option listed in the valid strftime patterns.
You could use %e to get a leading space instead of a leading zero but, since you discount that, you can get the day independently, then change it and use that changed value within the format string, something like (untested, but you'll get the idea):
# Get day and remove leading zero.
my $dayofmonth = $dt->strftime("%d");
$dayofmonth =~ s/^0//;
# Construct format string from "hard-coded" day.
my $s6 = $dt->strftime("%b " . $dayofmonth . ", %Y, %I:%M %p");
Try this:
my $s6 = $dt->format_cldr('MMM d, Y, h:mm a');
It makes use of the module:
DateTime::Format::CLDR - Parse and format CLDR time patterns
Which you can find documented here.
Because there aren't any strftime patterns for days other than %d and %e, the easy solution is just to edit the result after the fact
$s6 =~ s/\s+/ /g;
This would change your script to the following:
use strict;
use warnings;
use DateTime::Format::ISO8601;
my $date = "2014-04-03T17:28:54.864Z";
my $iso8601 = DateTime::Format::ISO8601->new;
my $dt = $iso8601->parse_datetime( $date );
print qq{Date before conversion *******-> "$date"\n};
my $s6 = $dt->strftime("%b %e, %Y, %I:%M %p");
$s6 =~ s/\s+/ /g;
print qq{Date after conversion *******-> "$s6"\n};
Need help parsing the datetime stamp and splitting it up by date and time.
use strict;
use warnings;
use Time::Piece;
my $string = "05:57:03 08/31/10 MDT";
print $string,"\n";
my $time = Time::Piece->strptime($string, "%H:%M:%S");
my $date = Time::Piece->strptime($string, "%Y/%m/%d");
print $time,$date,"\n";
Thanks! Also how do I figure out which day of week this is using code?
use DateTime::Format::Strptime;
my $s = DateTime::Format::Strptime->new(pattern => '%T %D %Z');
my $dt = $s->parse_datetime('05:57:03 08/31/10 MDT');
say $dt->strftime('%A'); # Tuesday
You should be able to use code like the following:
my $t = Time::Piece->strptime($string, "%H:%M:%S %m/%d/%y %Z");
However, on my system at least, I have to change the time zone MST to GMT for it to match; if I leave it as in your example, I get an error:
Perl> my $t = Time::Piece->strptime("05:57:03 08/31/10 DST", "%H:%M:%S %m/%d/%y %Z");
[!] Runtime error: Error parsing time at /usr/local/lib/perl/5.10.0/Time/Piece.pm line 469.
If it works for you, though, you'll have a Time::Piece object, on which you can call e.g. $t->day_of_week for the day of the week as a number, $t->day for e.g. 'Tue', or $t->fullday for e.g. 'Tuesday'.
See the documentation for Time::Piece for details on the methods you can call.
I had a problem with date manipulation.This is my scenario.I have a daily generating report files,they will be generated from Monday to Friday.Now I need to compare those reports.My perl code should work in such a way that if today is Monday I need to compare todays report with previous week Friday's report.And my date format will be like this 20100803 if i give like this i need to be compared with 20100802. If i give Monday's report i.e 20100802 it should compare with 20100730 i.e 30th of July. Can anyone please help me.
Thanks in Advance.
You'll probably want to look at some kind of Date/DateTime object like Date::Calc. With that one, for example, you could split the string into a day, month, and year (using Regex or whatever you'd like to use) for something like:
#
#Set $date = "20100308" somehow, based on your file structure
#
$date =~ /^(\d{4})(\d{2})(\d{2})$/;
$year = $1;
$month = $2;
$day = $3;
$dow = Day_of_Week($day, $month, $year);
if ( $dow == 1 )
$offset = -3; #Date is a Monday
} else {
$offset = -1; #Date is Tuesday-Friday
}
#Find the date of the last report
$prev_report_date = Add_Delta_Days($day, $month, $year, $offset);
#
#Compare reports
#
The above is just a generalized example, of course. I don't know exactly how your files are structure or where the date field is coming from, but you can also go to Date::Calc's CPAN page for more help. There are a plethora of other packages that deal with dates, too. This is just one.
This won't handle holidays (and you should really think about those), but it's the minimal implementation in Perl:
use strict;
use warnings;
use POSIX ();
my $date_string = '20100802';
my ( $year, $month, $day ) = unpack 'A4 A2 A2', $date_string;
my $today = POSIX::mktime( 0, 0, 0, $day, $month - 1, $year - 1900 );
( $day, $month, $year, my $wday ) = ( localtime $today )[3..6];
my $day_back
= POSIX::mktime( 0, 0, 0, $day - ( $wday == 1 ? 3 : 1 ), $month, $year )
;
my $day_str = POSIX::strftime( '%Y%m%d', localtime( $day_back ));
Where $time is a specified time value.
With Time::Piece there meanwhile is a base distribution module, that offers date manipulation, comparison etc.
Time::Piece objects e.g. support - and + operators. From the POD:
The following are valid ($t1 and $t2 are Time::Piece objects):
$t1 - $t2; # returns Time::Seconds object
$t1 - 42; # returns Time::Piece object
$t1 + 533; # returns Time::Piece object
Depending on the level of performance that you require, there is a Perl package that is a swiss-army-knife of date manipulation and conversion.
It is called Date::Manip.
From that, you could easily ask for any part of the date (like day-of-week or week-of-year), or compare or subtract dates in a really wide variety of formats.
It is doable with other tools too, I'm sure. I've used Date::Manip for funky stuff like dealing with the date string "last friday" and getting a real value.
There are lots of date/time modules on CPAN, but DateTime is the canonical Perl module for dates and times.
use DateTime;
print DateTime->now()->subtract(3)->ymd('');
If you are ever in doubt about which CPAN module to use, a good first step is to see if Task::Kensho says anything about it.