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')
Beispiel #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')
Beispiel #3
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))
Beispiel #4
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()
Beispiel #5
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))
Beispiel #6
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))
    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))
Beispiel #8
0
 def test_DateType(self):
     now = datetime.datetime.now()
     date_type = DateType(now)
     self.assertEqual(date_type.my_timestamp(), now)
     self.assertRaises(ValueError, date_type.interpret_datestring,
                       'fakestring')
Beispiel #9
0
 def test_date(self):
     # separate test because it will deserialize as datetime
     self.assertEqual(DateType.from_binary(DateType.to_binary(date(2015, 11, 2), 1), 1), datetime(2015, 11, 2))
Beispiel #10
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')
 def test_date(self):
     # separate test because it will deserialize as datetime
     self.assertEqual(DateType.from_binary(DateType.to_binary(date(2015, 11, 2), 1), 1), datetime(2015, 11, 2))
Beispiel #12
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')
Beispiel #13
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))
 def test_DateType(self):
     now = datetime.datetime.now()
     date_type = DateType(now)
     self.assertEqual(date_type.my_timestamp(), now)
     self.assertRaises(ValueError, date_type.interpret_datestring, 'fakestring')