问题
I have a date in the format: 05/26/2013 06:08:00
The timezone of above date is GMT -7
how can i change the above to GMT date and time and in format: 26 May 2013 13:08:00 GMT
Note: I cannot install any perl module. I know that i can do this easily via DateTime but i cannot install it.
Thanks.
回答1:
You can use Time::Piece which should be included with your Perl installation.
use Time::Piece;
my $datetime = '05/26/2013 06:08:00';
$datetime .= '-0700'; # attach the timezone offset
my $dt = Time::Piece->strptime($datetime, '%m/%d/%Y %H:%M:%S %z');
print $dt->strftime('%d %b %Y %H:%M:%S');
回答2:
Assuming your local time is already set to GMT-7, simply:
use POSIX 'mktime', 'strftime';
my $datetime = '05/26/2013 06:08:00';
my ($month,$day,$year,$hour,$min,$sec) = $datetime =~ m{^([0-9]+)/([0-9]+)/([0-9]+) ([0-9]+):([0-9]+):([0-9]+)\z}
or die "invalid datetime $datetime\n";
my $formatted_time = strftime '%e %B %Y %T %Z', gmtime mktime $sec,$min,$hour,$day,$month-1,$year-1900,0,0,0;
You may want %d
instead of %e
and/or %b
instead of %B
; see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.html and verify that the desired format specifiers are supported on your system.
来源:https://stackoverflow.com/questions/17037539/perl-convert-date-timezone-without-datetime-module