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)
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 )
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)