Skip to content
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

Avoid using floating points during timestamp-datetime conversions #591

Merged
merged 4 commits into from
Apr 19, 2024

Conversation

hakanakyurek
Copy link
Contributor

Right now, when we convert timestamp to datetime or datetime to timestamp, the library uses floating points to do the conversion. This introduces losses during the conversion as the datetime and timestamp datatypes do not support the same precision(nanoseconds).

For instance if we have a timestamp such as:
2024-04-16 08:43:09.420317+00:00
with the current implementation the nanoseconds in the resulting Timestamp object has 420316934 while it should be 420317000 instead.

or in this timestamp:
2024-04-16 08:43:09.400000+00:00
it has 400000095 nanoseconds instead of 400000000.

@hakanakyurek hakanakyurek changed the title Avoid using floaring points during timestamp-datetime conversions Avoid using floating points during timestamp-datetime conversions Apr 18, 2024
Copy link
Contributor

@ThomasWaldmann ThomasWaldmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a few nitpicks.

I assume that these tests fail without your changes?

msgpack/ext.py Outdated

@staticmethod
def from_datetime(dt):
"""Create a Timestamp from datetime with tzinfo.

:rtype: Timestamp
"""
return Timestamp.from_unix(dt.timestamp())
return Timestamp(seconds=int(dt.timestamp() // 1), nanoseconds=dt.microsecond * 10**3)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That x // 1 feels strange.

Considering you use int() after that anyway, why do you need that?

Also: just use 1000 instead of 10**3?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That x // 1 feels strange.

Considering you use int() after that anyway, why do you need that?

Yes, i overlooked that part completely, we dont need the floor division at all. I'll remove it.

Also: just use 1000 instead of 10**3?

we can of course use 1000 instead, its just that in the code that i checked, i either saw "10**x" or "1ex" and wanted follow that convention.

msgpack/ext.py Outdated
@@ -157,12 +157,14 @@ def to_datetime(self):
:rtype: `datetime.datetime`
"""
utc = datetime.timezone.utc
return datetime.datetime.fromtimestamp(0, utc) + datetime.timedelta(seconds=self.to_unix())
return datetime.datetime.fromtimestamp(0, utc) + datetime.timedelta(
seconds=self.seconds, microseconds=round(self.nanoseconds / 1e3)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use 1000 instead of 1e3?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer // over round.

Consider "12:34:56.78". It is "12:34" or "12:34:56", not "12:35" nor "12:34:57".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use 1000 instead of 1e3?

same as the previous comment, I just though "10**x" or "1ex" was the convention that was being used.

I prefer // over round.

Consider "12:34:56.78". It is "12:34" or "12:34:56", not "12:35" nor "12:34:57".

I'm not actually knowledgeable about whether there should be a round or not. The idea I had with the round is when working with a timestamp with nanosecond precision, lets say, "100,000,900" nanoseconds, I thought it should round to "100,001" microseconds, whereas "100,000,100" nanoseconds should be "100,000" microseconds.

With the case "12:34:56.78", I'm not sure whether it should "12:34:56" or "12:34:57", if rounding isn't the norm in these cases, we should definitely remove it and the tests related to it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea I had with the round is when working with a timestamp.

The year of 2024-10-01 is 2025?
The day of 2024-10-01 15:00:00 is 2024-10-02?
The hour of 15:34 is 16?

Despite what you explain it in natural language, "round down" is consistent in programming.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/time/Instant.html#toEpochMilli()

If this instant has greater than millisecond precision, then the conversion will drop any excess precision information as though the amount in nanoseconds was subject to integer division by one million.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm i see, thanks a lot it makes more sense. Then i'll remove the rounding and tests related to it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2023-12-31 23:59:59.999999500 -- year 2023, date 2023-12-31.

After round half by micros:

2024-01-01 00:00:00.000000 -- year 2024, date 2024-01-01.

This is why most languages uses consistent "round down" for subseconds.
Consistency is important to avoid bugs. Every programmer should love consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup of course, I didn't look at it like that when I was proposing the change. Again it makes more sense to round down instead as it would be more consistant as you said. Thanks for the explanation!

t2 = Timestamp(1713256989, 420318123)
t3 = Timestamp(1713256989, 420318499)
t4 = Timestamp(1713256989, 420318501)
assert t2.to_datetime() == t3.to_datetime() != t4.to_datetime()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just using 2 asserts would be simpler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definitely, we dont necessarily need to compare with t2 now i think of it. But these were to test the rounding specifically, if rounding is wrong way to go, these tests arent necessarily needed imo.

@hakanakyurek
Copy link
Contributor Author

Exactly, the tests i added except line 100 should fail without the changes.

msgpack/ext.py Outdated
@@ -157,12 +157,14 @@ def to_datetime(self):
:rtype: `datetime.datetime`
"""
utc = datetime.timezone.utc
return datetime.datetime.fromtimestamp(0, utc) + datetime.timedelta(seconds=self.to_unix())
return datetime.datetime.fromtimestamp(0, utc) + datetime.timedelta(
seconds=self.seconds, microseconds=self.nanoseconds // 1e3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
seconds=self.seconds, microseconds=self.nanoseconds // 1e3
seconds=self.seconds, microseconds=self.nanoseconds // 1000

msgpack/ext.py Outdated

@staticmethod
def from_datetime(dt):
"""Create a Timestamp from datetime with tzinfo.

:rtype: Timestamp
"""
return Timestamp.from_unix(dt.timestamp())
return Timestamp(seconds=int(dt.timestamp()), nanoseconds=dt.microsecond * 10**3)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Timestamp(seconds=int(dt.timestamp()), nanoseconds=dt.microsecond * 10**3)
return Timestamp(seconds=int(dt.timestamp()), nanoseconds=dt.microsecond * 1000)

ts = datetime.datetime(2024, 4, 16, 8, 43, 9, 420317, tzinfo=utc)
ts2 = datetime.datetime(2024, 4, 16, 8, 43, 9, 420318, tzinfo=utc)

assert Timestamp.from_datetime(ts2).nanoseconds - Timestamp.from_datetime(ts).nanoseconds == 1e3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert Timestamp.from_datetime(ts2).nanoseconds - Timestamp.from_datetime(ts).nanoseconds == 1e3
assert Timestamp.from_datetime(ts2).nanoseconds - Timestamp.from_datetime(ts).nanoseconds == 1000

ts3 = datetime.datetime(2024, 4, 16, 8, 43, 9, 4256)
ts4 = datetime.datetime(2024, 4, 16, 8, 43, 9, 4257)
assert (
Timestamp.from_datetime(ts4).nanoseconds - Timestamp.from_datetime(ts3).nanoseconds == 1e3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Timestamp.from_datetime(ts4).nanoseconds - Timestamp.from_datetime(ts3).nanoseconds == 1e3
Timestamp.from_datetime(ts4).nanoseconds - Timestamp.from_datetime(ts3).nanoseconds == 1000

@methane methane merged commit e776722 into msgpack:main Apr 19, 2024
18 checks passed
@methane
Copy link
Member

methane commented Apr 19, 2024

thanks

@uyha uyha mentioned this pull request Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants