Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

parsing times like "5 minutes ago"?

0 views
Skip to first unread message

m...@pixar.com

unread,
Jul 6, 2009, 8:21:32 PM7/6/09
to
I'm looking for something like Tcl's [clock scan] command which parses
human-readable time strings such as:

% clock scan "5 minutes ago"
1246925569
% clock scan "tomorrow 12:00"
1246993200
% clock scan "today + 1 fortnight"
1248135628

Does any such package exist for Python?

Many TIA!
Mark

--
Mark Harrison
Pixar Animation Studios

Carl Banks

unread,
Jul 6, 2009, 8:43:09 PM7/6/09
to
On Jul 6, 5:21 pm, m...@pixar.com wrote:
> I'm looking for something like Tcl's [clock scan] command which parses
> human-readable time strings such as:
>
>     % clock scan "5 minutes ago"
>     1246925569
>     % clock scan "tomorrow 12:00"
>     1246993200
>     % clock scan "today + 1 fortnight"
>     1248135628
>
> Does any such package exist for Python?

If you're on a machine with GNU datethe simplest solution is to use it
to parse the string.

Q&D:

def clock_scan(datestring):
stdout,stderr = subprocess.Popen(
['date','+%s','--date=%s' % datestring ],
stdout=subprocess.PIPE).communicate()
return float(stdout)


clock_scan('1 hour ago')
clock_scan('next week')


Carl Banks

selasley

unread,
Jul 6, 2009, 11:48:36 PM7/6/09
to

The timelib module might do what you want
http://pypi.python.org/pypi/timelib/0.2

Paul McGuire

unread,
Jul 7, 2009, 2:02:44 AM7/7/09
to

I've been dabbling with such a parser with pyparsing - here is my
progress so far: http://pyparsing.wikispaces.com/UnderDevelopment

It parses these test cases:

today
tomorrow
yesterday
in a couple of days
a couple of days from now
a couple of days from today
in a day
3 days ago
3 days from now
a day ago
now
10 minutes ago
10 minutes from now
in 10 minutes
in a minute
in a couple of minutes
20 seconds ago
in 30 seconds
20 seconds before noon
20 seconds before noon tomorrow
noon
midnight
noon tomorrow


-- Paul

Stefan Behnel

unread,
Jul 7, 2009, 9:01:07 AM7/7/09
to
m...@pixar.com wrote:
> I'm looking for something like Tcl's [clock scan] command which parses
> human-readable time strings such as:
>
> % clock scan "5 minutes ago"
> 1246925569
> % clock scan "tomorrow 12:00"
> 1246993200
> % clock scan "today + 1 fortnight"
> 1248135628
>
> Does any such package exist for Python?

Is this only for English times or is I18N a concern?

Stefan

0 new messages