Exemplo n.º 1
0
def _strptime_datetime(cls, data_string, format="%a %b %d %H:%M:%S %Y"):
    """Return a class cls instance based on the input string and the
    format string."""
    tt, fraction = _strptime(data_string, format)
    tzname, gmtoff = tt[-2:]
    args = tt[:6] + (fraction,)
    if gmtoff is not None:
        tzdelta = datetime_timedelta(seconds=gmtoff)
        if tzname:
            tz = datetime_timezone(tzdelta, tzname)
        else:
            tz = datetime_timezone(tzdelta)
        args += (tz,)
    return cls(*args)
Exemplo n.º 2
0
def _strptime_datetime(cls, data_string, format="%a %b %d %H:%M:%S %Y"):
    """Return a class cls instance based on the input string and the
    format string."""
    tt, fraction = _strptime(data_string, format)
    tzname, gmtoff = tt[-2:]
    args = tt[:6] + (fraction, )
    if gmtoff is not None:
        tzdelta = datetime_timedelta(seconds=gmtoff)
        if tzname:
            tz = datetime_timezone(tzdelta, tzname)
        else:
            tz = datetime_timezone(tzdelta)
        args += (tz, )
    return cls(*args)
Exemplo n.º 3
0
    def parse_timezone(timezone_str):
        ''' Parse the timezone as an offset from UTC.

            Try the pytz module first.

            Returns tzinfo or None.
        '''

        #log.debug(f'timezone_str: {timezone_str}')
        tzinfo = None

        if PYTZ_AVAILABLE:
            try:
                tzinfo = pytz.timezone(timezone_str)
            except UnknownTimeZoneError:
                #log.debug(f'timezone unknown to pytz: {timezone_str}')
                pass

        if not tzinfo:
            match = TIMEZONE_RE.search(timezone_str)
            if match:
                mdict = match.groupdict()
                #log.debug(f'timezone dict: {mdict}')

                # name must be UTC; if it's not and pytz wasn't able to
                # handle the conversion, then we'll ignore the timezone
                if 'name' in mdict:
                    if mdict['name']:
                        timezone_is_utc = mdict['name'].upper() == 'UTC'
                    else:
                        timezone_is_utc = True
                else:
                    timezone_is_utc = True

                if timezone_is_utc:
                    # timedelta() requires hour/minute
                    if ('sign' in mdict) and ('hour' in mdict) and ('minute' in mdict):

                        tz_sign = mdict['sign']
                        tz_hours = int(mdict['hour'])
                        tz_minutes = int(mdict['minute'])

                        offset = timedelta(hours=tz_hours, minutes=tz_minutes)
                        if tz_sign == '-':
                            offset = -offset
                        tzinfo = datetime_timezone(offset)
            else:
                log.debug(f'no timezone parsed. TIMEZONE_RE did not match. timestr: {timestr}')

        return tzinfo