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.
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';