Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert the systemdate in iso 8601 format in perl

Tags:

iso8601

perl

I want the system date to be converted to ISO 8601 format. code:

my $now = time();
my $tz = strftime("%z", localtime($now));
$tz =~ s/(\d{2})(\d{2})/$1:$2/;
print "Time zone *******-> \"$tz\"\n";
# ISO8601
my $currentDate =  strftime("%Y-%m-%dT%H:%M:%S", localtime($now)) . $tz;
print "Current date *******-> \"$currentDate\"\n";

Current output is:

Time zone *******-> "-04:00"
Current date *******-> "2014-06-03T03:46:07-04:00"

I want the current date to be in format "2014-07-02T10:48:07.124Z", So that I can compute the difference between the two.

like image 960
user3616128 Avatar asked Jun 03 '14 07:06

user3616128


People also ask

How do I change the date format in Perl?

use v5. 10; use POSIX qw(strftime); my $date = '19700101'; my @times; @times[5,4,3] = $date =~ m/\A(\d{4})(\d{2})(\d{2})\z/; $times[5] -= 1900; $times[4] -= 1; # strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1) say strftime( '%d-%b-%Y', @times );

How do I specify the duration in an ISO 8601 format?

Briefly, the ISO 8601 notation consists of a P character, followed by years, months, weeks, and days, followed by a T character, followed by hours, minutes, and seconds with a decimal part, each with a single-letter suffix that indicates the unit. Any zero components may be omitted.

What is Z in ISO 8601 date format?

Z is the zone designator for the zero UTC offset. "09:30 UTC" is therefore represented as "09:30Z" or "T0930Z". "14:45:15 UTC" would be "14:45:15Z" or "T144515Z". The Z suffix in the ISO 8601 time representation is sometimes referred to as "Zulu time" because the same letter is used to designate the Zulu time zone.


3 Answers

You should use gmtime() instead of localtime() to get the broken-down time values in UTC.

use POSIX qw(strftime);
my $now = time();
print strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($now)), "\n";

output:

2014-06-04T10:17:17Z
like image 86
chansen Avatar answered Oct 28 '22 23:10

chansen


Time::Piece and Time::Seconds have been included as a standard part of Perl since 2007.

#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Time::Piece;

my $time = localtime;
say $time->datetime; # Time in ISO8601 format
say $time->tzoffset; # Time zone offset in seconds

# But tzoffset actually returns a Time::Seconds object
say $time->tzoffset->hours; # Time zone offset in hours (for example)
like image 26
Dave Cross Avatar answered Oct 28 '22 23:10

Dave Cross


Perl's DateTime package (on CPAN) can produce ISO8601 dates for you very easily, but, with one caveat.

The string returned by DateTime will be in UTC, but, without a timezone specifier. This SHOULD be fine, because according to the ISO8601 spec, if no timezone is specified, then UTC should be assumed. However, not all parsers obey the spec perfectly. To make my dates more robust I append a Z to the end of the string I get from DateTime, so this is the code I recommend:

use DateTime;
my $now = DateTime->now()->iso8601().'Z';
like image 40
Bart B Avatar answered Oct 28 '22 22:10

Bart B