views:

109

answers:

1

I have a lot of date strings like Mon, 16 Aug 2010 24:00:00 and some of them are in 00-23 hour format and some of them in 01-24 hour format. I want to get a list of date objects of them, but when I try to transform the example string into a date object, I have to transform it from Mon, 16 Aug 2010 24:00:00 to Tue, 17 Aug 2010 00:00:00. What is the easiest way?

+6  A: 
import email.utils as eutils
import time
import datetime

ntuple=eutils.parsedate('Mon, 16 Aug 2010 24:00:00')
print(ntuple)
# (2010, 8, 16, 24, 0, 0, 0, 1, -1)
timestamp=time.mktime(ntuple)
print(timestamp)
# 1282017600.0
date=datetime.datetime.fromtimestamp(timestamp)
print(date)
# 2010-08-17 00:00:00
print(date.strftime('%a, %d %b %Y %H:%M:%S'))
# Tue, 17 Aug 2010 00:00:00

Since you say you have a lot of these to fix, you should define a function:

def standardize_date(date_str):
    ntuple=eutils.parsedate(date_str)
    timestamp=time.mktime(ntuple)
    date=datetime.datetime.fromtimestamp(timestamp)
    return date.strftime('%a, %d %b %Y %H:%M:%S')

print(standardize_date('Mon, 16 Aug 2010 24:00:00'))
# Tue, 17 Aug 2010 00:00:00
unutbu
+1. Just because I liked it >8-)
Manoj Govindan
+1 because I never even knew `email.utils.parsedate()` existed until now and learning is fun!
jathanism
Yes, I learned about `email.utils.parsedate` here on SO too... :)
unutbu
Not sure if files are ever time-stamped with leap seconds, but there can be up to 86402 seconds in a day (though usually just 86401 when needed). (time can go `Mon, 16 Aug 24:00:01` to `Tue, 17 Aug 00:00:00`)
Nick T