MCPcopy Index your code
hub / github.com/python/cpython / formatdate

Function formatdate

Lib/email/utils.py:239–266  ·  view source on GitHub ↗

Returns a date string as specified by RFC 2822, e.g.: Fri, 09 Nov 2001 01:08:47 -0000 Optional timeval if given is a floating-point time value as accepted by gmtime() and localtime(), otherwise the current time is used. Optional localtime is a flag that when True, interprets timev

(timeval=None, localtime=False, usegmt=False)

Source from the content-addressed store, hash-verified

237 zone)
238
239def formatdate(timeval=None, localtime=False, usegmt=False):
240 """Returns a date string as specified by RFC 2822, e.g.:
241
242 Fri, 09 Nov 2001 01:08:47 -0000
243
244 Optional timeval if given is a floating-point time value as accepted by
245 gmtime() and localtime(), otherwise the current time is used.
246
247 Optional localtime is a flag that when True, interprets timeval, and
248 returns a date relative to the local timezone instead of UTC, properly
249 taking daylight savings time into account.
250
251 Optional argument usegmt means that the timezone is written out as
252 an ascii string, not numeric one (so "GMT" instead of "+0000"). This
253 is needed for HTTP, and is only used when localtime==False.
254 """
255 # Note: we cannot use strftime() because that honors the locale and RFC
256 # 2822 requires that day and month names be the English abbreviations.
257 if timeval is None:
258 timeval = time.time()
259 dt = datetime.datetime.fromtimestamp(timeval, datetime.timezone.utc)
260
261 if localtime:
262 dt = dt.astimezone()
263 usegmt = False
264 elif not usegmt:
265 dt = dt.replace(tzinfo=None)
266 return format_datetime(dt, usegmt)
267
268def format_datetime(dt, usegmt=False):
269 """Turn a datetime into a date string as specified in RFC 2822.

Callers

nothing calls this directly

Calls 5

format_datetimeFunction · 0.85
astimezoneMethod · 0.80
timeMethod · 0.45
fromtimestampMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…