0

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!

Michael R
  • 1
  • 2
  • possible duplicate of [How can I parse dates and convert time zones in Perl?](http://stackoverflow.com/questions/411740/how-can-i-parse-dates-and-convert-time-zones-in-perl) – Chankey Pathak Jan 27 '15 at 06:57

2 Answers2

1
#!/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);
Dave Cross
  • 68,119
  • 3
  • 51
  • 97
-1

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

rurouni88
  • 1,165
  • 5
  • 10