Is there a generic parser to parse “… ago” strings and turn them into DateTime objects?
Possible sentences are:
- 1 year|@count years (and 1 week|@count weeks) ago
- 1 week|@count weeks (and 1 day|@count days) ago
- 1 day|@count days (and 1 hour|@count hours) ago
- 1 hour|@count hours ago (and 1 min|@count min) ago
- 1 min|@count min ago (and 1 sec|@count sec) ago
- 1 sec|@count sec ago
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.
Per @injekt (one of Chronic’s maintainers), you can handle “1 year and 1 week ago” like this: