String to Date/Time

Are there any String to DateTime conversion functions out there ?

Searched in RAA but did not get any … which kind of surprised me: surely,
I cannot be the first one to be asking for it :wink:

If no attempt is made in that direction yet, what would be the best starting
point for such an effort?
I am willing to devote some time and get it out there …

– shanko

Hm, how about a static method in class Time, maybe Time.parse(pattern =
“”) with replaced by the ISO default pattern
(something like “YYYY.MM…”)?

Alternatively you could introduce a class TimeParser that is constructed
with a pattern and can create Time instances from given strings.

Maybe even a combination of both would be good: use TimeParser in the static
method and provide this class for more control, such as dailight saving
switch, a fixed offset to every parsed time etc.

What do others think?

robert

“Shashank Date” sdate@everestkc.net schrieb im Newsbeitrag
news:483e0ce751bc0580f1108dd59df946da@TeraNews…

Are there any String to DateTime conversion functions out there ?

Searched in RAA but did not get any … which kind of surprised me:
surely,
I cannot be the first one to be asking for it :wink:

If no attempt is made in that direction yet, what would be the best
starting

···

point for such an effort?
I am willing to devote some time and get it out there …

– shanko

Have a look at the ParseDate module, which is in the standard lib
distributed with Ruby. It’s documented in the PickAxe book at
http://www.rubycentral.com/book/lib_standard.html#ParseDate.parsedate

Farrel

···

Data Network Architecture Research Lab mailto:flifson@cs.uct.ac.za
Dept. of Computer Science http://people.cs.uct.ac.za/~flifson
University of Cape Town +27-21-650-3127

Hm, how about a static method in class Time, maybe
Time.parse(pattern =“”) with
replaced by the ISO default pattern (something like
“YYYY.MM…”)?

Yes, I will like to consider the ISO patterns even though
currently my “need of the day” does not require it to be.

Alternatively you could introduce a class TimeParser
that is constructed with a pattern and can create Time
instances from given strings.

I am looking at the ParseDate module that comes with the
standard distribution which can handle a subset of ISO-8601
formats among others. But it returns an array which I can
easily convert to a Time object.

On my WinXP Pro (ruby 1.6.8 (2002-12-24) [i586-mswin32]
something like:

···

On Friday, 2 May 2003 22:05:43 +0900, Robert wrote:

#-----------------------------------------------------

require ‘parsedate’

date = date /t
time = time /t
puts date, time if $DEBUG

a = ParseDate::parsedate(date + ’ ’ + time)
p a if $DEBUG

a.pop
t = Time::local(*a)
p t #=> Sat May 03 17:57:00 Central Daylight Time 2003

#-----------------------------------------------------

What do you think ?
– shanko