TheBigAmbulance has asked for the wisdom of the Perl Monks concerning the following question:

I have a set of time stamps that are set in UTC. Here is a small example:

2011-08-09 03:33:11 2011-07-09 00:27:23 2011-07-25 22:24:08

I need to convert these to my systems local time. So I need to first acquire the local time zone, then convert the above values from UTC to whatever is revealed from finding out the system time zone.

I have tried using DateTime. I have a simple script written that does "stuff", but not what I need.

#!/usr/bin/perl -w use strict; use warnings; use DateTime; my $dt = DateTime->now(time_zone => 'America/North_Dakota/Center'); #$dt->set_time_zone('Europe/Guernsey'); my $time = $dt->strftime('%F %T'); print "$time\n";

What this this does is prints the local time:

2011-11-10 10:58:00

What is the best method to convert $utctime (2011-08-09 03:33:11) to a local time zone based on what time zone if found out by acquiring the system timezone, converting it to (2011-08-08 22:33:11)? This should be the safest way to avoid the whole daylight savings time issue.? The above script doesn't do it, but I think it's a good start.

Replies are listed 'Best First'.
Re: time stamp store in $utctime needs converting to local time zone
by martell (Hermit) on Nov 10, 2011 at 19:56 UTC

    something like this?

    use DateTime; my $input = '2011-08-09 03:33:11'; my ($year, $month, $day, $hour, $minute, $second) = split (/[\s-:]/, $ +input); $dt = DateTime->new( year => $year, month => $month, day => $day, hour => $hour, minute => $minute, second => $second, time_zone => 'UTC', ); $dt->set_time_zone('America/North_Dakota/Center'); print $dt->strftime('%F %T'), "\n";

    Kind Regards

Re: time stamp store in $utctime needs converting to local time zone
by jethro (Monsignor) on Nov 10, 2011 at 18:21 UTC

    If you want to convert a UTC time to a local time (and I assume with local time in your example you mean North Dakota) you have to create the time in UTC and then change to local timezone. I tried it with your script with switched time zones and it seemed to work as expected.

    Now DateTime documentation explicitely says it doesn't parse data. Since you have fixed date strings that are easy to parse I would suggest using a simple regex to parse your dates and use DateTime->new() with that data

    PS: Are you sure Guernsey has always UTC time, with or without Daylight Saving Time?