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.
相关问题
- Correctly parse PDF paragraphs with Python
- $ENV{$variable} in perl
- R: eval(parse()) error message: cannot ope
- Date with SimpleDateFormat in Java
- Display time in local timezone when querying Influ
相关文章
- How to truncate seconds in TSQL?
- How do I get from a type to the TryParse method?
- How to remove seconds from datetime?
- OLS with pandas: datetime index as predictor
- Running a perl script on windows without extension
- Comparing speed of non-matching regexp
- MYSQL: How can I find 'last monday's date&
- Calculate number of working days in a month [dupli
Here's an example, using DateTime and its strptime format module.
If you had wanted to parse localtime, here's how you'd do it :)
That's what I'd do ...
You can also use
Time::ParseDate
andparsedate()
instead ofDate::Parse
andstr2time()
. Note that the de facto standard atm. seems to be DateTime (but you might not want to use OO syntax just to convert a timestamp).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.
Take your pick:
There are a zillion others, no doubt, but they're probably the top contenders.