Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Convert GMT date and time to local time

by Anonymous Monk
on Sep 13, 2017 at 09:14 UTC ( #1199280=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I would like to find out the local time (wall clock time, i.e. accounting for daylight saving time periods) starting from a GMT time. I made several attempts with Date::Manip but I did not find a way...
use strict; use Date::Manip; my $dt = new Date::Manip::Date; my $datestr = "2016-07-30-15:30:00 GMT"; $dt->parse($datestr); # Print date in local time, if I knew how to do that...

Replies are listed 'Best First'.
Re: Convert GMT date and time to local time
by haukex (Abbot) on Sep 13, 2017 at 09:33 UTC

    Does it have to be Date::Manip? I prefer DateTime, it has excellent time zone handling and everything else regarding date/time handling, also via add-ons like for example DateTime::Format::Strptime.

    use warnings; use strict; use DateTime; use DateTime::Format::Strptime; my $strp = DateTime::Format::Strptime->new(on_error=>'croak', pattern => '%Y-%m-%d-%H:%M:%S %Z'); my $datestr = "2016-07-30-15:30:00 GMT"; my $dt = $strp->parse_datetime($datestr); print $dt->strftime('%Y-%m-%d-%H:%M:%S %Z'), "\n"; $dt->set_time_zone('America/Los_Angeles'); print $dt->strftime('%Y-%m-%d-%H:%M:%S %Z'), "\n"; __END__ 2016-07-30-15:30:00 UTC 2016-07-30-08:30:00 PDT
      Thanks! It does the job as I needed. The Date::Manip documentation was a nightmare for me.
Re: Convert GMT date and time to local time (UPDATED)
by thanos1983 (Vicar) on Sep 13, 2017 at 15:00 UTC

    Hello Anonymous Monk,

    Fellow Monk haukex, has already answered your question but since you mentioned the module Date::Manip here are some examples:

    #!/usr/bin/perl use strict; use warnings; use Date::Manip; use feature 'say'; say "PARSING A DATE:"; my $date; say $date = ParseDate("today"); say $date = ParseDate("1st Thursday in June 1992"); say $date = ParseDate("05/10/93"); say $date = ParseDate("12:30 Dec 12th 1880"); say $date = ParseDate("8:00pm December tenth"); say ""; say "PARSING AN AMOUNT OF TIME:"; my $delta; say $delta = ParseDateDelta("in 12 hours"); say $delta = ParseDateDelta("-1:30:0"); say $delta = ParseDateDelta("4 business days later"); __END__ $ perl PARSING A DATE: 2017091300:00:00 1992060400:00:00 1993051000:00:00 1880121212:30:00 2017121020:00:00 PARSING AN AMOUNT OF TIME: 0:0:0:0:12:0:0 0:0:0:0:-1:30:0 0:0:0:4:0:0:0

    You can find plenty of examples in the Date::Manip::Examples.

    Update: Very nice explanation with examples of the module Date::Manip - date manipulation routines.

    Update2: I think this is what you are looking for:

    #!/usr/bin/perl use strict; use warnings; use Date::Manip; use feature 'say'; my $tz = new Date::Manip::TZ; my $dateLocal = ParseDate('now'); say $dateLocal; # From timeZone To timeZone my $dateTimeZone = Date_ConvTZ($dateLocal,"GMT","CST"); my $unixLocal = UnixDate($dateLocal,'%Y-%m-%d-%H-%M-%S'); say $unixLocal; my $unixTimeZone = UnixDate($dateTimeZone,'%Y-%m-%d-%H-%M-%S'); say $unixTimeZone; __END__ $ perl 2017091400:41:59 2017-09-14-00-41-59 2017-09-14-08-41-59

    Relevant question convert GMT to other time zone using Date::Manip, and more information Date::Manip - Date manipulation routines.

    Time zone abbreviations Time Zone Abbreviations Worldwide List.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Convert GMT date and time to local time
by Anonymous Monk on Sep 13, 2017 at 09:22 UTC
    I forgot to say that I would like to convert it to the local time of a time zone I can specify, for example local time in Seattle.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1199280]
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2018-02-24 12:40 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (310 votes). Check out past polls.