Is there a generic parser to parse "... ago" strings and turn them into DateTime objects?
Possible sentences are:
So its either a combination of two (Foo and Bar ago) or only one (Foo ago). And it can be singular or plural.
Ruby's DateTime::parse()
cannot handle this, nor can DateTime::strptime()
.
I am hoping for a gem or a snippet somewhere that handles this.
Else I will have to create my own parser, in which case a pointer to how-to-create own DateTime Parsers would be very welcome.
Sidenote: For Future Reference: These are the timestrings generated by Drupals Format Interval
The Chronic gem may be what you're looking for.
require 'chronic'
Chronic.parse "2 days ago"
# => 2011-06-14 14:13:59 -0700
Per @injekt (one of Chronic's maintainers), you can handle "1 year and 1 week ago" like this:
str = "one year and 1 week ago"
chunks = str.split('and')
Chronic.parse(chunks[1], :now => Chronic.parse(chunks[0] + 'ago'))
#=> 2010-06-09 14:29:37 -0700
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With