当前位置: 首页>>代码示例>>Python>>正文


Python utils.parsedate_tz方法代码示例

本文整理汇总了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 
开发者ID:Sotera,项目名称:pst-extraction,代码行数:26,代码来源:email_extract_json_unicode.py

示例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 
开发者ID:quay,项目名称:quay,代码行数:22,代码来源:mirror.py

示例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 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:32,代码来源:http.py

示例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))} 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:31,代码来源:heuristics.py

示例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) 
开发者ID:FPGAwars,项目名称:apio,代码行数:7,代码来源:downloader.py

示例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 
开发者ID:jpush,项目名称:jbox,代码行数:33,代码来源:http.py

示例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)) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:6,代码来源:test_email_renamed.py

示例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)) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:6,代码来源:test_email_renamed.py

示例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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:12,代码来源:test_email_renamed.py

示例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) 
开发者ID:bq,项目名称:web2board,代码行数:6,代码来源:downloader.py

示例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') 
开发者ID:Sotera,项目名称:pst-extraction,代码行数:15,代码来源:email_extract_json.py

示例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') 
开发者ID:Sotera,项目名称:pst-extraction,代码行数:12,代码来源:msg.py

示例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) 
开发者ID:appuio,项目名称:nagios-plugins-openshift,代码行数:17,代码来源:hawkular_client.py

示例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))} 
开发者ID:PacktPublishing,项目名称:Mastering-Elasticsearch-7.0,代码行数:31,代码来源:heuristics.py


注:本文中的email.utils.parsedate_tz方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。