2019-02-27 00:45:00 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
#
|
|
|
|
|
import calendar
|
|
|
|
|
import threading
|
2022-11-29 06:42:04 +00:00
|
|
|
|
import time
|
|
|
|
|
from email.utils import formatdate
|
2019-02-27 00:45:00 +00:00
|
|
|
|
|
|
|
|
|
_STRPTIME_LOCK = threading.Lock()
|
|
|
|
|
|
|
|
|
|
_GMT_FORMAT = "%a, %d %b %Y %H:%M:%S GMT"
|
|
|
|
|
_ISO8601_FORMAT = "%Y-%m-%dT%H:%M:%S.000Z"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def to_unixtime(time_string, format_string):
|
|
|
|
|
time_string = time_string.decode("ascii")
|
|
|
|
|
with _STRPTIME_LOCK:
|
|
|
|
|
return int(calendar.timegm(time.strptime(time_string, format_string)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def http_date(timeval=None):
|
|
|
|
|
"""返回符合HTTP标准的GMT时间字符串,用strftime的格式表示就是"%a, %d %b %Y %H:%M:%S GMT"。
|
|
|
|
|
但不能使用strftime,因为strftime的结果是和locale相关的。
|
|
|
|
|
"""
|
|
|
|
|
return formatdate(timeval, usegmt=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def http_to_unixtime(time_string):
|
|
|
|
|
"""把HTTP Date格式的字符串转换为UNIX时间(自1970年1月1日UTC零点的秒数)。
|
|
|
|
|
|
|
|
|
|
HTTP Date形如 `Sat, 05 Dec 2015 11:10:29 GMT` 。
|
|
|
|
|
"""
|
|
|
|
|
return to_unixtime(time_string, _GMT_FORMAT)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def iso8601_to_unixtime(time_string):
|
|
|
|
|
"""把ISO8601时间字符串(形如,2012-02-24T06:07:48.000Z)转换为UNIX时间,精确到秒。"""
|
|
|
|
|
return to_unixtime(time_string, _ISO8601_FORMAT)
|
2022-11-29 06:42:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|