本文整理汇总了Python中twisted.web.http.stringToDatetime方法的典型用法代码示例。如果您正苦于以下问题:Python http.stringToDatetime方法的具体用法?Python http.stringToDatetime怎么用?Python http.stringToDatetime使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.web.http
的用法示例。
在下文中一共展示了http.stringToDatetime方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testStringToDatetime
# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import stringToDatetime [as 别名]
def testStringToDatetime(self):
dateStrings = [
b"Sun, 06 Nov 1994 08:49:37 GMT",
b"06 Nov 1994 08:49:37 GMT",
b"Sunday, 06-Nov-94 08:49:37 GMT",
b"06-Nov-94 08:49:37 GMT",
b"Sunday, 06-Nov-1994 08:49:37 GMT",
b"06-Nov-1994 08:49:37 GMT",
b"Sun Nov 6 08:49:37 1994",
b"Nov 6 08:49:37 1994",
]
dateInt = calendar.timegm((1994, 11, 6, 8, 49, 37, 6, 6, 0))
for dateString in dateStrings:
self.assertEqual(http.stringToDatetime(dateString), dateInt)
self.assertEqual(
http.stringToDatetime(b"Thursday, 29-Sep-16 17:15:29 GMT"),
calendar.timegm((2016, 9, 29, 17, 15, 29, 3, 273, 0)))
示例2: _cache_period_from_headers
# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import stringToDatetime [as 别名]
def _cache_period_from_headers(headers, time_now=time.time):
cache_controls = _parse_cache_control(headers)
if b'no-store' in cache_controls:
return 0
if b'max-age' in cache_controls:
try:
max_age = int(cache_controls[b'max-age'])
return max_age
except ValueError:
pass
expires = headers.getRawHeaders(b'expires')
if expires is not None:
try:
expires_date = stringToDatetime(expires[-1])
return expires_date - time_now()
except ValueError:
# RFC7234 says 'A cache recipient MUST interpret invalid date formats,
# especially the value "0", as representing a time in the past (i.e.,
# "already expired").
return 0
return None
示例3: testRoundtrip
# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import stringToDatetime [as 别名]
def testRoundtrip(self):
for i in range(10000):
time = random.randint(0, 2000000000)
timestr = http.datetimeToString(time)
time2 = http.stringToDatetime(timestr)
self.assertEqual(time, time2)
示例4: retry_after
# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import stringToDatetime [as 别名]
def retry_after(cls, response, default=5, _now=time.time):
"""
Parse the Retry-After value from a response.
"""
val = response.headers.getRawHeaders(b'retry-after', [default])[0]
try:
return int(val)
except ValueError:
return http.stringToDatetime(val) - _now()
示例5: testRoundtrip
# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import stringToDatetime [as 别名]
def testRoundtrip(self):
for i in range(10000):
time = random.randint(0, 2000000000)
timestr = http.datetimeToString(time)
time2 = http.stringToDatetime(timestr)
self.assertEquals(time, time2)