You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
#
|
|
|
|
|
import time
|
|
|
|
|
from email.utils import formatdate
|
|
|
|
|
import calendar
|
|
|
|
|
import threading
|
|
|
|
|
|
|
|
|
|
_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)
|