def test_convert_to_utc(self):

        strings = {
            # Valid UTC strings and fractions
            '2017-11-05T01:41:20Z': 1509846080.0,
            '2017-11-05T01:41:20.123Z': 1509846080.123,
            '2017-11-05T01:41:20.123123123+00:00': 1509846080.123123,
            '2017-11-05T01:41:20.123123123123123-00:00': 1509846080.123123,

            # Invalid strings
            '2017-11-05T01:41:20Z+05:00': None,
            '2017-11-05T01:41:20+0000': None,
            '2017-11-05T01:41:20-0000': None,

            # Valid strings with offset
            '2017-11-05T01:41:20-05:00': 1509864080.0,
            '2017-11-05T01:41:20+05:00': 1509828080.0,
        }

        strings2 = {
            # Valid strings with offset
            '2017-11-05T01:41:20-05:00':
            datetime(2017, 11, 5, 1, 41, 20, 0, create_tzinfo(hours=-5)),
            '2017-11-05T01:41:20+05:00':
            datetime(2017, 11, 5, 1, 41, 20, 0, create_tzinfo(hours=5)),
        }

        for time_string, expected_value in strings.items():
            result = parse_datetime(time_string, convert='utc', epoch=True)
            self.assertEqual(result, expected_value)

        for time_string, expected_value in strings2.items():
            result = parse_datetime(time_string, convert='utc')
            self.assertEqual(result, expected_value.astimezone(timezone.utc))
Esempio n. 2
0
    def _extract_info(self, stanza):
        time_ = stanza.getTag('time')
        if not time_:
            self._log.warning('No time node: %s', stanza)
            return None

        tzo = time_.getTag('tzo').getData()
        if not tzo:
            self._log.warning('Wrong tzo node: %s', stanza)
            return None

        remote_tz = create_tzinfo(tz_string=tzo)
        if remote_tz is None:
            self._log.warning('Wrong tzo node: %s', stanza)
            return None

        utc_time = time_.getTag('utc').getData()
        date_time = parse_datetime(utc_time, check_utc=True)
        if date_time is None:
            self._log.warning('Wrong timezone definition: %s %s',
                              utc_time, stanza.getFrom())
            return None

        date_time = date_time.astimezone(remote_tz)
        return date_time.strftime('%c %Z')
    def test_convert_to_local(self):

        strings = {
            # Valid UTC strings and fractions
            '2017-11-05T01:41:20Z':
            datetime(2017, 11, 5, 1, 41, 20, 0, timezone.utc),
            '2017-11-05T01:41:20.123Z':
            datetime(2017, 11, 5, 1, 41, 20, 123000, timezone.utc),
            '2017-11-05T01:41:20.123123123+00:00':
            datetime(2017, 11, 5, 1, 41, 20, 123123, timezone.utc),
            '2017-11-05T01:41:20.123123123123123-00:00':
            datetime(2017, 11, 5, 1, 41, 20, 123123, timezone.utc),

            # Valid strings with offset
            '2017-11-05T01:41:20-05:00':
            datetime(2017, 11, 5, 1, 41, 20, 0, create_tzinfo(hours=-5)),
            '2017-11-05T01:41:20+05:00':
            datetime(2017, 11, 5, 1, 41, 20, 0, create_tzinfo(hours=5)),
        }

        for time_string, expected_value in strings.items():
            result = parse_datetime(time_string, convert='local')
            self.assertEqual(result,
                             expected_value.astimezone(LocalTimezone()))
Esempio n. 4
0
def _parse_response(response):
    time_ = response.getTag('time')
    if not time_:
        raise MalformedStanzaError('time node missing', response)

    tzo = time_.getTagData('tzo')
    if not tzo:
        raise MalformedStanzaError('tzo node or data missing', response)

    remote_tz = create_tzinfo(tz_string=tzo)
    if remote_tz is None:
        raise MalformedStanzaError('invalid tzo data', response)

    utc_time = time_.getTagData('utc')
    if not utc_time:
        raise MalformedStanzaError('utc node or data missing', response)

    date_time = parse_datetime(utc_time, check_utc=True)
    if date_time is None:
        raise MalformedStanzaError('invalid timezone definition', response)

    date_time = date_time.astimezone(remote_tz)
    return date_time.strftime('%c %Z')