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)
| 237 | zone) |
| 238 | |
| 239 | def 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 | |
| 268 | def format_datetime(dt, usegmt=False): |
| 269 | """Turn a datetime into a date string as specified in RFC 2822. |
nothing calls this directly
no test coverage detected
searching dependent graphs…