예제 #1
0
def parse_iso8601(datestring):
    """Parse and convert ISO-8601 string to datetime."""
    m = ISO8601_REGEX.match(datestring)
    if not m:
        raise ValueError("unable to parse date string %r" % datestring)
    groups = m.groupdict()
    tz = groups["timezone"]
    if tz == "Z":
        tz = FixedOffset(0)
    elif tz:
        m = TIMEZONE_REGEX.match(tz)
        prefix, hours, minutes = m.groups()
        hours, minutes = int(hours), int(minutes)
        if prefix == "-":
            hours = -hours
            minutes = -minutes
        tz = FixedOffset(minutes + hours * 60)
    return datetime(
        int(groups["year"]),
        int(groups["month"]),
        int(groups["day"]),
        int(groups["hour"] or 0),
        int(groups["minute"] or 0),
        int(groups["second"] or 0),
        int(groups["fraction"] or 0),
        tz,
    )
예제 #2
0
def test_from_native_case_2():
    # python -m pytest tests/unit/time/test_time.py -s -v -k test_from_native_case_2
    native = time(12, 34, 56, 789123, FixedOffset(0))
    t = Time.from_native(native)
    assert t.hour == native.hour
    assert t.minute == native.minute
    assert t.second == nano_add(native.second, nano_div(native.microsecond, 1000000))
    assert t.tzinfo == FixedOffset(0)
예제 #3
0
def test_from_native_case_2():
    # python -m pytest tests/unit/time/test_datetime.py -s -v -k test_from_native_case_2
    native = datetime(2018, 10, 1, 12, 34, 56, 789123, FixedOffset(0))
    dt = DateTime.from_native(native)
    assert dt.year == native.year
    assert dt.month == native.month
    assert dt.day == native.day
    assert dt.hour == native.hour
    assert dt.minute == native.minute
    assert dt.second == nano_add(native.second, nano_div(native.microsecond, 1000000))
    assert dt.tzinfo == FixedOffset(0)
예제 #4
0
    def __init__(self, repo, commit):
        self.repo = repo
        self._commit = commit
        self.type = 'commit'
        self.repo_name = repo.name
        parent = commit['parent'][0] if commit['parent'] else None
        self.parent = parent
        self.parents = commit['parent']
        message = ("%s\n\n%s" %
                   (commit['message'], remove_unknown_character(
                       commit['body']))).strip()
        self.message = message
        self.message_header = commit['message']
        self.message_body = commit['body']
        self.sha = commit['sha']
        self.tree = commit['tree']
        self.has_author_link = True

        # author
        author_name = commit['author']['name']
        self.author_name = author_name
        author_email = commit['author']['email']
        self.author_email = author_email
        self.email = author_email
        code_author_name = get_author_by_email(author_email, None)
        if code_author_name is None:
            self.has_author_link = False
            author = User(name=author_name, email=author_email)
        else:
            author = User(name=code_author_name, email=author_email)
        self.author = author
        author_date = datetime.fromtimestamp(
            commit['author']['time'], FixedOffset(commit['author']['offset']))
        self.author_time = author_date
        author_timestamp = str(commit['author']['time'])
        self.author_timestamp = author_timestamp
        self.time = author_date

        # committer
        committer_name = commit['committer']['name']
        committer_email = email_normalizer(committer_name,
                                           commit['committer']['email'])
        committer = User(name=committer_name, email=committer_email)
        self.committer = committer
        committer_date = datetime.fromtimestamp(
            commit['committer']['time'],
            FixedOffset(commit['committer']['offset']))
        self.committer_time = committer_date
        self.commit_time = committer_date  # FIXME: remove this!
예제 #5
0
def test_iso_format_with_time_zone_case_1():
    # python -m pytest tests/unit/time/test_time.py -s -v -k test_iso_format_with_time_zone_case_1
    expected = Time(7, 54, 2.129790999, tzinfo=timezone_utc)
    assert expected.iso_format() == "07:54:02.129790999+00:00"
    assert expected.tzinfo == FixedOffset(0)
    actual = Time.from_iso_format("07:54:02.129790999+00:00")
    assert expected == actual
def parse_stock_price(selector):
    logger = logging.getLogger('parse_stock_price')
    # <span class="price" id="Col0Price">42,41</span>
    price_item = selector.css('span#Col0Price.price::text').extract()
    value = float(price_item[0].replace(',', '.'))
    # <p class="priceInformation" id="Col0PriceTime">Päivitetty 20.03.2015<br />18:29:38
    # <abbr title="TimeZone_EET">EET</abbr>
    datetime_text = selector.css(
        'p#Col0PriceTime.priceInformation').extract()[0]
    datetime_text = html.fromstring(
        _HTML_CLEANER.clean_html(datetime_text)).text
    # datetime_text ~= u'Päivitetty 20.03.201518:29:38 EET | EUR \t\t ....
    date_text = datetime_text[10:21]
    time_text = datetime_text[21:29]
    tz_text = datetime_text[30:].partition('|')[0].strip()
    # UGLY: support for EEST (pytz does not support it), more proper support provided by this
    # https://github.com/mithro/python-datetime-tz/blob/master/datetime_tz/pytz_abbr.py
    if tz_text in ('EEST', 'EEDT'):
        tz = FixedOffset(3 * 60)
    else:
        tz = timezone(tz_text)
    value_date = pd.to_datetime(date_text + ' ' + time_text,
                                dayfirst=True).tz_localize(tz)
    value_date = value_date.tz_convert(UTC)
    return dict(value=value, value_date=value_date)
예제 #7
0
def convert_to_datetime(input, tz, arg_name):
    """
    Converts the given object to a timezone aware datetime object.

    If a timezone aware datetime object is passed, it is returned unmodified.
    If a native datetime object is passed, it is given the specified timezone.
    If the input is a string, it is parsed as a datetime with the given timezone.

    Date strings are accepted in three different forms: date only (Y-m-d), date with time
    (Y-m-d H:M:S) or with date+time with microseconds (Y-m-d H:M:S.micro). Additionally you can
    override the time zone by giving a specific offset in the format specified by ISO 8601:
    Z (UTC), +HH:MM or -HH:MM.

    :param str|datetime input: the datetime or string to convert to a timezone aware datetime
    :param datetime.tzinfo tz: timezone to interpret ``input`` in
    :param str arg_name: the name of the argument (used in an error message)
    :rtype: datetime

    """
    if input is None:
        return
    elif isinstance(input, datetime):
        datetime_ = input
    elif isinstance(input, date):
        datetime_ = datetime.combine(input, time())
    elif isinstance(input, six.string_types):
        m = _DATE_REGEX.match(input)
        if not m:
            raise ValueError('Invalid date string')

        values = m.groupdict()
        tzname = values.pop('timezone')
        if tzname == 'Z':
            tz = utc
        elif tzname:
            hours, minutes = (int(x) for x in tzname[1:].split(':'))
            sign = 1 if tzname[0] == '+' else -1
            tz = FixedOffset(sign * (hours * 60 + minutes))

        values = {k: int(v or 0) for k, v in values.items()}
        datetime_ = datetime(**values)
    else:
        raise TypeError('Unsupported type for %s: %s' %
                        (arg_name, input.__class__.__name__))

    if datetime_.tzinfo is not None:
        return datetime_
    if tz is None:
        raise ValueError(
            'The "tz" argument must be specified if %s has no timezone information'
            % arg_name)
    if isinstance(tz, six.string_types):
        tz = timezone(tz)

    try:
        return tz.localize(datetime_, is_dst=None)
    except AttributeError:
        raise TypeError(
            'Only pytz timezones are supported (need the localize() and normalize() methods)'
        )
예제 #8
0
 def test_datetime_with_numeric_time_offset(self):
     self.assert_supports_temporal_types()
     with self.driver.session() as session:
         result = session.run("RETURN datetime('1976-06-13T12:34:56.789012+01:30')")
         value = result.single().value()
         self.assertIsInstance(value, datetime)
         self.assertEqual(value, datetime(1976, 6, 13, 12, 34, 56, 789012, tzinfo=FixedOffset(90)))
예제 #9
0
    def from_string(cls, string):
        """expect ISO formatted dates"""
        def parse_date(date_match, tz=None):
            fields = date_match.groupdict(0)
            year, month, day, hour, min, sec = [int(fields[x]) for x in
                ("year", "month", "day", "hr", "min", "sec")]

            # use of decimal module here (rather than float) might be better
            # here, if willing to require python 2.4 or higher
            microsec = int(float(fields.get("sec_frac", 0)) * 10 ** 6)

            return datetime.datetime(year,month,day, hour,min,sec, microsec, tz)

        match = _utc_re.match(string)
        if match:
            return parse_date(match, tz=pytz.utc)

        match = _offset_re.match(string)
        if match:
            tz_hr, tz_min = [int(match.group(x)) for x in "tz_hr", "tz_min"]
            return parse_date(match, tz=FixedOffset(tz_hr * 60 + tz_min, {}))

        match = _local_re.match(string)
        if not match:
            raise Exception("DateTime [%s] not in known format" % string)

        return parse_date(match)
예제 #10
0
 def test_datetime_with_numeric_time_offset(self):
     self.assert_supports_temporal_types()
     with self.driver.session() as session:
         result = session.run(
             "CYPHER runtime=interpreted WITH $x AS x "
             "RETURN x.year, x.month, x.day, "
             "       x.hour, x.minute, x.second, x.microsecond, x.offset",
             x=datetime(1976,
                        6,
                        13,
                        12,
                        34,
                        56,
                        789012,
                        tzinfo=FixedOffset(90)))
         year, month, day, hour, minute, second, microsecond, offset = result.single(
         )
         self.assertEqual(year, 1976)
         self.assertEqual(month, 6)
         self.assertEqual(day, 13)
         self.assertEqual(hour, 12)
         self.assertEqual(minute, 34)
         self.assertEqual(second, 56)
         self.assertEqual(microsecond, 789012)
         self.assertEqual(offset, "+01:30")
예제 #11
0
def hydrate_datetime(seconds, nanoseconds, tz=None):
    """ Hydrator for `DateTime` and `LocalDateTime` values.

    :param seconds:
    :param nanoseconds:
    :param tz:
    :return: datetime
    """
    microseconds, nanoseconds = divmod(nanoseconds, 1000)
    if nanoseconds != 0:
        warn(
            "Nanosecond resolution is not available on this platform, value is truncated at microsecond resolution"
        )
    minutes, seconds = divmod(seconds, 60)
    hours, minutes = divmod(minutes, 60)
    days, hours = divmod(hours, 24)
    t = datetime.combine(UNIX_EPOCH_DATE + timedelta(days=days),
                         time(hours, minutes, seconds, microseconds))
    if tz is None:
        return t
    if isinstance(tz, int):
        tz_offset_minutes, tz_offset_seconds = divmod(tz, 60)
        zone = FixedOffset(tz_offset_minutes)
    else:
        zone = timezone(tz)
    return zone.localize(t)
예제 #12
0
 def test_time_with_numeric_time_offset(self):
     self.assert_supports_temporal_types()
     with self.driver.session() as session:
         result = session.run("RETURN time('12:34:56.789012345+0130')")
         value = result.single().value()
         self.assertIsInstance(value, Time)
         self.assertEqual(value, Time(12, 34, 56.789012345, tzinfo=FixedOffset(90)))
예제 #13
0
파일: _base.py 프로젝트: pxiol/spyne
    def datetime_from_string_iso(self, cls, string):
        astz = cls.Attributes.as_timezone

        match = cls._utc_re.match(string)
        if match:
            tz = pytz.utc
            retval = _parse_datetime_iso_match(match, tz=tz)
            if astz is not None:
                retval = retval.astimezone(astz)
            return retval

        if match is None:
            match = cls._offset_re.match(string)
            if match:
                tz_hr, tz_min = [int(match.group(x)) for x in ("tz_hr", "tz_min")]
                tz = FixedOffset(tz_hr * 60 + tz_min, {})
                retval = _parse_datetime_iso_match(match, tz=tz)
                if astz is not None:
                    retval = retval.astimezone(astz)
                return retval

        if match is None:
            match = cls._local_re.match(string)
            if match:
                retval = _parse_datetime_iso_match(match)
                if astz:
                    retval = retval.replace(tzinfo=astz)
                return retval

        raise ValidationError(string)
예제 #14
0
파일: primitives.py 프로젝트: jab/lektor-1
    def value_from_raw(self, raw):
        if raw.value is None:
            return raw.missing_value('Missing datetime')
        try:
            chunks = raw.value.split(' ')
            date_info = [int(bit) for bit in chunks[0].split('-')]
            time_info = [int(bit) for bit in chunks[1].split(':')]
            datetime_info = date_info + time_info
            result = datetime(*datetime_info)

            if len(chunks) > 2:
                try:
                    tz = get_timezone(chunks[-1])
                except LookupError:
                    if len(chunks[-1]) > 5:
                        chunks[-1] = chunks[-1][-5:]
                    delta = int(chunks[-1][1:3]) * 60 + int(chunks[-1][3:])
                    if chunks[-1][0] == '-':
                        delta *= -1
                    tz = FixedOffset(delta)
                return tz.localize(result)

            return result
        except Exception:
            return raw.bad_value('Bad date format')
예제 #15
0
파일: git.py 프로젝트: jackfrued/code-1
 def get_revlist(self,
                 max_count=20,
                 skip=0,
                 rev='HEAD',
                 path='',
                 author=None,
                 query=None):
     commits = []
     cs = self._gyt_repo.rev_list(rev,
                                  max_count=max_count,
                                  skip=skip,
                                  paths=path,
                                  author=author,
                                  query=query)
     for c in cs:
         commit = {}
         commit['parents'] = [p.hex for p in c.parents] if c.parents else ''
         commit['date'] = datetime.fromtimestamp(
             c.committer.time, FixedOffset(c.committer.offset))
         commit['age'] = compute_relative_time(c.author.time)
         commit['author'] = c.author.name
         commit['email'] = email_normalizer(c.author.name, c.author.email)
         message_title = c.message.splitlines()[0] if c.message else ''
         commit['message'] = message_title
         commit['commit'] = c.hex
         commits.append(commit)
     return commits
예제 #16
0
 def test_whole_second_time(self):
     self.assert_supports_temporal_types()
     with self.driver.session() as session:
         result = session.run("RETURN time('12:34:56')")
         value = result.single().value()
         self.assertIsInstance(value, Time)
         self.assertEqual(value, Time(12, 34, 56, tzinfo=FixedOffset(0)))
예제 #17
0
def _element_to_datetime(element):
    # expect ISO formatted dates
    #
    text = element.text
    if not text:
        return None

    def parse_date(date_match, tz=None):
        fields = date_match.groupdict(0)
        year, month, day, hr, min, sec = [
            int(fields[x])
            for x in ("year", "month", "day", "hr", "min", "sec")
        ]
        # use of decimal module here (rather than float) might be better
        # here, if willing to require python 2.4 or higher
        microsec = int(float(fields.get("fractional_sec", 0)) * 10**6)
        return datetime.datetime(year, month, day, hr, min, sec, microsec, tz)

    match = _utc_re.match(text)
    if match:
        return parse_date(match, tz=pytz.utc)
    match = _offset_re.match(text)
    if match:
        tz_hr, tz_min = [int(match.group(x)) for x in "tz_hr", "tz_min"]
        return parse_date(match, tz=FixedOffset(tz_hr * 60 + tz_min, {}))
    match = _local_re.match(text)
    if match:
        return parse_date(match)
    raise Exception("DateTime [%s] not in known format" % text)
예제 #18
0
 def test_format(self):
     """
     L{Date.format} returns a string representation of the given datetime
     instance.
     """
     parameter = Date("Test")
     date = datetime(2010, 9, 15, 23, 59, 59, tzinfo=FixedOffset(120))
     self.assertEqual("2010-09-15T21:59:59Z", parameter.format(date))
예제 #19
0
def test_to_native_case_2():
    # python -m pytest tests/unit/time/test_time.py -s -v -k test_to_native_case_2
    t = Time(12, 34, 56.789123456, tzinfo=timezone_utc)
    native = t.to_native()
    assert native.hour == t.hour
    assert native.minute == t.minute
    assert nano_add(native.second, nano_div(native.microsecond, 1000000)) == 56.789123
    assert native.tzinfo == FixedOffset(0)
    assert native.isoformat() == "12:34:56.789123+00:00"
예제 #20
0
def _convert_event_timestamp(event_timestamp: pd.Timestamp, t: EventTimestampType):
    if t == EventTimestampType.TZ_NAIVE:
        return event_timestamp
    elif t == EventTimestampType.TZ_AWARE_UTC:
        return event_timestamp.replace(tzinfo=utc)
    elif t == EventTimestampType.TZ_AWARE_FIXED_OFFSET:
        return event_timestamp.replace(tzinfo=utc).astimezone(FixedOffset(60))
    elif t == EventTimestampType.TZ_AWARE_US_PACIFIC:
        return event_timestamp.replace(tzinfo=utc).astimezone(timezone("US/Pacific"))
예제 #21
0
def test_to_native_case_2():
    # python -m pytest tests/unit/time/test_datetime.py -s -v -k test_to_native_case_2
    dt = DateTime.from_iso_format("2019-10-30T12:34:56.789123456+00:00")
    native = dt.to_native()
    assert native.hour == dt.hour
    assert native.minute == dt.minute
    assert nano_add(native.second, nano_div(native.microsecond, 1000000)) == 56.789123
    assert native.tzinfo == FixedOffset(0)
    assert native.isoformat() == "2019-10-30T12:34:56.789123+00:00"
예제 #22
0
파일: commit.py 프로젝트: banjin/code
    def __init__(self, repo, commit):
        self.repo = repo
        self._commit = commit
        self.type = 'commit'
        self.repo_name = repo.name
        parent = commit['parent'][0] if commit['parent'] else None
        self.parent = parent
        self.parents = commit['parent']
        message = ("%s\n\n%s" %
                   (commit['message'], remove_unknown_character(
                       commit['body']))).strip()
        self.message = message
        self.message_header = commit['message']
        self.message_body = commit['body']
        self.sha = commit['sha']
        self.tree = commit['tree']

        author_name = commit['author']['name']
        self.author_name = author_name
        author_email = email_normalizer(author_name, commit['author']['email'])
        self.author_email = author_email
        self.email = author_email
        # FIXME: user
        #author = User(name=author_name, email=author_email)
        author = User.get_by_name(author_name)
        self.author = author
        author_date = datetime.fromtimestamp(
            commit['author']['time'], FixedOffset(commit['author']['offset']))
        author_timestamp = str(commit['author']['time'])
        self.author_time = author_date
        self.author_timestamp = author_timestamp
        self.time = author_date

        committer_name = commit['committer']['name']
        committer_email = email_normalizer(committer_name,
                                           commit['committer']['email'])
        # FIXME: user
        #committer = User(name=committer_name, email=committer_email)
        committer = User.get_by_name(committer_name)
        self.committer = committer
        committer_date = datetime.fromtimestamp(
            commit['committer']['time'],
            FixedOffset(commit['committer']['offset']))
        self.committer_time = committer_date
예제 #23
0
 def test_nanosecond_resolution_time(self):
     self.assert_supports_temporal_types()
     with catch_warnings(record=True) as warning_list:
         simplefilter("always")
         with self.driver.session() as session:
             result = session.run("RETURN time('12:34:56.789012345')")
             value = result.single().value()
             self.assertIsInstance(value, time)
             self.assertEqual(value, time(12, 34, 56, 789012, tzinfo=FixedOffset(0)))
             self.assertEqual(len(warning_list), 1)
예제 #24
0
 def test_from_iso_format_with_negative_long_tz(self):
     expected = DateTime(2018,
                         10,
                         1,
                         12,
                         34,
                         56.123456789,
                         tzinfo=FixedOffset(-754))
     actual = DateTime.from_iso_format(
         "2018-10-01T12:34:56.123456789-12:34:56.123456")
     self.assertEqual(expected, actual)
예제 #25
0
 def test_time_with_numeric_time_offset(self):
     with self.driver.session() as session:
         result = session.run("CYPHER runtime=interpreted WITH $x AS x "
                              "RETURN x.hour, x.minute, x.second, x.nanosecond, x.offset",
                              x=Time(12, 34, 56.789012345, tzinfo=FixedOffset(90)))
         hour, minute, second, nanosecond, offset = result.single()
         self.assertEqual(hour, 12)
         self.assertEqual(minute, 34)
         self.assertEqual(second, 56)
         self.assertEqual(nanosecond, 789012345)
         self.assertEqual(offset, "+01:30")
def test_time_with_numeric_time_offset_input(cypher_eval):
    result = cypher_eval("CYPHER runtime=interpreted WITH $x AS x "
                         "RETURN [x.hour, x.minute, x.second, "
                         "        x.nanosecond, x.offset]",
                         x=Time(12, 34, 56.789012345, tzinfo=FixedOffset(90)))
    hour, minute, second, nanosecond, offset = result
    assert hour == 12
    assert minute == 34
    assert second == 56
    assert nanosecond == 789012345
    assert offset == "+01:30"
def test_datetime_with_numeric_time_offset_output(cypher_eval):
    value = cypher_eval("RETURN "
                        "datetime('1976-06-13T12:34:56.789012345+01:30')")
    assert isinstance(value, DateTime)
    assert value == DateTime(1976,
                             6,
                             13,
                             12,
                             34,
                             56.789012345,
                             tzinfo=FixedOffset(90))
예제 #28
0
파일: time.py 프로젝트: zxjly/celery
        def fromutc(self, dt):
            # The base tzinfo class no longer implements a DST
            # offset aware .fromutc() in Python 3 (Issue #2306).

            # I'd rather rely on pytz to do this, than port
            # the C code from cpython's fromutc [asksol]
            offset = int(self.utcoffset(dt).seconds / 60.0)
            try:
                tz = self._offset_cache[offset]
            except KeyError:
                tz = self._offset_cache[offset] = FixedOffset(offset)
            return tz.fromutc(dt.replace(tzinfo=tz))
예제 #29
0
def parse_iso8601(datestring):
    """Parse and convert ISO-8601 string to datetime."""
    m = ISO8601_REGEX.match(datestring)
    if not m:
        raise ValueError('unable to parse date string %r' % datestring)
    groups = m.groupdict()
    tz = groups['timezone']
    if tz == 'Z':
        tz = FixedOffset(0)
    elif tz:
        m = TIMEZONE_REGEX.match(tz)
        prefix, hours, minutes = m.groups()
        hours, minutes = int(hours), int(minutes)
        if prefix == '-':
            hours = -hours
            minutes = -minutes
        tz = FixedOffset(minutes + hours * 60)
    return datetime(int(groups['year']), int(groups['month']),
                    int(groups['day']), int(groups['hour'] or 0),
                    int(groups['minute'] or 0), int(groups['second'] or 0),
                    int(groups['fraction'] or 0), tz)
예제 #30
0
    def default_parse(cls, string):
        match = _utc_re.match(string)
        if match:
            return cls.parse(match, tz=pytz.utc)

        match = _offset_re.match(string)
        if match:
            tz_hr, tz_min = [int(match.group(x)) for x in ("tz_hr", "tz_min")]
            return cls.parse(match, tz=FixedOffset(tz_hr * 60 + tz_min, {}))

        match = _local_re.match(string)
        if match is None:
            raise ValidationError(string)

        return cls.parse(match)