I have a time of format 2013-04-29 08:17:58 and I need to convert it into seconds.
In UNIX, we can use date +%s. Is there anyway to do it in Solaris?
Use Time::Piece. It has been part of the standard Perl 5 distribution since version 9.5 and shouldn't need installing.
use strict;
use warnings;
use 5.010;
use Time::Piece;
my $t = '2013-04-29 08:17:58';
$t = Time::Piece->strptime($t, '%Y-%m-%d %H:%M:%S');
say $t->epoch;
output
1367223478
With a little more effort, you can do this with your horribly outdated version of Perl.
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
my $str = '2013-04-29 08:17:58';
my #dt = split /[- :]/, $str;
$dt[0] -= 1900;
$dt[1] -= 1;
print timelocal reverse #dt;
Time::Local has been included with Perl since the first release of Perl 5 (in 1994).
But please do what you can to get your ancient version of Perl updated.
Update: Getting a few downvotes on this. But no-one has bothered to explain why.
Related
Hi I am trying to store the date in a variable $date. I will then use Excel::Writer::XLSX to print the date into a cell. I am using perl 5.8. I know a lot of the modules used for getting the date such as TimePiece were installed in later versions of perl.
use POSIX qw( strftime );
strftime('%m/%d/%Y', localtime)
You are correct that Time::Piece is only core since Perl 5.10. But you can install it from CPAN. You could then use its strftime method:
use strict;
use warnings;
use Time::Piece;
my $date = localtime->strftime('%m/%d/%Y');
Without it, you can use the built-in localtime function, which also has a nicer wrapper Time::localtime (even in 5.8). You just have to be careful because the values returned by POSIX localtime aren't exactly what you'd expect.
use strict;
use warnings;
use Time::localtime;
my $now = localtime;
my $date = sprintf '%02d/%02d/%04d', $now->mon + 1, $now->mday, $now->year + 1900;
How do I convert "11am" and "10pm" into "11:00:00" and "22:00:00"? Is there a simple way in perl to convert this?
Time::Piece has been a standard part of Perl since Perl 5.10 in 2007.
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use Time::Piece;
for (qw[11am 10pm]) {
my $time = Time::Piece->strptime($_, '%H%p');
say $time->strftime('%H:%M:%S');
}
Time::Piece's documentation claims that the definition of %p is based on your locale. So, according to the documentation, Time::Piece's %p can't reliably be used to handle am and pm, so you shouldn't use it.
On the other hand, Time::Piece behaves differently than documented, and %p will reliably handle am and pm, so it could technically be used to solve your problem despite documentation to the contrary.
I'd personally avoid that giant mess (and all of Time::Piece's other problems) and use the following lighter, simpler and clearer code:
my ($h, $ampm) = /^([0-9]+)(am|pm)\z/;
$h = 0 if $h == 12;
$h += 12 if $ampm eq 'pm';
my $hms = sprintf("%d:00:00", $h); # or %02d if you want 00:00:00
I need to get the ISO 8601 week number of todays date in perl.
What is wrong woth the following code?
#! /usr/bin/perl
use strict;
use warnings;
use Time::Local;
use POSIX qw(strftime);
my $weekNumber = POSIX::strftime("%V", localtime time);
print $weekNumber, "\n";
The output I get is simply %V and my expected result (for epoch 1407769639) is 33.
FYI using POSIX::strftime("%W", localtime time); results in 32.
It is best to use Time::Piece, which has been a core module since version 10 of Perl 5, and so shouldn't need installing unless you are running a very old version.
Time::Piece replaces the core localtime function with one that returns a Time::Piece object, so the code would look like this
use strict;
use warnings;
use Time::Piece;
print localtime->week, "\n";
output
33
The POSIX functions are thin layers over your C library. What you get is based on your C library's behaviour. You get %V because your C library's strftime doesn't recognize %V.
This question already has an answer here:
Get time in milliseconds without an installing an extra package?
(1 answer)
Closed 8 years ago.
I need to get the time in the format "20130808 12:12:12.123" i.e., "yyyymmdd hour:min:sec.msec".
I tried
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
$mon++;
if ($mon<10){$mon="0$mon"}
if ($mday<10){$mday="0$mday"}
if ($hour<10){$hour="0$hour"}
if ($min<10){$min="0$min"}
if ($sec<10){$sec="0$sec"} but this doesn't provide the `msec` as a part of time.
How can i do that ?
Here's a complete script. As proposed before, it is using Time::HiRes::time for microsecond support, and it's also using POSIX::strftime for easier formatting. Unfortunately strftime cannot deal with microseconds, so this has to be added manually.
use Time::HiRes qw(time);
use POSIX qw(strftime);
my $t = time;
my $date = strftime "%Y%m%d %H:%M:%S", localtime $t;
$date .= sprintf ".%03d", ($t-int($t))*1000; # without rounding
print $date, "\n";
If you don't mind to use a CPAN module, then I would propose the excellent Time::Moment module:
use Time::Moment;
print Time::Moment->now->strftime("%Y%m%d %T%3f"), "\n";
And if it may be formatted as an ISO8601 date including a time zone offset and microseconds instead of milliseconds, then it's simply:
print Time::Moment->now->to_string, "\n";
use Time::HiRes
Looking at this briefly, it can provide milliseconds since epoch fairly
easily but didn't seem to extend localtime(), so there's probably a bit of work
involved in using it in a full calendar context.
Here's a working example:
use strict;
use warnings;
use Time::Format qw/%time/;
use Time::HiRes qw/gettimeofday/;
my $time = gettimeofday; # Returns ssssssssss.uuuuuu in scalar context
print qq|$time{'yyyymmdd hh:mm:ss.mmm', $time}\n|;
What's the most efficient way to get the current date in ISO format (e.g. "2010-10-06") using Perl?
Most efficient for you or the computer?
For you:
use POSIX qw/strftime/;
print strftime("%Y-%m-%d", localtime), "\n";
For the Computer:
my #t = localtime;
$t[5] += 1900;
$t[4]++;
printf "%04d-%02d-%02d", #t[5,4,3];
Or you can use the DateTime module which seems to be the de facto standard date handling module these days. Need to install it from CPAN.
use DateTime;
my $now = DateTime->now->ymd;
There are plenty of modules in the Date:: namespace, many of which can help you do what you need. Or, you can just roll your own:
my ($day, $mon, $year) = (localtime)[3..5];
printf "%04d-%02d-%02d\n", 1900+$year, 1+$mon, $day;
Resources
Date:: namespace on The CPAN: http://search.cpan.org/search?query=Date%3A%3A&mode=module
You can use the Time::Piece module (bundled with Perl 5.10 and up, or you can download from CPAN), as follows:
use strict;
use warnings;
use Time::Piece;
my $today = localtime->ymd(); # Local time zone
my $todayUtc = gmtime->ymd(); # UTC
This doesn't create any temporary variables:
printf "%d-%02d-%02d", map { $$_[5]+1900, $$_[4]+1, $$_[3] } [localtime];