CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
time stamp store in $utctime needs converting to local time zone

 



TheBigAmbulance
Novice

Nov 10, 2011, 9:17 AM

Post #1 of 2 (497 views)
time stamp store in $utctime needs converting to local time zone Can't Post

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


Code
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.


Code
#!/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:


Code
2011-11-10 10:58:00 PM


What is the best method to convert a variable $utctime (2011-08-09 03:33:11) to the local time zone equivalent using the system time as the source for the conversion (2011-08-08 22:33:11)? The above script doesn't do it, but I think it's a good start.

I have also messed with this script:


Code
use Modern::Perl; 
use DateTime::Format::DateManip;

my $dt = DateTime::Format::DateManip->parse_datetime(
"2011-07-24T17:46:33Z"
);
$dt->set_time_zone( 'UTC' );
$dt->set_time_zone( "America/North_Dakota/Center" );
$dt =~ s/T/ /g;
say $dt;


But I want to send DateTime::Format::DateManip a variable instead of a string.


(This post was edited by TheBigAmbulance on Nov 10, 2011, 9:35 AM)


TheBigAmbulance
Novice

Nov 10, 2011, 9:53 AM

Post #2 of 2 (489 views)
Re: [TheBigAmbulance] time stamp store in $utctime needs converting to local time zone [In reply to] Can't Post

Getting close:

I've worked up this script:


Code
#!/usr/bin/perl -w 
use strict;
use warnings;
use Modern::Perl;
use DateTime::Format::DateManip;

my @times = ("2011-08-09T03:33:11.000Z","2011-07-09T00:27:23Z","2011-07-25T22:24:08-04:00");

my $count = @times;
for (my $i = 0; $i < $count; $i++) {
my $tempf = substr($times[$i] , 0 , 19);
$tempf =~ s/T/ /g;
$tempf = $tempf . "Z";
my $dt = DateTime::Format::DateManip->parse_datetime( "$tempf" );
$dt->set_time_zone( 'UTC' );
$dt->set_time_zone( "America/North_Dakota/Center" );
$dt =~ s/T/ /g;
$times[$i] = $dt;
}
print "@times\n";


Which results in :


Code
2011-08-08 22:33:11 2011-07-08 19:27:23 2011-07-25 17:24:08


So it's close. How do I parse this so that it's not in 24 hour format, and is in 12 hour format (I.E. am/pm)?


(This post was edited by TheBigAmbulance on Nov 10, 2011, 11:02 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives