Skip to content

Commit 131f086

Browse files
committed
Add support for Date and DateNano types #6
1 parent e19c686 commit 131f086

File tree

8 files changed

+29
-16
lines changed

8 files changed

+29
-16
lines changed

example/__main__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
from cmd import Cmd
22
import objectbox
3-
import datetime
3+
import time
44
from example.model import *
55

66

77
# objectbox expects date timestamp in milliseconds since UNIX epoch
88
def now_ms() -> int:
9-
seconds: float = datetime.datetime.utcnow().timestamp()
10-
return round(seconds * 1000)
9+
return time.time_ns() / 1000000
1110

1211

1312
def format_date(timestamp_ms: int) -> str:
14-
return "" if timestamp_ms == 0 else str(datetime.datetime.fromtimestamp(timestamp_ms / 1000))
13+
return "" if timestamp_ms == 0 else time.ctime(timestamp_ms / 1000)
1514

1615

1716
class TasklistCmd(Cmd):

example/model.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ class Task:
66
id = Id(id=1, uid=1001)
77
text = Property(str, id=2, uid=1002)
88

9-
# TODO property type DATE
10-
date_created = Property(int, id=3, uid=1003)
11-
date_finished = Property(int, id=4, uid=1004)
9+
date_created = Property(int, type=PropertyType.date, id=3, uid=1003)
10+
date_finished = Property(int, type=PropertyType.date, id=4, uid=1004)
1211

1312

1413
def get_objectbox_model():

objectbox/c.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ def c_voidp_as_bytes(voidp, size):
394394
OBXPropertyType_String = 9
395395
OBXPropertyType_Date = 10
396396
OBXPropertyType_Relation = 11
397+
OBXPropertyType_DateNano = 12
397398
OBXPropertyType_ByteVector = 23
398399
OBXPropertyType_IntVector = 26
399400
OBXPropertyType_LongVector = 27

objectbox/model/entity.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import flatbuffers
1717
import numpy as np
18+
from math import floor
1819
from objectbox.c import *
1920
from objectbox.model.properties import Property
2021

@@ -132,6 +133,8 @@ def marshal(self, object, id: int) -> bytearray:
132133
else:
133134
val = id if prop == self.id_property else self.get_value(
134135
object, prop)
136+
if prop._ob_type == OBXPropertyType_Date or prop._ob_type == OBXPropertyType_DateNano:
137+
val = floor(val) # use floor to allow for float types
135138
builder.Prepend(prop._fb_type, val)
136139

137140
builder.Slot(prop._fb_slot)

objectbox/model/properties.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class PropertyType(IntEnum):
2929
float = OBXPropertyType_Float
3030
double = OBXPropertyType_Double
3131
string = OBXPropertyType_String
32-
# date = OBXPropertyType_Date
32+
date = OBXPropertyType_Date
33+
dateNano = OBXPropertyType_DateNano
3334
# relation = OBXPropertyType_Relation
3435
byteVector = OBXPropertyType_ByteVector
3536
intVector = OBXPropertyType_IntVector
@@ -49,7 +50,8 @@ class PropertyType(IntEnum):
4950
PropertyType.float: flatbuffers.number_types.Float32Flags,
5051
PropertyType.double: flatbuffers.number_types.Float64Flags,
5152
PropertyType.string: flatbuffers.number_types.UOffsetTFlags,
52-
# PropertyType.date: flatbuffers.number_types.Int64Flags,
53+
PropertyType.date: flatbuffers.number_types.Int64Flags,
54+
PropertyType.dateNano: flatbuffers.number_types.Int64Flags,
5355
# PropertyType.relation: flatbuffers.number_types.Int64Flags,
5456
PropertyType.byteVector: flatbuffers.number_types.UOffsetTFlags,
5557
PropertyType.intVector: flatbuffers.number_types.UOffsetTFlags,

tests/common.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def autocleanup():
2424
def load_empty_test_objectbox(name: str = "") -> objectbox.ObjectBox:
2525
model = objectbox.Model()
2626
from objectbox.model import IdUid
27-
model.entity(TestEntity, last_property_id=IdUid(18, 1018))
27+
model.entity(TestEntity, last_property_id=IdUid(20, 1020))
2828
model.last_entity_id = IdUid(1, 1)
2929

3030
db_name = test_dir if len(name) == 0 else test_dir + "/" + name
@@ -40,8 +40,8 @@ def assert_equal_prop_vector(actual, expected, default):
4040
assert (actual == np.array(expected)).all() or (isinstance(
4141
expected, objectbox.model.Property) and actual == default)
4242

43-
# compare approx values for list
44-
def assert_equal_prop_list(actual, expected, default):
43+
# compare approx values
44+
def assert_equal_prop_approx(actual, expected, default):
4545
assert pytest.approx(actual) == expected or (isinstance(
4646
expected, objectbox.model.Property) and actual == default)
4747

@@ -59,7 +59,9 @@ def assert_equal(actual: TestEntity, expected: TestEntity):
5959
assert_equal_prop_vector(actual.longs, expected.longs, np.array([]))
6060
assert_equal_prop_vector(actual.floats, expected.floats, np.array([]))
6161
assert_equal_prop_vector(actual.doubles, expected.doubles, np.array([]))
62-
assert_equal_prop_list(actual.ints_list, expected.ints_list, [])
63-
assert_equal_prop_list(actual.longs_list, expected.longs_list, [])
64-
assert_equal_prop_list(actual.floats_list, expected.floats_list, [])
65-
assert_equal_prop_list(actual.doubles_list, expected.doubles_list, [])
62+
assert_equal_prop_approx(actual.ints_list, expected.ints_list, [])
63+
assert_equal_prop_approx(actual.longs_list, expected.longs_list, [])
64+
assert_equal_prop_approx(actual.floats_list, expected.floats_list, [])
65+
assert_equal_prop_approx(actual.doubles_list, expected.doubles_list, [])
66+
assert_equal_prop_approx(actual.date, expected.date, 0)
67+
assert_equal_prop(actual.date_nano, expected.date_nano, 0)

tests/model.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class TestEntity:
2222
longs_list = Property(list, type=PropertyType.longVector, id=16, uid=1016)
2323
floats_list = Property(list, type=PropertyType.floatVector, id=17, uid=1017)
2424
doubles_list = Property(list, type=PropertyType.doubleVector, id=18, uid=1018)
25+
date = Property(int, type=PropertyType.date, id=19, uid=1019)
26+
date_nano = Property(int, type=PropertyType.dateNano, id=20, uid=1020)
2527
transient = "" # not "Property" so it's not stored
2628

2729
def __init__(self, string: str = ""):

tests/test_box.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44
from tests.model import TestEntity
55
from tests.common import autocleanup, load_empty_test_objectbox, assert_equal
6+
import time
67

78

89
def test_box_basics():
@@ -38,6 +39,8 @@ def test_box_basics():
3839
object.longs_list = [4568, 8714, 1234, 5678, 9012240941]
3940
object.floats_list = [0.11, 1.22, 2.33, 3.44, 4.5595]
4041
object.doubles_list = [99.1999, 88.2888, 77.3777, 66.4666, 55.6597555]
42+
object.date = time.time() * 1000 # milliseconds since UNIX epoch
43+
object.date_nano = time.time_ns() # nanoseconds since UNIX epoch
4144
object.transient = "abcd"
4245

4346
id = box.put(object)
@@ -54,6 +57,8 @@ def test_box_basics():
5457

5558
# update
5659
object.str = "bar"
60+
object.date = time.time_ns() / 1000000 # check that date can also be int
61+
object.date_nano = time.time() * 1000000000 # check that date_nano can also be float
5762
id = box.put(object)
5863
assert id == 5
5964

0 commit comments

Comments
 (0)