RFC 1123 date representation in Python?

Is there an easy way to convert a datetime object to an RFC 1123 date / time string (HTTP / 1.1), i.e. format string

Sun, 06 Nov 1994 08:49:37 GMT 

Using strftime does not work as strings are language dependent. Do I need to build a string manually?

+52
python datetime
Oct 22 '08 at 9:59
source share
5 answers

You can use wsgiref.handlers.format_date_time from stdlib, which is independent of locale settings

 from wsgiref.handlers import format_date_time from datetime import datetime from time import mktime now = datetime.now() stamp = mktime(now.timetuple()) print format_date_time(stamp) #--> Wed, 22 Oct 2008 10:52:40 GMT 

You can use email.utils.formatdate from stdlib, which is independent of locale settings.

 from email.utils import formatdate from datetime import datetime from time import mktime now = datetime.now() stamp = mktime(now.timetuple()) print formatdate( timeval = stamp, localtime = False, usegmt = True ) #--> Wed, 22 Oct 2008 10:55:46 GMT 

If you can set the local process wide, you can do:

 import locale, datetime locale.setlocale(locale.LC_TIME, 'en_US') datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT') 

If you do not want to set the language as a whole, you can use Babel date formation

 from datetime import datetime from babel.dates import format_datetime now = datetime.utcnow() format = 'EEE, dd LLL yyyy hh:mm:ss' print format_datetime(now, format, locale='en') + ' GMT' 

A manual formatting method that is identical to wsgiref.handlers.format_date_time:

 def httpdate(dt): """Return a string representation of a date according to RFC 1123 (HTTP/1.1). The supplied date must be in UTC. """ weekday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dt.weekday()] month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][dt.month - 1] return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (weekday, dt.day, month, dt.year, dt.hour, dt.minute, dt.second) 
+75
Oct 22 '08 at 10:07
source share

You can use the formatdate () function from the standard Python email module:

 from email.utils import formatdate print formatdate(timeval=None, localtime=False, usegmt=True) 

Gives the current time in the desired format:

 Wed, 22 Oct 2008 10:32:33 GMT 

In fact, this function does this "manually" without using strftime ()

+29
22 Oct. '08 at 10:34
source share

If anyone reads this, is working on a Django project, Django provides the function django.utils.http.http_date(epoch_seconds) .

 from django.utils.http import http_date some_datetime = some_object.last_update response['Last-Modified'] = http_date(some_datetime.timestamp()) 
+5
May 12, '16 at 15:18
source share

You can set LC_TIME to force stftime () to use a specific locale:

 >>> locale.setlocale(locale.LC_TIME, 'en_US') 'en_US' >>> datetime.datetime.now().strftime(locale.nl_langinfo(locale.D_T_FMT)) 'Wed 22 Oct 2008 06:05:39 AM ' 
+1
Oct 22 '08 at 10:05
source share

Well, here is a manual function for formatting it:

 def httpdate(dt): """Return a string representation of a date according to RFC 1123 (HTTP/1.1). The supplied date must be in UTC. """ weekday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dt.weekday()] month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][dt.month - 1] return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (weekday, dt.day, month, dt.year, dt.hour, dt.minute, dt.second) 
+1
Oct 22 '08 at 10:40
source share



All Articles