I've used the localtime function in Perl to get the current date and time but need to parse in existing dates. I have a GMT date in the following format: "20090103 12:00" I'd like to parse it into a date object I can work with and then convert the GMT time/date into my current time zone which is currently Eastern Standard Time. So I'd like to convert "20090103 12:00" to "20090103 7:00" any info on how to do this would be greatly appreciated.
Perl – How to parse dates and convert time zones in Perl
datedatetimeparsingperltimezone
Related Question
- Python – How to get the current time in Python
- Javascript – How to convert a JavaScript date to UTC
- Python – ISO time (ISO 8601) in Python
- Php – How to parse and process HTML/XML in PHP
- C# – DateTime vs DateTimeOffset
- Python – How to make a timezone aware datetime object in Python
- Java – How to parse/format dates with LocalDateTime? (Java 8)
Best Solution
Because the Perl built in date handling interfaces are kind of clunky and you wind up passing around a half dozen variables, the better way is to use either DateTime or Time::Piece. DateTime is the all-singing, all-dancing Perl date object, and you'll probably eventually want to use it, but Time::Piece is simpler and perfectly adequate to this task, has the advantage of shipping with 5.10 and the technique is basically the same for both.
Here's the simple, flexible way using Time::Piece and strptime.
And here's the by-hand way, for contrast.
Since the format is fixed, a regular expression will do just fine, but if the format changes you'll have to tweak the regex.
Then convert it to Unix epoch time (seconds since Jan 1st, 1970)
And then back to your local time.