2

I have one time that is shown as:

2012-11-12 15:16:08.648521

And another time shown as:

14:44:02 Mon Nov 12 2012

Is there any way to convert the second time format into the format of the first time format (eg. 14:44:02 Mon Nov 12 2012 becomes 2012-11-12 14:44:02.?????)? I need to compare the second date (which is pulled from a log) to the other date, to confirm I'm getting the correct files.

J. Steen
  • 15,470
  • 15
  • 56
  • 63
EGr
  • 2,072
  • 10
  • 41
  • 61
  • Have you tried anything from the standard library `datetime` class? http://docs.python.org/2/library/datetime.html#datetime-objects – Hannele Nov 12 '12 at 20:21
  • possible duplicate of [Parsing Dates and Times from Strings using python](http://stackoverflow.com/questions/1713594/parsing-dates-and-times-from-strings-using-python) – Martijn Pieters Nov 12 '12 at 20:29

2 Answers2

6

Look at datetime.datetime.strptime.

You just need to write a format spec for each format, convert both values to datetime objects, and compare them. (You could convert one string format to the other and compare the strings, but that's not as good as idea, as I'll explain below.)

For example:

>>> import datetime
>>> fmt1 = '%Y-%m-%d %H:%M:%S.%f'
>>> fmt2 = '%H:%M:%S %a %b %d %Y'
>>> s1 = '2012-11-12 15:16:08.648521'
>>> s2 = '14:44:02 Mon Nov 12 2012'
>>> dt1 = datetime.datetime.strptime(s1, fmt1)
>>> dt2 = datetime.datetime.strptime(s2, fmt2)
>>> cmp(dt1, dt2)
1
>>> dt1
datetime.datetime(2012, 11, 12, 15, 16, 8, 648521)
>>> dt2
datetime.datetime(2012, 11, 12, 14, 44, 2)

Note that one of your time formats includes microseconds and the other doesn't, which means they will only actually be equal if the first one happens to be exactly at the second mark (ends with .000000). How to deal with that depends on what you're trying to do.

For example, if you want to round to to the nearest second, you can do something like this:

dt1 = datetime.datetime(dt1.year, dt1.month, dt1.day, 
                        dt1.hour, dt1.minute, 
                        dt1.second + (1 if dt1.microsecond >= 500000 else 0), 0)

Or, if you want to check for equality within a certain delta range:

abs(dt2 - dt1) < datetime.timedelta(0, 1)

One more problem you may run into is timezones. If one of those dates is local, and the other is GMT, you have to convert one or the other after parsing.

Your original question is asking how to convert one format to the other. You could do this, and then compare them as strings, but it's much better to compare them as actual datetime values. For one thing, if you convert them both to the second format, they don't even compare properly as strings (14:44:02 Mon Nov 12 2012 is less than 15:44:02 Sun Nov 11 2012 even though it's 23 hours later). It also makes dealing with microseconds, time zones, etc. a lot more complicated. But if you really want to do it, strftime is the opposite function to strptime:

>>> datetime.datetime.strftime(dt2, fmt1)
'2012-11-12 14:44:02.00000'
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • I just tried doing otime = "14:22:20 Mon Nov 12 2012 " and datetime.strptime(str(otime), "%Y-%m-%d %H:%M:%S"), but I get an error that the formats do not match – EGr Nov 12 '12 at 20:23
  • @EGr: Well, yes, that's because the formats don't match. The format you're using there tries to parse the `14` part as a `%Y` (a 4-digit year), the `:` part as a `-`, etc. But parse it with the right format and it'll work fine, as shown in my example above. – abarnert Nov 12 '12 at 20:26
  • Ahhh, okay. I didn't realize the format I needed to put in was the format it is currently in, not the format I wanted. Thanks! – EGr Nov 12 '12 at 20:31
  • @EGr: You really don't want to put it into any format—better to just convert them both to `datetime` objects and use them directly. – abarnert Nov 12 '12 at 20:35
  • 1
    it might also be worth noting that in case you want to dump your parsed dates, the first format is ISO 8601, which any datetime object can output with datetime.isoformat(' ') [here using a space for a separator, which is 'T' by default] – Cameron Nov 12 '12 at 20:36
1

Try

datetime.strptime("14:44:02 Mon Nov 12 2012", "%H:%M:%S %a %m %d %Y").strftime("%Y-%m-%d %H:%M:%S.%f")