简体   繁体   中英

Convert String from GMT to EST

I'm trying to convert a string value of a date stamp which is provided in GMT over to the correct time in EST ( or GMT -5). I'm not grasping how to pass the value to the function with GMT and return the EST value.

Source Value looks like this: 2015-01-01 17:05:53

and I would need to return 2015-01-01 12:05:53

Any help appreciated...

Thanks!

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

# Use DateTime::Format::Strptime to parse your date string
use DateTime::Format::Strptime;

my $format  = '%F %T'; # This is the format of your date/time strings
my $from_tz = 'UTC';
my $to_tz   = '-0500';

# Create a parser object that knows the strings it is given are in UTC
my $parser = DateTime::Format::Strptime->new(
    pattern   => $format,
    time_zone => $from_tz,
);

my $in_date = '2015-01-01 17:05:53';

# Use the parser to convert your string to a DateTime object
my $dt = $parser->parse_datetime($in_date);

# Use DateTime's set_time_zone() method to change the time zone
$dt->set_time_zone($to_tz);

# Print the (shifted) date/time string in the same format
say $dt->strftime($format);

use DateTime::Format::Strptime to get a DateTime Object and then set the timezone to "UTC" followed by "-0500" to get your conversion

use DateTime::Format::Strptime;

my $parser = DateTime::Format::Strptime->new(pattern => "%Y-%m-%d %H:%M:%S");
$datetime=$parse->parse_datetime("2015-01-01 17:05:53");

$datetime->set_time_zone("UTC");
$datetime->set_time_zone("-0500");

print $datetime->strftime("%Y-%m-%d %H:%M:%S");

For more information, refer to CPAN docs:

http://search.cpan.org/~drolsky/DateTime-Format-Strptime-1.56/lib/DateTime/Format/Strptime.pm

http://search.cpan.org/~drolsky/DateTime-1.18/lib/DateTime.pm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM