Python DateTime object accepts seconds as input but returns milliseconds as output, so the output cannot be reused as input

Description

The Python implementation of the DateTime database type accepts either an integer timestamp in seconds or a datetime object in the "to_python" method, but the "to_database" method returns milliseconds, so the output of the object cannot be used as input to create another object.

For example, if I create a Model object and extract the field values from it to be sent over Kafka to another process, the receiving process cannot re-instantiate the object because the DateTime fields in the Model were output as milliseconds which the object does not accept as input.

One possible solution:

def to_python(self, value):
if value is None:
return
if isinstance(value, datetime):
if DateTime.truncate_microseconds:
us = value.microsecond
truncated_us = us // 1000 * 1000
return value - timedelta(microseconds=us - truncated_us)
else:
return value
elif isinstance(value, date):
return datetime(*(value.timetuple()[:6]))
try:
return datetime.utcfromtimestamp(value)
except ValueError:
return datetime.utcfromtimestamp(value / 1000)

Environment

None

Pull Requests

None

Assignee

Unassigned

Reporter

Bob Haddleton

Fix versions

None

Labels

None

Reproduced in

None

PM Priority

None

External issue ID

None

Doc Impact

None

Reviewer

None

Size

None

Pull Request

None

Affects versions

Priority

Major
Configure