TASdvlper has asked for the wisdom of the Perl Monks concerning the following question:
I've read some threads on how to convert to epoch time, but I can't seem to find the exact answer I'm looking for. I would like to convert a date/time in the following format '2005-03-28 12:00:00' (I'm looking at dates/times in the past, not just the current date/time) and convert it to epoch time.
I know this should be a easy one for most.
Thanks all !!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Converting to epoch time.
by Limbic~Region (Chancellor) on Mar 30, 2005 at 15:23 UTC | |
Take a look at Time::Local. If you are sure your data is rigidly formatted as you describe, a simple regex and that module is all you need.
Cheers - L~R | [reply] [d/l] |
Re: Converting to epoch time.
by cazz (Pilgrim) on Mar 30, 2005 at 15:23 UTC | |
| [reply] |
Re: Converting to epoch time.
by ikegami (Patriarch) on Mar 30, 2005 at 15:24 UTC | |
| [reply] |
by cowboy (Friar) on Mar 30, 2005 at 16:21 UTC | |
| [reply] |
Re: Converting to epoch time.
by duct_tape (Hermit) on Mar 30, 2005 at 23:34 UTC | |
What others have said about splitting up the string with a regex and using Time::Local will work great if all you need is to convert the date to epoch. But if you need to do anything else with the date, then you may want to look at the DateTime modules. Note: Since your date does not include the timezone, you may need to specify that to the DateTime object if you need it to be in a certain timezone. By default it is parsed into UTC. Here is an example using DateTime::Format::Strptime with the specified format. This is a good module to use if you know the format for the date. Check the manpage for strftime for information about the format.
And here it is using DateTime::Format::HTTP in case your date can be in many different formats. Check the documentation for the list of the many formats that it knows how to parse.
Hope that this is helpful. | [reply] [d/l] [select] |
Re: Converting to epoch time.
by rbi (Monk) on Mar 30, 2005 at 15:39 UTC | |
if you need the epoch time, for example to compute distances in time you can try (untested): Hope this helps. | [reply] [d/l] [select] |
Re: Converting to epoch time.
by bageler (Hermit) on Mar 30, 2005 at 19:35 UTC | |
$time = Date::Calc::Mktime($year,$month,$day, $hour,$min,$sec); | [reply] [d/l] |
Re: Converting to epoch time.
by trammell (Priest) on Mar 30, 2005 at 18:30 UTC | |
| [reply] |
Re: Converting to epoch time.
by VladSu (Acolyte) on Mar 30, 2005 at 23:30 UTC | |
I wrote some functions to manipulate the date I hope it will useful for you too.
Vlad. | [reply] [d/l] |
by jeanluca (Deacon) on Feb 11, 2006 at 18:26 UTC | |
Here it is:
Cheers Luca | [reply] [d/l] |
Is that a datetime from a DB?
by TomDLux (Vicar) on Mar 31, 2005 at 15:39 UTC | |
To me, that looks like a datetime string out of a database .... at least that's the way it looks in my work, with Informix. I would start by investigating your database to see if it can return the data as an epoch time. After all, it isn't stored as 'y-m-d h:m:s', but in a more condensed format. Tom-- | [reply] |
Re: Converting to epoch time.
by Thelonious (Scribe) on Mar 31, 2005 at 18:46 UTC | |
There are many other handy shortcut methods as well... hth | [reply] [d/l] |