parsing times like "5 minutes ago"?

Carl Banks pavlovevidence at gmail.com
Tue Jul 7 02:43:09 CEST 2009


On Jul 6, 5:21 pm, m... at 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



More information about the Python-list mailing list