Convert system date in iso 8601 format to perl

I want the system date to be converted to the 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:

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

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

+4
source share
3 answers

Perl DateTimepackage ( in CPAN ) can give you ISO8601 dates very easily, but with one caveat.

, DateTime, UTC, . , , ISO8601, , UTC. , . , Z , DateTime, , :

use DateTime;
my $now = DateTime->now()->iso8601().'Z';
+11

:: . :: Perl 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)
+4

gmtime() localtime(), UTC.

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

:

2014-06-04T10:17:17Z
+1

Source: https://habr.com/ru/post/1543022/


All Articles