question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TimeDelta serialization precision

See original GitHub issue

Hi 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
deckar01commented, Aug 24, 2021

This seems like a better option than going back to a long if chain that explicitly multiplies and adds the integer values together.

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’s timedelta has a private method named _to_microseconds() with a simple formula that we can borrow.

0reactions
yarsanichcommented, Dec 28, 2021

@deckar01 thanks for fixing this!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found