How can I format a timestamp in Perl? - perl

I would like to get this timestamps formatting:
01/13/2010 20:42:03 - -
Where it's always 2 digits for the number except for the year, where it's 4 digits. And it's based on a 24-hour clock.
How can I do this in Perl? I prefer native functions.

POSIX provides strftime:
$ perl -MPOSIX -we 'print POSIX::strftime("%m/%d/%Y %H:%M:%S\n", localtime)'
01/27/2010 14:02:34
You might be tempted to write something like:
my ($sec, $min, $hr, $day, $mon, $year) = localtime;
printf("%02d/%02d/%04d %02d:%02d:%02d\n",
$day, $mon + 1, 1900 + $year, $hr, $min, $sec);
as a means of avoiding POSIX. Don't! AFAIK, POSIX.pm has been in the core since 1996 or 1997.

This is a subroutine that I use. It allows you to get the date time stamp in whichever format you prefer.
#! /usr/bin/perl
use strict;
use warnings;
use POSIX;
use POSIX qw(strftime);
sub getTime
{
my $format = $_[0] || '%Y%m%d %I:%M:%S %p'; #default format: 20160801 10:48:03 AM
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(time);
return strftime($format, $sec, $min, $hour, $wday, $mon, $year, $mday);
}
# Usage examples
print getTime()."\n"; #20160801 10:48:03 AM
print getTime('%I:%M:%S %p')."\n"; #10:48:03 AM
print getTime('%A, %B %d, %Y')."\n"; #Monday, August 01, 2016
print getTime('%Y%m%d %H:%M:%S')."\n"; #20160801 10:48:03

Try the Date::Format formatting subroutines from CPAN.

Related

Alternative date command option in AIX

I want to convert specific date to timestamp in AIX. Following command is working in GNU/LINUX flavor.
Can someone please help me to get it done in AIX as well?
Command working on GNU/LINUX:
Command -> date -d"Nov 14 02:31" "+%s"
Output -> 1542162660
You might do something like this with Perl, if you have POSIX::strptime
Example program (totimestamp.pl):
#!/usr/bin/perl
use strict;
use POSIX ("tzset", "mktime");
use POSIX::strptime;
POSIX::tzset ();
my $ARGC= scalar (#ARGV);
my $tstamp;
if ($ARGC < 1) {
$tstamp= time ();
} else {
my $tstr= $ARGV[0];
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);
($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
POSIX::strptime($tstr, "%b %d %H:%M:%S %Y");
$tstamp= POSIX::mktime ($sec, $min, $hour, $mday, $mon, $year);
}
printf ("%d\n", $tstamp);
Usage:
perl ./totimestamp.pl "Nov 16 14:40:00 2018"
1542375600

Perl Incorrect day generated by localtime

Today I find following perl script running incorrect.
Current actual datetime is 20140814 13:19
But it returns: 2014-7-14-13-19-15 (the month value 7 is 1 less than actual value 8)
MY OS: win7
sub GetFileNameDate {
my ($sec,$min,$hour,$day,$month,$yr19,#rest) = localtime;
return sprintf "%s-%s-%s-%02d-%02d-%02d", ($yr19 + 1900), $month, $day, $hour, $min, $sec;
}
It is not incorrect it is like the month values starts from 0
sub GetFileNameDate {
my ($sec,$min,$hour,$day,$month,$yr19,#rest) = localtime;
return sprintf "%s-%s-%s-%02d-%02d-%02d", ($yr19 + 1900), ($month +1), $day, $hour, $min, $sec;
}
$month is the month itself, in the range 0..11 with 0 indicating January and 11 indicating December.
You can check the manual
That is correct according to the docs for localtime:
$mday is the day of the month and $mon the month in the range 0..11, with 0 indicating January and 11 indicating December. This makes it easy to get a month name from a list.
However, to simplify your goal, I'd recommend using Time::Piece:
use strict;
use warnings;
use Time::Piece;
sub GetFileNameDate {
return localtime->strftime("%Y-%m-%d-%H-%M-%S");
}
print GetFileNameDate(), "\n";
Outputs:
2014-08-13-23-42-56
Did you perhaps want POSIX::strftime?
use POSIX qw( strftime );
print strftime "%Y-%m-%d-%H-%M-%S", localtime;

Perl - How to convert a date?

How to convert date format YYYY-MM-DDTHH:MM:SSZ to YYYY-MM-DD HH:MM + 8 hours?
For example:
Input: 2011-07-07T18:05:45Z
Output: 2011-07-08 02:05
Let's start with Rahul's snippet, and add in the date math and output formatting...
use DateTime;
use DateTime::Format::ISO8601;
use DateTime::Format::Strptime;
my $string = '2011-07-07T18:05:45Z';
my $dt = DateTime::Format::ISO8601->parse_datetime( $string );
die "Impossible time" unless $dt;
my $formatter = new DateTime::Format::Strptime(pattern => '%Y-%m-%d %T');
$dt->add( hours => 8 )->set_formatter($formatter);
print "$dt\n";
I've added the use of DateTime::Format::Strptime, in order to specify the desired output format.
Then I've added three more lines:
First I create a formatter, and feed it the output pattern I desire.
Next I add eight hours to the original date, and I assign the output
formatter by chaining the set_formatter() call to the add() call.
Then I print it.
Are you using the DateTime modules?
Specifically, here's a link to DateTime::Format::ISO8601 that reads/writes ISO 8601 format you mentioned as your input.
If you don't have DateTime, you surely have Time::Piece:
use strict;
use warnings;
use Time::Piece;
use Time::Seconds qw(ONE_HOUR);
my $str = '2011-07-07T18:05:45Z';
my $t = Time::Piece->strptime($str, "%Y-%m-%dT%TZ");
$t += 8 * ONE_HOUR;
print $t->strftime("%Y-%m-%d %H:%M"),"\n";
Taken From
How can I validate a "yyyy-MM-dd'T'HH:mm:ssZ" date/timestamp in UTC with Perl?
use DateTime;
use DateTime::Format::ISO8601;
my $string = '2010-02-28T15:21:33Z';
my $dt = DateTime::Format::ISO8601->parse_datetime( $string ); die "Impossible time" unless $dt;
It doesn't work, result is 2010-02-28T15:21:33
Then, do it the hard way...
use Time::Local
use warnings;
use strict;
$time = '2010-02-28T15:21:33Z';
my ($year, month, day) = split (/-/, $time)
$year -= 1900; #Year is an offset of 1900
$month -= 1; #Months are 0 - 11
#Now split the time off of the day (DDTHH:MM:SS)
$day = substr($day, 0, 2);
time = substr($day, 3)
#Now split the time
(my $hour, $minute, $second) = split(/:/, $time);
$second =~ s/Z$//; #Remove Z
my $time_converted = timelocal($second, $minute, $hour, $day, $month, $year);
#Now you have the time, Add eight hours
my $hours_in_seconds = 8 * 60 * 60;
$time_converted += $hours_in_seconds;
# Almost done: Convert time back into the correct array:
($second, $minute, $hour, $day, $month, $year) = localtime($time_converted);
$year += 1900;
$month += 1;
# Now, reformat:
my $formatted_time = sprint (%04d-%02d-%02d %02d:%02d),
$year, $month, $day, $hour, $minute;

How do I get yesterday's date using localtime?

How do I tweak this to get yesterday's date using localtime?
use strict;
sub spGetCurrentDateTime;
print spGetCurrentDateTime;
sub spGetCurrentDateTime {
my ($sec, $min, $hour, $mday, $mon, $year) = localtime();
my #abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my $currentDateTime = sprintf "%s %02d %4d", $abbr[$mon], $mday, $year+1900; #Returns => 'Aug 17 2010'
return $currentDateTime;
}
~
use DateTime qw();
DateTime->now->subtract(days => 1);
The expression on the second line returns a DateTime object.
As tempting as it is to just subtract a day's worth of seconds from the current time, there are times when this will yield the wrong answer (leap seconds, DST, and possibly others). I find it easier to just let strftime (available in the Perl 5 core module POSIX) take care of all of that for me.
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
use POSIX qw/strftime/;
#2010-03-15 02:00:00
my ($s, $min, $h, $d, $m, $y) = (0, 0, 0, 15, 2, 110);
my $time = timelocal $s, $min, $h, $d, $m, $y;
my $today = strftime "%Y-%m-%d %T", localtime $time;
my $yesterday = strftime "%Y-%m-%d %T", $s, $min, $h, $d - 1, $m, $y;
my $oops = strftime "%Y-%m-%d %T", localtime $time - 24*60*60;
print "$today -> $yesterday -> $oops\n";
The DST problem can be worked around by taking 3600s from midday today instead of the current time:
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
sub spGetYesterdaysDate;
print spGetYesterdaysDate;
sub spGetYesterdaysDate {
my ($sec, $min, $hour, $mday, $mon, $year) = localtime();
my $yesterday_midday=timelocal(0,0,12,$mday,$mon,$year) - 24*60*60;
($sec, $min, $hour, $mday, $mon, $year) = localtime($yesterday_midday);
my #abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my $YesterdaysDate = sprintf "%s %02d %4d", $abbr[$mon], $mday, $year+1900;
return $YesterdaysDate;
}
In light of the "unspecified" documented behaviour of the strftime solution suggested by Chas, this approach might be better if you're not able to test for expected-but-not-guaranteed results across multiple platforms.
use Time::Piece.
use strict;
use warnings;
use 5.010;
# These are core modules in Perl 5.10 and newer
use Time::Piece;
use Time::Seconds;
my $yesterday = localtime() - ONE_DAY;
say $yesterday->strftime('%b %d %Y');
Note that this can go wrong in certain borderline cases, such as the start of daylight saving time.
The following version does behave correct in such cases:
use strict;
use warnings;
use 5.010;
# These are core modules in Perl 5.10 and newer
use Time::Piece;
use Time::Seconds;
my $now = localtime();
my $yesterday = $now - ONE_HOUR*($now->hour + 12);
say $yesterday->strftime('%b %d %Y');
Alternatively, you can use the DateTime module as described in a different answer. That is not a core module, though.
Solution suggested by most users is wrong!
localtime(time() - 24*60*60)
The worst thing you can do is to assume that 1 day = 86400 seconds.
Example: Timezone is America/New_York, date is Mon Apr 3 00:30:00 2006
timelocal gives us 1144038600
localtime(1144038600 - 86400) = Sat Apr 1 23:30:00 EST 2006
oops!
The right and the only solution is to let system function normalize values
$prev_day = timelocal(0, 0, 0, $mday-1, $mon, $year);
Or let datetime frameworks (DateTime, Class::Date, etc) do the same.
That's it.
localtime(time() - 24*60*60)
my $yesterday = time();
$yesterday = $yesterday - (24*60*60);
24 as 24 hours, 60 as 60 minutes in hour and 60 as 60 seconds in minute
time() will return actual timestamp, and 246060 will remove seconds for exactly one day
After this simply do:
localtime($yesterday);
This is how I do it.
#!/usr/bin/perl
use POSIX qw(strftime);
$epoc = time();
$epoc = $epoc - 24 * 60 * 60;
$datestring = strftime "%F", localtime($epoc);
print "Yesterday's date is $datestring \n";

How do I convert local time to a Unix timestamp in Perl?

For example: from date: 10/02/2010
How do I convert an equal timestamp for 10/02/2010 00:00:00 in Perl?
I can't use local time or time .. is there another way to achieve this?
You can use the Time::Local core module:
use Time::Local 'timelocal';
my ($d, $m, $y) = split '/', '10/02/2010';
my $time = timelocal(0, 0, 0, $d, $m-1, $y);
Note that the month argument for timelocal() is in the range 0..11.
Without localtime():
use Time::Local;
$time = timelocal($sec, $min, $hour, $mday, $mon, $year);
(See perldoc.)
A standard way would be something like:
use POSIX;
use strict;
use warnings;
my $sec = 0;
my $min = 0;
my $hour = 0;
my $day = 10;
my $mon = 2 - 1;
my $year = 2010 - 1900;
my $wday = 0;
my $yday = 0;
my $unixtime = mktime ($sec, $min, $hour, $day, $mon, $year, $wday, $yday);
print "$unixtime\n";
my $readable_time = localtime($unixtime);
print "$readable_time\n"
(From Converting Unix time and readable time with Perl)
You could use Date::Parse:
use Date::Parse;
print str2time('10/02/2010 00:00:00');
On my machine this prints 1285970400, which corresponds to October 2nd, 2010 (I live in +1 GMT with +1 Wintertime.)
I think you want the built-in module Time::Local.
The DateTime module should be helpful here. In particular, I believe the DateTime::Format::Natural module can parse a user-supplied date string. From there, you have a DateTime object and can print it out or transform it as you like.
Depending on where your initial date is coming from you might be able to parse it using
Date::Manip
and calling
ParseDate("10/02/2010")
You can then take that output and convert it into whatever format you wish.