Пример #1
0
    def testTimeConvertNative(self):
        class GMT8(datetime.tzinfo):
            def utcoffset(self, dt):
                return datetime.timedelta(hours=8)

            def dst(self, date_time):
                return datetime.timedelta(0)

            def tzname(self, dt):
                return "GMT +8"

        class UTC(datetime.tzinfo):
            def utcoffset(self, dt):
                return datetime.timedelta(hours=0)

            def dst(self, date_time):
                return datetime.timedelta(0)

            def tzname(self, dt):
                return "UTC"

        to_milliseconds = utils.to_milliseconds
        to_datetime = utils.to_datetime

        base_time = datetime.datetime.now().replace(microsecond=0)
        base_time_utc = datetime.datetime.utcfromtimestamp(
            time.mktime(base_time.timetuple()))
        milliseconds = long_type(time.mktime(base_time.timetuple())) * 1000

        self.assertEqual(milliseconds, to_milliseconds(base_time,
                                                       local_tz=True))
        self.assertEqual(milliseconds,
                         to_milliseconds(base_time_utc, local_tz=False))

        self.assertEqual(to_datetime(milliseconds, local_tz=True), base_time)
        self.assertEqual(to_datetime(milliseconds, local_tz=False),
                         base_time_utc)

        base_time = datetime.datetime.now(tz=GMT8()).replace(microsecond=0)
        milliseconds = long_type(
            calendar.timegm(base_time.astimezone(UTC()).timetuple())) * 1000

        self.assertEqual(milliseconds, to_milliseconds(base_time,
                                                       local_tz=True))
        self.assertEqual(milliseconds,
                         to_milliseconds(base_time, local_tz=False))
        self.assertEqual(milliseconds,
                         to_milliseconds(base_time, local_tz=UTC()))

        self.assertEqual(to_datetime(milliseconds, local_tz=GMT8()), base_time)

        base_time = base_time.replace(tzinfo=None)

        self.assertEqual(milliseconds,
                         to_milliseconds(base_time, local_tz=GMT8()))
    def testTimeConvertPytz(self):
        base_time = datetime.datetime.now(
            tz=pytz.timezone('Etc/GMT-8')).replace(microsecond=0)
        milliseconds = long_type(
            calendar.timegm(base_time.astimezone(pytz.utc).timetuple())) * 1000

        self.assertEqual(to_datetime(milliseconds, local_tz='Etc/GMT-8'),
                         base_time)

        base_time = base_time.replace(tzinfo=None)

        self.assertEqual(milliseconds,
                         to_milliseconds(base_time, local_tz='Etc/GMT-8'))
        self.assertEqual(
            milliseconds,
            to_milliseconds(base_time, local_tz=pytz.timezone('Etc/GMT-8')))

        base_time = datetime.datetime.now(
            tz=pytz.timezone('Etc/GMT-8')).replace(microsecond=0)
        milliseconds = time.mktime(base_time.timetuple()) * 1000

        self.assertEqual(milliseconds, to_milliseconds(base_time,
                                                       local_tz=True))
        self.assertEqual(milliseconds,
                         to_milliseconds(base_time, local_tz=False))
        self.assertEqual(milliseconds,
                         to_milliseconds(base_time, local_tz='Etc/GMT-1'))
        self.assertEqual(
            milliseconds,
            to_milliseconds(base_time, local_tz=pytz.timezone('Etc/GMT-1')))
Пример #3
0
    def testBuilders(self):
        expr = make_list(1, 2, 3, 4)
        self.assertIsInstance(expr, ListBuilder)
        self.assertIsInstance(expr, ListScalar)
        self.assertEqual(expr.dtype, validate_data_type('list<int32>'))

        expr = make_list(1, 2, 3, self.expr.id)
        self.assertIsInstance(expr, ListBuilder)
        self.assertIsInstance(expr, ListSequenceExpr)
        self.assertEqual(expr.dtype, validate_data_type('list<int64>'))

        self.assertRaises(TypeError, make_list, 1, 2, 'str', type='int32')
        self.assertRaises(TypeError, make_list, 1, 2, 'str')
        expr = make_list(1, 2, 3, 4, type='int64')
        self.assertEqual(expr.dtype, validate_data_type('list<int64>'))
        expr = make_list(1.1, 2.2, 3.3, 4.4)
        self.assertEqual(expr.dtype, validate_data_type('list<float64>'))
        expr = make_list(1, 2, 3, 65535)
        self.assertEqual(expr.dtype, validate_data_type('list<int32>'))
        expr = make_list(1, 2, 3, compat.long_type(12345678910))
        self.assertEqual(expr.dtype, validate_data_type('list<int64>'))
        expr = make_list(1, 2, 3, 3.5)
        self.assertEqual(expr.dtype, validate_data_type('list<float64>'))

        self.assertRaises(ValueError, make_dict, 1, 2, 3)

        expr = make_dict(1, 2, 3, 4)
        self.assertIsInstance(expr, DictBuilder)
        self.assertIsInstance(expr, DictScalar)
        self.assertEqual(expr.dtype, validate_data_type('dict<int32,int32>'))

        expr = make_dict(1, 2, 3, 4, key_type='int16', value_type='int64')
        self.assertIsInstance(expr, DictBuilder)
        self.assertIsInstance(expr, DictScalar)
        self.assertEqual(expr.dtype, validate_data_type('dict<int16,int64>'))

        expr = make_dict(1, 2, 3, self.expr.id)
        self.assertIsInstance(expr, DictBuilder)
        self.assertIsInstance(expr, DictSequenceExpr)
        self.assertEqual(expr.dtype, validate_data_type('dict<int32,int64>'))