Skip to content

Instantly share code, notes, and snippets.

@noqqe
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noqqe/54fae1e42280be99b290 to your computer and use it in GitHub Desktop.
Save noqqe/54fae1e42280be99b290 to your computer and use it in GitHub Desktop.
python2.7 email.utils.parsedate() seems to be broken O_o
import time
import email.utils
d1='Mon, 18 Nov 2014 14:10:44 +0000'
d2='Sat, 15 Nov 2014 14:10:47 +0000'
d3='Fri, 14 Nov 2014 11:49:18 -0800'
d4=time.strptime("16 11 14", "%d %m %y")
d1e=email.utils.parsedate(d1)
print "d1: " + d1 + " (correct because monday)"
print d1e
print time.strftime("%A", d1e)
print
d2e=email.utils.parsedate(d2)
print "d2: " + d2 + " (broken!)"
print d2e
print time.strftime("%A", d2e)
print
d3e=email.utils.parsedate(d3)
print "d3: " + d3 + " (broken!)"
print d3e
print time.strftime("%A", d3e)
print
print "d4: Sunday without email.utils.parsedate() (works!)"
print d4
print time.strftime("%A", d4)
print
print "d5: Convert email.utils.parsedate to epoch and throw into strftime() (works!)"
d5e=email.utils.parsedate(d2)
print d5e
d5e=time.mktime(d5e)
print d5e
print time.strftime("%F %A", time.localtime(d5e))
$ python dates.py
d1: Mon, 18 Nov 2014 14:10:44 +0000 (correct because monday)
(2014, 11, 18, 14, 10, 44, 0, 1, -1)
Monday
d2: Sat, 15 Nov 2014 14:10:47 +0000 (broken!)
(2014, 11, 15, 14, 10, 47, 0, 1, -1)
Monday
d3: Fri, 14 Nov 2014 11:49:18 -0800 (broken!)
(2014, 11, 14, 11, 49, 18, 0, 1, -1)
Monday
d4: Sunday without email.utils.parsedate() (works!)
time.struct_time(tm_year=2014, tm_mon=11, tm_mday=16, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=320, tm_isdst=-1)
Sunday
d5: Convert email.utils.parsedate to epoch and throw into strftime() (works!)
(2014, 11, 15, 14, 10, 47, 0, 1, -1)
1416057047.0
2014-11-15 Saturday
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment