本文整理汇总了Python中email.utils.parsedate_tz方法的典型用法代码示例。如果您正苦于以下问题:Python utils.parsedate_tz方法的具体用法?Python utils.parsedate_tz怎么用?Python utils.parsedate_tz使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email.utils
的用法示例。
在下文中一共展示了utils.parsedate_tz方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dateToUTCstr
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_tz [as 别名]
def dateToUTCstr(str_date):
# this fails to parse timezones out of formats like
# Tue, 17 Jun 2010 08:33:51 EDT
# so it will assume the local timezone for those cases
try:
dt = dateutil.parser.parse(str_date)
except (TypeError, ValueError) as e:
# print u"Failed to parse date with dateutil, using email utils: date={}".format(str_date)
parsed_dt = parsedate_tz(str_date)
# Make an arbitrary tz info object name can be anything NSTZ "Newman Seconds Time Zone"
nstz_info = dateutil.tz.tzoffset("NSTZ",parsed_dt[9])
dt= datetime.datetime(*parsed_dt[:6], tzinfo=nstz_info)
if not dt.tzinfo:
print "WARNING: Failed to parse timezone defaulting to UTC for Date: {}".format(str_date)
dt = dt.replace(tzinfo=dateutil.tz.tzutc())
dt_tz = dt.astimezone(dateutil.tz.tzutc())
time_str = dt_tz.strftime('%Y-%m-%dT%H:%M:%S')
# print u"Parsed date={} ====> {}".format(str_date, time_str)
return time_str
示例2: _string_to_dt
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_tz [as 别名]
def _string_to_dt(self, string):
"""
Convert String to correct DateTime format.
"""
if string is None:
return None
"""
# TODO: Use RFC2822. This doesn't work consistently.
# TODO: Move this to same module as `format_date` once fixed.
tup = parsedate_tz(string)
if len(tup) == 8:
tup = tup + (0,) # If TimeZone is omitted, assume UTC
ts = mktime_tz(tup)
dt = datetime.fromtimestamp(ts, pytz.UTC)
return dt
"""
assert isinstance(string, str)
dt = datetime.strptime(string, "%Y-%m-%dT%H:%M:%SZ")
return dt
示例3: parse_date
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_tz [as 别名]
def parse_date(value):
"""Parse one of the following date formats into a datetime object:
.. sourcecode:: text
Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
If parsing fails the return value is `None`.
:param value: a string with a supported date format.
:return: a :class:`datetime.datetime` object.
"""
if value:
t = parsedate_tz(value.strip())
if t is not None:
try:
year = t[0]
# unfortunately that function does not tell us if two digit
# years were part of the string, or if they were prefixed
# with two zeroes. So what we do is to assume that 69-99
# refer to 1900, and everything below to 2000
if year >= 0 and year <= 68:
year += 2000
elif year >= 69 and year <= 99:
year += 1900
return datetime(*((year,) + t[1:7])) - timedelta(seconds=t[-1] or 0)
except (ValueError, OverflowError):
return None
示例4: update_headers
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_tz [as 别名]
def update_headers(self, resp):
headers = resp.headers
if 'expires' in headers:
return {}
if 'cache-control' in headers and headers['cache-control'] != 'public':
return {}
if resp.status not in self.cacheable_by_default_statuses:
return {}
if 'date' not in headers or 'last-modified' not in headers:
return {}
date = calendar.timegm(parsedate_tz(headers['date']))
last_modified = parsedate(headers['last-modified'])
if date is None or last_modified is None:
return {}
now = time.time()
current_age = max(0, now - date)
delta = date - calendar.timegm(last_modified)
freshness_lifetime = max(0, min(delta / 10, 24 * 3600))
if freshness_lifetime <= current_age:
return {}
expires = date + freshness_lifetime
return {'expires': time.strftime(TIME_FMT, time.gmtime(expires))}
示例5: _preserve_filemtime
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_tz [as 别名]
def _preserve_filemtime(self, lmdate):
if lmdate is not None:
timedata = parsedate_tz(lmdate)
lmtime = mktime(timedata[:9])
util.change_filemtime(self._destination, lmtime)
示例6: parse_date
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_tz [as 别名]
def parse_date(value):
"""Parse one of the following date formats into a datetime object:
.. sourcecode:: text
Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
If parsing fails the return value is `None`.
:param value: a string with a supported date format.
:return: a :class:`datetime.datetime` object.
"""
if value:
t = parsedate_tz(value.strip())
if t is not None:
try:
year = t[0]
# unfortunately that function does not tell us if two digit
# years were part of the string, or if they were prefixed
# with two zeroes. So what we do is to assume that 69-99
# refer to 1900, and everything below to 2000
if year >= 0 and year <= 68:
year += 2000
elif year >= 69 and year <= 99:
year += 1900
return datetime(*((year,) + t[1:7])) - \
timedelta(seconds=t[-1] or 0)
except (ValueError, OverflowError):
return None
示例7: test_parsedate_no_dayofweek
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_tz [as 别名]
def test_parsedate_no_dayofweek(self):
eq = self.assertEqual
eq(utils.parsedate_tz('25 Feb 2003 13:47:26 -0800'),
(2003, 2, 25, 13, 47, 26, 0, 1, -1, -28800))
示例8: test_parsedate_compact_no_dayofweek
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_tz [as 别名]
def test_parsedate_compact_no_dayofweek(self):
eq = self.assertEqual
eq(utils.parsedate_tz('5 Feb 2003 13:47:26 -0800'),
(2003, 2, 5, 13, 47, 26, 0, 1, -1, -28800))
示例9: test_parsedate_acceptable_to_time_functions
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_tz [as 别名]
def test_parsedate_acceptable_to_time_functions(self):
eq = self.assertEqual
timetup = utils.parsedate('5 Feb 2003 13:47:26 -0800')
t = int(time.mktime(timetup))
eq(time.localtime(t)[:6], timetup[:6])
eq(int(time.strftime('%Y', timetup)), 2003)
timetup = utils.parsedate_tz('5 Feb 2003 13:47:26 -0800')
t = int(time.mktime(timetup[:9]))
eq(time.localtime(t)[:6], timetup[:6])
eq(int(time.strftime('%Y', timetup[:9])), 2003)
示例10: _preserve_filemtime
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_tz [as 别名]
def _preserve_filemtime(self, lmdate):
timedata = parsedate_tz(lmdate)
lmtime = mktime(timedata[:9])
util.change_filemtime(self._destination, lmtime)
示例11: dateToUTCstr
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_tz [as 别名]
def dateToUTCstr(str_date):
# this fails to parse timezones out of formats like
# Tue, 17 Jun 2010 08:33:51 EDT
# so it will assume the local timezone for those cases
try:
dt = dateutil.parser.parse(str_date)
except TypeError:
dt= datetime.datetime(*parsedate_tz(str_date)[:6])
if not dt.tzinfo:
dt = dt.replace(tzinfo=dateutil.tz.tzutc())
dt_tz = dt.astimezone(dateutil.tz.tzutc())
return dt_tz.strftime('%Y-%m-%dT%H:%M:%S')
示例12: dateToUTCstr
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_tz [as 别名]
def dateToUTCstr(str_date):
str_date = '' if not str_date else str_date
try:
dt = dateutil.parser.parse(str_date)
except TypeError:
dt= datetime.datetime(*parsedate_tz(str_date)[:6])
if not dt.tzinfo:
dt = dt.replace(tzinfo=dateutil.tz.tzutc())
dt_tz = dt.astimezone(dateutil.tz.tzutc())
return dt_tz.strftime('%Y-%m-%dT%H:%M:%S')
示例13: from_request
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_tz [as 别名]
def from_request(cls, req):
if req.status_code == requests.codes.no_content:
data = None
else:
data = req.json()
raw_date = req.headers.get("Date", None)
if raw_date:
# See https://stackoverflow.com/a/26435566
timestamp = mktime_tz(parsedate_tz(raw_date))
else:
timestamp = None
return cls(data=data, server_time=timestamp)
示例14: update_headers
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import parsedate_tz [as 别名]
def update_headers(self, resp):
headers = resp.headers
if "expires" in headers:
return {}
if "cache-control" in headers and headers["cache-control"] != "public":
return {}
if resp.status not in self.cacheable_by_default_statuses:
return {}
if "date" not in headers or "last-modified" not in headers:
return {}
date = calendar.timegm(parsedate_tz(headers["date"]))
last_modified = parsedate(headers["last-modified"])
if date is None or last_modified is None:
return {}
now = time.time()
current_age = max(0, now - date)
delta = date - calendar.timegm(last_modified)
freshness_lifetime = max(0, min(delta / 10, 24 * 3600))
if freshness_lifetime <= current_age:
return {}
expires = date + freshness_lifetime
return {"expires": time.strftime(TIME_FMT, time.gmtime(expires))}