Skip to content

Add microseconds support for DateTimeField #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions couchdb/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,22 +474,32 @@ def _to_json(self, value):

class DateTimeField(Field):
"""Mapping field for storing date/time values.

>>> field = DateTimeField()
>>> field._to_python('2007-04-01T15:30:00Z')
datetime.datetime(2007, 4, 1, 15, 30)
>>> field._to_json(datetime(2007, 4, 1, 15, 30, 0, 9876))
>>> field._to_python('2007-04-01T15:30:00.009876Z')
datetime.datetime(2007, 4, 1, 15, 30, 0, 9876)
>>> field._to_json(datetime(2007, 4, 1, 15, 30, 0))
'2007-04-01T15:30:00Z'
>>> field._to_json(datetime(2007, 4, 1, 15, 30, 0, 9876))
'2007-04-01T15:30:00.009876Z'
>>> field._to_json(date(2007, 4, 1))
'2007-04-01T00:00:00Z'
"""

def _to_python(self, value):
if isinstance(value, util.strbase):
try:
value = value.split('.', 1)[0] # strip out microseconds
value = value.rstrip('Z') # remove timezone separator
value = datetime(*strptime(value, '%Y-%m-%dT%H:%M:%S')[:6])
split_value = value.split('.') # strip out microseconds
if len(split_value) == 1: # No microseconds provided
value = split_value[0]
value = value.rstrip('Z') #remove timezone separator
value = datetime.strptime(value, '%Y-%m-%dT%H:%M:%S')
else:
value = value.rstrip('Z')
value = datetime.strptime(value, '%Y-%m-%dT%H:%M:%S.%f')

except ValueError:
raise ValueError('Invalid ISO date/time %r' % value)
return value
Expand All @@ -499,12 +509,12 @@ def _to_json(self, value):
value = datetime.utcfromtimestamp(timegm(value))
elif not isinstance(value, datetime):
value = datetime.combine(value, time(0))
return value.replace(microsecond=0).isoformat() + 'Z'
return value.isoformat() + 'Z'


class TimeField(Field):
"""Mapping field for storing times.

>>> field = TimeField()
>>> field._to_python('15:30:00')
datetime.time(15, 30)
Expand Down
10 changes: 10 additions & 0 deletions couchdb/tests/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from couchdb import design, mapping
from couchdb.tests import testutil
from datetime import datetime

class DocumentTestCase(testutil.TempDatabaseMixin, unittest.TestCase):

Expand Down Expand Up @@ -83,6 +84,15 @@ def test_old_datetime(self):
dt = mapping.DateTimeField()
assert dt._to_python('1880-01-01T00:00:00Z')

def test_datetime_with_microseconds(self):
dt = mapping.DateTimeField()
assert dt._to_python('2016-06-09T21:21:49.739248Z')

def test_datetime_to_json(self):
dt = mapping.DateTimeField()
d = datetime.now()
assert dt._to_json(d)

def test_get_has_default(self):
doc = mapping.Document()
doc.get('foo')
Expand Down