TimeDelta serialization precision
See original GitHub issueHi there!
I just found quite strange behaviour of TimeDelta
field serialization
from marshmallow.fields import TimeDelta
from datetime import timedelta
td_field = TimeDelta(precision="milliseconds")
obj = dict()
obj["td_field"] = timedelta(milliseconds=345)
print(td_field.serialize("td_field", obj))
Output of this snippet is 344
, but it seems that 345
is correct.
Looks like a rounding issue here: https://github.com/marshmallow-code/marshmallow/blob/dev/src/marshmallow/fields.py#L1474
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Format timedelta to string - python - Stack Overflow
Here's what I'm trying to do: I have a list of objects and one of the members of the class of the object...
Read more >Fields — marshmallow 3.19.0 documentation
TimeDelta (precision, serialization_type, ...) A field that (de)serializes a datetime.timedelta object to an integer or float and vice versa ...
Read more >How to serialize a datetime object as JSON using Python?
It is easy to serialize a Python data structure as JSON, we just need to call the json.dumps method, but if our data...
Read more >time/time.h - chromium/src/base - Git at Google
`TimeDelta` represents a duration of time, internally represented in ... Use the helpers in //base/json/values_util.h when serializing `Time`.
Read more >How to work with dates and time with Python | Bloomberg LP
When working with timestamps, the best strategy is to use non DST-aware time zones (ideally UTC+00:00). Serializing your datetime objects. The day will...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I assumed the introduction of additional time resolutions in #691 corresponded to
timedelta
also splitting the times across these units internally, but it actually doesn’t. The intermediate units get packed into the original 3 integers (days, seconds, microseconds). Cpython’stimedelta
has a private method named_to_microseconds()
with a simple formula that we can borrow.@deckar01 thanks for fixing this!