4

Assuming the text is typed at the same time in the same (Israeli) timezone, The following free text lines are equivalent:

Wed Sep  9 16:26:57 IDT 2009
2009-09-09 16:26:57
16:26:57
September 9th, 16:26:57

Is there a python module that would convert all these text-dates to an (identical) datetime.datetime instance?

I would like to use it in a command-line tool that would get a freetext date and time as an argument, and return the equivalent date and time in different time zones, e.g.:

~$wdate 16:00 Israel
Israel:        16:00
San Francisco: 06:00
UTC:           13:00

or:

~$wdate 18:00 SanFran
San Francisco  18:00:22
Israel:        01:00:22 (Day after)
UTC:           22:00:22

Any Ideas?

Thanks,

Udi

Adam Matan
  • 128,757
  • 147
  • 397
  • 562

3 Answers3

6

The python-dateutil package sounds like it would be helpful. Your examples only use simple HH:MM timestamps with a (magically shortened) city identifier, but it seems able to handle more complicated formats like those earlier in the question, too.

unwind
  • 391,730
  • 64
  • 469
  • 606
3

parsedatetime seems to be a very capable module for this specific task.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
0

You could you time.strptime

Like this :

time.strptime("2009-09-09 16:26:57", "%Y-%m-%d %H:%M:%S")

It will return a struct_time value (more info on the python doc page).

Loïc Wolff
  • 3,205
  • 25
  • 26
  • Yes, but I'm looking for a module that would do the parsing for me - I guess this work has been done before. – Adam Matan Sep 09 '09 at 13:53