def to_python(self, value):
     if value is None:
         return
     if isinstance(value, util.Date):
         return value
     if isinstance(value, datetime):
         value = value.date()
     return util.Date(value)
    def to_database(self, value):
        if value is None:
            return

        # need to translate to int version because some dates are not representable in
        # string form (datetime limitation)
        d = value if isinstance(value, util.Date) else util.Date(value)
        return d.days_from_epoch + SimpleDateType.EPOCH_OFFSET_DAYS
    def setUpClass(cls):
        if PROTOCOL_VERSION < 4 or CASSANDRA_VERSION < "3.0":
            return

        cls.db_klass, cls.python_klass = UserDefinedType, User
        cls.first_value = User(
            age=1,
            date_param=datetime.utcnow(),
            map_param={
                1: time(2, 12, 7, 50),
                2: util.Time(time(2, 12, 7, 49))
            },
            list_param=[datetime(1, 1, 2),
                        datetime(1, 1, 3)],
            set_param=set((datetime(1, 1, 3), util.Date(datetime(1, 1, 1)))),
            tuple_param=(datetime(1, 1, 3), 2, False, 1, 2.324, uuid4()))

        cls.second_value = User(
            age=1,
            date_param=datetime.utcnow(),
            map_param={
                1: time(2, 12, 7, 50),
                2: util.Time(time(2, 12, 7, 49))
            },
            list_param=[datetime(1, 1, 2),
                        datetime(1, 2, 3)],
            set_param=None,
            tuple_param=(datetime(1, 1, 2), 2, False, 1, 2.324, uuid4()))

        cls.third_value = User(
            age=2,
            date_param=None,
            map_param={
                1: time(2, 12, 7, 51),
                2: util.Time(time(2, 12, 7, 49))
            },
            list_param=[datetime(1, 1, 2),
                        datetime(1, 1, 4)],
            set_param=set((datetime(1, 1, 3), util.Date(datetime(1, 1, 2)))),
            tuple_param=(None, 3, False, None, 2.3214, uuid4()))

        cls.model_class = UserModel
        sync_table(cls.model_class)
Beispiel #4
0
 def serialize(val, protocol_version):
     try:
         days = val.days_from_epoch
     except AttributeError:
         if isinstance(val, six.integer_types):
             # the DB wants offset int values, but util.Date init takes days from epoch
             # here we assume int values are offset, as they would appear in CQL
             # short circuit to avoid subtracting just to add offset
             return uint32_pack(val)
         days = util.Date(val).days_from_epoch
     return uint32_pack(days + SimpleDateType.EPOCH_OFFSET_DAYS)
Beispiel #5
0
 def deserialize(byts, protocol_version):
     days = uint32_unpack(byts) - SimpleDateType.EPOCH_OFFSET_DAYS
     return util.Date(days)
 def test_date_io_using_datetime(self):
     now = datetime.utcnow()
     self.DateTest.objects.create(test_id=0, created_at=now)
     result = self.DateTest.objects(test_id=0).first()
     self.assertIsInstance(result.created_at, util.Date)
     self.assertEqual(result.created_at, util.Date(now))
 def test_date_io(self):
     today = date.today()
     self.DateTest.objects.create(test_id=0, created_at=today)
     result = self.DateTest.objects(test_id=0).first()
     self.assertEqual(result.created_at, util.Date(today))
    def setUpClass(cls):
        cls.db_klass, cls.python_klass = (Date, util.Date)

        cls.first_value, cls.second_value, cls.third_value = (
            datetime.utcnow(), util.Date(datetime(1, 1, 1)), datetime(1, 1, 2))
        super(TestDate, cls).setUpClass()