0

Possible Duplicate:
Converting string into datetime

What's a good way to convert a string of text (that represents a time) into a timestamp.

Times could come in various formats that are not predictable.

'January 2, 2012'
'2012-01-05 13:01:51'
'2012-01-01'

All outputs should be timestamps:

1325480400
1325786511
1325394000

The time.strptime() function requires that inputs be consistent. I'm parsing a list of user-submitted dates that have various time formats.

Community
  • 1
  • 1
ensnare
  • 40,069
  • 64
  • 158
  • 224
  • 1
    Is there a good reason why you cannot use `dateutil`? – Matthew Trevor Aug 27 '12 at 03:25
  • @esaelPsnoroMoN Different question. I can't predict the format of the strings, so strptime() does no good. It's not a consistent format. – ensnare Aug 27 '12 at 03:29
  • 1
    Use exceptions with `strptime()` – Daniil Ryzhkov Aug 27 '12 at 03:33
  • And there is not reliable way for guessing date format. Date formats can be ambigious. So what you are trying to do is partly weird. –  Aug 27 '12 at 03:35
  • @esaelPsnoroMoN These are coming from a list of user-submitted dates, I can't control the input. – ensnare Aug 27 '12 at 03:42
  • 1
    @ensnare: Why can't you use 3rd party libraries? Can you use them if they satisfy certain license requirements? – Cam Aug 27 '12 at 03:54
  • 1
    If you have a limited set of date string formats, you could try to identify them using regexps an then take the necessary action to convert it using the available tools from python. – Francisco Aug 27 '12 at 03:57
  • 1
    You could also try doing a depth-first search or something similar where you try different combinations of tokens using strptime(), if you don't need the input processing to be blazing-fast. – Cam Aug 27 '12 at 03:58

2 Answers2

2

Use time.strptime()

>>> int(mktime(strptime('January 2, 2012', '%B %d, %Y')))
1325422800
>>> int(mktime(strptime('2012-01-05 13:01:51', '%Y-%m-%d %H:%M:%S')))
1325728911
>>> int(mktime(strptime('2012-01-05', '%Y-%m-%d')))
1325682000
Daniil Ryzhkov
  • 7,416
  • 2
  • 41
  • 58
  • What has this do to with the question? The question is about parsing dates without knowledge of the date format –  Aug 27 '12 at 03:52
  • 3
    Everything - if you don't know the format, you have to guess multiple times. Try tonnes of different formats, catching the exceptions, or use a library like dateutil which should make it only partially easier. OP is trying to solve a problem which can't really be solved trivially, but is complaining about having to write a lot of cases.. don't allow users to manually enter dates. Ever. – Josh Smeaton Aug 27 '12 at 03:57
  • No solution deals with ambigious date formats. And not guessing is better than *wild* guessing –  Aug 27 '12 at 04:10
-1

Check out this post :

What is the formula for calculating a timestamp?

it's in PHP but does the job

Community
  • 1
  • 1
jay
  • 507
  • 4
  • 16
  • PHP has a great function, strtotime(), but it doesn't seem like anything similar exists natively in Python without the use of 3rd party libraries. – ensnare Aug 27 '12 at 03:51
  • 2
    I don't think this helps. The question is about how to figure out what date a string is referring to, not how to calculate a timestamp given a known date. – Cam Aug 27 '12 at 03:51