예제 #1
0
    def test_datetype(self):
        now_time_seconds = time.time()
        now_datetime = datetime.datetime.utcfromtimestamp(now_time_seconds)

        # Cassandra timestamps in millis
        now_timestamp = now_time_seconds * 1e3

        # same results serialized
        self.assertEqual(DateType.serialize(now_datetime, 0),
                         DateType.serialize(now_timestamp, 0))

        # deserialize
        # epoc
        expected = 0
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0),
                         datetime.datetime.utcfromtimestamp(expected))

        # beyond 32b
        expected = 2**33
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0),
                         datetime.datetime(2242, 3, 16, 12, 56, 32))

        # less than epoc (PYTHON-119)
        expected = -770172256
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0),
                         datetime.datetime(1945, 8, 5, 23, 15, 44))

        # work around rounding difference among Python versions (PYTHON-230)
        expected = 1424817268.274
        self.assertEqual(
            DateType.deserialize(int64_pack(int(1000 * expected)), 0),
            datetime.datetime(2015, 2, 24, 22, 34, 28, 274000))
예제 #2
0
    def test_datetype(self):
        now_timestamp = time.time()
        now_datetime = datetime.datetime.utcfromtimestamp(now_timestamp)

        # same results serialized (must scale the timestamp to milliseconds)
        self.assertEqual(DateType.serialize(now_datetime, 0), DateType.serialize(now_timestamp * 1e3, 0))

        # from timestamp
        date_type = DateType(now_timestamp)
        self.assertEqual(date_type.my_timestamp(), now_timestamp)

        # from datetime object
        date_type = DateType(now_datetime)
        self.assertEqual(date_type.my_timestamp(), now_datetime)

        # deserialize
        # epoc
        expected = 0
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected))

        # beyond 32b
        expected = 2**33
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected))

        # less than epoc (PYTHON-119)
        expected = -770172256
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime(1945, 8, 5, 23, 15, 44))

        self.assertRaises(ValueError, date_type.interpret_datestring, 'fakestring')
예제 #3
0
    def test_datetype(self):
        now_timestamp = time.time()
        now_datetime = datetime.datetime.utcfromtimestamp(now_timestamp)

        # same results serialized
        # (this could change if we follow up on the timestamp multiplication warning in DateType.serialize)
        self.assertEqual(DateType.serialize(now_datetime, 0),
                         DateType.serialize(now_timestamp, 0))

        # from timestamp
        date_type = DateType(now_timestamp)
        self.assertEqual(date_type.my_timestamp(), now_timestamp)

        # from datetime object
        date_type = DateType(now_datetime)
        self.assertEqual(date_type.my_timestamp(), now_datetime)

        # deserialize
        # epoc
        expected = 0
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0),
                         datetime.datetime.utcfromtimestamp(expected))

        # beyond 32b
        expected = 2**33
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0),
                         datetime.datetime.utcfromtimestamp(expected))

        # less than epoc (PYTHON-119)
        expected = -770172256
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0),
                         datetime.datetime(1945, 8, 5, 23, 15, 44))

        self.assertRaises(ValueError, date_type.interpret_datestring,
                          'fakestring')
예제 #4
0
    def test_datetype(self):
        now_time_seconds = time.time()
        now_datetime = datetime.datetime.utcfromtimestamp(now_time_seconds)

        # Cassandra timestamps in millis
        now_timestamp = now_time_seconds * 1e3

        # same results serialized
        self.assertEqual(DateType.serialize(now_datetime, 0), DateType.serialize(now_timestamp, 0))

        # deserialize
        # epoc
        expected = 0
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected))

        # beyond 32b
        expected = 2 ** 33
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime(2242, 3, 16, 12, 56, 32))

        # less than epoc (PYTHON-119)
        expected = -770172256
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime(1945, 8, 5, 23, 15, 44))

        # work around rounding difference among Python versions (PYTHON-230)
        expected = 1424817268.274
        self.assertEqual(DateType.deserialize(int64_pack(int(1000 * expected)), 0), datetime.datetime(2015, 2, 24, 22, 34, 28, 274000))

        # Large date overflow (PYTHON-452)
        expected = 2177403010.123
        self.assertEqual(DateType.deserialize(int64_pack(int(1000 * expected)), 0), datetime.datetime(2038, 12, 31, 10, 10, 10, 123000))
예제 #5
0
    def test_datetype(self):
        now_timestamp = time.time()
        now_datetime = datetime.datetime.utcfromtimestamp(now_timestamp)

        # same results serialized
        # (this could change if we follow up on the timestamp multiplication warning in DateType.serialize)
        self.assertEqual(DateType.serialize(now_datetime, 0), DateType.serialize(now_timestamp, 0))

        # from timestamp
        date_type = DateType(now_timestamp)
        self.assertEqual(date_type.my_timestamp(), now_timestamp)

        # from datetime object
        date_type = DateType(now_datetime)
        self.assertEqual(date_type.my_timestamp(), now_datetime)

        # deserialize
        # epoc
        expected = 0
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected))

        # beyond 32b
        expected = 2**33
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected))

        # less than epoc (PYTHON-119)
        expected = -770172256
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime(1945, 8, 5, 23, 15, 44))

        self.assertRaises(ValueError, date_type.interpret_datestring, 'fakestring')
예제 #6
0
    def test_datetype(self):
        now_time_seconds = time.time()
        now_datetime = datetime.datetime.utcfromtimestamp(now_time_seconds)

        # Cassandra timestamps in millis
        now_timestamp = now_time_seconds * 1e3

        # same results serialized
        self.assertEqual(DateType.serialize(now_datetime, 0), DateType.serialize(now_timestamp, 0))

        # from timestamp
        date_type = DateType(now_timestamp)
        self.assertEqual(date_type.my_timestamp(), now_timestamp)

        # from datetime object
        date_type = DateType(now_datetime)
        self.assertEqual(date_type.my_timestamp(), now_datetime)

        # deserialize
        # epoc
        expected = 0
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected))

        # beyond 32b
        expected = 2**33
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected))

        # less than epoc (PYTHON-119)
        expected = -770172256
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime(1945, 8, 5, 23, 15, 44))

        self.assertRaises(ValueError, date_type.interpret_datestring, 'fakestring')
예제 #7
0
 def to_python(self, value):
     if value is None: return
     if isinstance(value, datetime):
         return value.date()
     elif isinstance(value, date):
         return value
     try:
         return datetime.utcfromtimestamp(value).date()
     except TypeError:
         return datetime.utcfromtimestamp(DateType.deserialize(value)).date()
예제 #8
0
 def to_python(self, value):
     if value is None: return
     if isinstance(value, datetime):
         return value
     elif isinstance(value, date):
         return datetime(*(value.timetuple()[:6]))
     try:
         return datetime.utcfromtimestamp(value)
     except TypeError:
         return datetime.utcfromtimestamp(DateType.deserialize(value))
예제 #9
0
    def test_datetype(self):
        now_time_seconds = time.time()
        now_datetime = datetime.datetime.utcfromtimestamp(now_time_seconds)

        # Cassandra timestamps in millis
        now_timestamp = now_time_seconds * 1e3

        # same results serialized
        self.assertEqual(DateType.serialize(now_datetime, 0),
                         DateType.serialize(now_timestamp, 0))

        # from timestamp
        date_type = DateType(now_timestamp)
        self.assertEqual(date_type.my_timestamp(), now_timestamp)

        # from datetime object
        date_type = DateType(now_datetime)
        self.assertEqual(date_type.my_timestamp(), now_datetime)

        # deserialize
        # epoc
        expected = 0
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0),
                         datetime.datetime.utcfromtimestamp(expected))

        # beyond 32b
        expected = 2**33
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0),
                         datetime.datetime.utcfromtimestamp(expected))

        # less than epoc (PYTHON-119)
        expected = -770172256
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0),
                         datetime.datetime(1945, 8, 5, 23, 15, 44))

        self.assertRaises(ValueError, date_type.interpret_datestring,
                          'fakestring')

        # work around rounding difference among Python versions (PYTHON-230)
        expected = 1424817268.274
        self.assertEqual(
            DateType.deserialize(int64_pack(int(1000 * expected)), 0),
            datetime.datetime(2015, 2, 24, 22, 34, 28, 274000))
예제 #10
0
 def to_python(self, value):
     if value is None: return
     if isinstance(value, datetime):
         return value
     elif isinstance(value, date):
         return datetime(*(value.timetuple()[:6]))
     try:
         return datetime.utcfromtimestamp(value)
     except TypeError:
         return datetime.utcfromtimestamp(DateType.deserialize(value, 3))
예제 #11
0
    def test_datetype(self):
        now_time_seconds = time.time()
        now_datetime = datetime.datetime.utcfromtimestamp(now_time_seconds)

        # Cassandra timestamps in millis
        now_timestamp = now_time_seconds * 1e3

        # same results serialized
        self.assertEqual(DateType.serialize(now_datetime, 0), DateType.serialize(now_timestamp, 0))

        # from timestamp
        date_type = DateType(now_timestamp)
        self.assertEqual(date_type.my_timestamp(), now_timestamp)

        # from datetime object
        date_type = DateType(now_datetime)
        self.assertEqual(date_type.my_timestamp(), now_datetime)

        # deserialize
        # epoc
        expected = 0
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected))

        # beyond 32b
        expected = 2 ** 33
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected))

        # less than epoc (PYTHON-119)
        expected = -770172256
        self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime(1945, 8, 5, 23, 15, 44))

        self.assertRaises(ValueError, date_type.interpret_datestring, 'fakestring')

        # work around rounding difference among Python versions (PYTHON-230)
        expected = 1424817268.274
        self.assertEqual(DateType.deserialize(int64_pack(int(1000 * expected)), 0), datetime.datetime(2015, 2, 24, 22, 34, 28, 274000))