user3839613
user3839613

Reputation: 109

convert localtime to utc time in perl

I calculated time that is 10 minutes before current time. i need to convert it into UTC time using perl script. Please help. I have used this code :-

my $dt = time();
$dt = $dt - 10 * 60;   # 10 minutes before of current date.

I want time in this format :-

2014-08-14T05:52:16.

Upvotes: 1

Views: 5742

Answers (2)

Dave Cross
Dave Cross

Reputation: 69314

Time::Piece gives a slightly higher level interface to things like strftime.

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

use Time::Piece;

my $now = gmtime;
$now -= 10*60;
say $now->strftime('%Y-%m-%dT%H:%M:%S');

Upvotes: 1

Rory Hunter
Rory Hunter

Reputation: 3470

You should consider using strftime, available via the POSIX module. See:

perldoc POSIX

for information on the module, and

man strftime

for information on the function. You could use it as e.g.

#!/usr/bin/perl -w

use strict;
use warnings;

use POSIX qw(strftime);

my $dt = time();
$dt -= 10 * 60;

print "Datetime: ", strftime('%Y-%m-%dT%H:%M:%S.', gmtime($dt)), "\n";

Upvotes: 4

Related Questions