parent
4c2b83d9ca
commit
576db9ca88
@ -232,12 +232,16 @@ class DateTimeField(BaseField):
|
|||||||
"""A datetime field.
|
"""A datetime field.
|
||||||
|
|
||||||
Note: Microseconds are rounded to the nearest millisecond.
|
Note: Microseconds are rounded to the nearest millisecond.
|
||||||
Pre UTC microsecond support is effecively broken see
|
Pre UTC microsecond support is effecively broken.
|
||||||
`tests.field.test_datetime` for more information.
|
Use :class:`~mongoengine.fields.ComplexDateTimeField` if you
|
||||||
|
need accurate microsecond support.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def validate(self, value):
|
def validate(self, value):
|
||||||
assert isinstance(value, datetime.datetime)
|
assert isinstance(value, (datetime.datetime, datetime.date))
|
||||||
|
|
||||||
|
def to_mongo(self, value):
|
||||||
|
return self.prepare_query_value(None, value)
|
||||||
|
|
||||||
def prepare_query_value(self, op, value):
|
def prepare_query_value(self, op, value):
|
||||||
if value is None:
|
if value is None:
|
||||||
|
@ -182,6 +182,9 @@ class FieldTest(unittest.TestCase):
|
|||||||
log.time = datetime.datetime.now()
|
log.time = datetime.datetime.now()
|
||||||
log.validate()
|
log.validate()
|
||||||
|
|
||||||
|
log.time = datetime.date.today()
|
||||||
|
log.validate()
|
||||||
|
|
||||||
log.time = -1
|
log.time = -1
|
||||||
self.assertRaises(ValidationError, log.validate)
|
self.assertRaises(ValidationError, log.validate)
|
||||||
log.time = '1pm'
|
log.time = '1pm'
|
||||||
@ -199,6 +202,15 @@ class FieldTest(unittest.TestCase):
|
|||||||
|
|
||||||
LogEntry.drop_collection()
|
LogEntry.drop_collection()
|
||||||
|
|
||||||
|
# Test can save dates
|
||||||
|
log = LogEntry()
|
||||||
|
log.date = datetime.date.today()
|
||||||
|
log.save()
|
||||||
|
log.reload()
|
||||||
|
self.assertEquals(log.date.date(), datetime.date.today())
|
||||||
|
|
||||||
|
LogEntry.drop_collection()
|
||||||
|
|
||||||
# Post UTC - microseconds are rounded (down) nearest millisecond and dropped
|
# Post UTC - microseconds are rounded (down) nearest millisecond and dropped
|
||||||
d1 = datetime.datetime(1970, 01, 01, 00, 00, 01, 999)
|
d1 = datetime.datetime(1970, 01, 01, 00, 00, 01, 999)
|
||||||
d2 = datetime.datetime(1970, 01, 01, 00, 00, 01)
|
d2 = datetime.datetime(1970, 01, 01, 00, 00, 01)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user