Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
537 views
in Technique[技术] by (71.8m points)

perl different time format output

I am trying to convert to some date format from the below hash output. I am in need of convert many hash output to this date format below. I am using this below code now. it seems this code is not proper way I used.

Please share your thoughts to get that time format as simple way from the each hash output

my $status_update_time = "$row->{'update_time'}";
$status_update_time =~ m/(d{4})-(d{2})-(d{2}) (d{2}):(d{2}):(d{2})$/;
my ($year, $month, $date,$hours,$minute,$second) = ($1, $2, $3, $4, $5, $6);
my $date_time = "$1-$2-$3T$4:$5:$6TZ"; #2015-08-11T04:31:41Z# expecting this time output

my $next_check = "$row->{'next_check'}";
$next_check =~ m/(d{4})-(d{2})-(d{2}) (d{2}):(d{2}):(d{2})$/;
my ($year, $month, $date,$hours,$minute,$second) = ($1, $2, $3, $4, $5, $6);
my $next_check_time = "$1-$2-$3T$4:$5:$6Z"; #2015-08-11T04:31:41Z# expecting this time output

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use Time::Piece. It's a standard part of the Perl distribution. Use strptime (string parse time) to parse your string into a Time::Piece object. Then use strftime (string format time) to display your Time::Piece object in whatever format you want.

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

use Time::Piece;

my $in_format  = '%Y-%m-%d %H:%M:%S';
my $out_format = '%Y-%m-%dT%H:%M:%SZ';

my $in_date = '2015-08-18 08:51:00';

my $date = Time::Piece->strptime($in_date, $in_format);

say $date->strftime($out_format);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...