parsing times like "5 minutes ago"?

M

mh

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
 
C

Carl Banks

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
 
S

selasley

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

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

Paul McGuire

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

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
 
S

Stefan Behnel

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top