-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add Time.unix_ns
and #to_unix_ns
#13359
Conversation
Resolves #11061 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My main concern is that a 64-bit value for nanoseconds can only represent about 300 years +/- the epoch. So it falls short of representing the full range of Time
(years 1..9999
).
Probably in practice this isn't very relevant. If you're using the unix nanosecond API it's most likely for contemporary timestamps, not far in the future or history.
It would still be nice and easy to remove the limitation on the constructor and allow bigger number types as well. (drop the .to_i
).
We could consider changing the return type of #to_unix_ns
to Int128
as well, but I'm not sure that makes sense.
Time.unix_ns
and #to_unix_ns
src/time.cr
Outdated
# time.to_unix_ns # => 1452567845678910123 | ||
# ``` | ||
def to_unix_ns : Int64 | ||
(to_unix * NANOSECONDS_PER_SECOND) + nanosecond |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This causes an OverflowError
for Time.unix_ns(Int64::MIN).to_unix_ns
, because nanosecond > 0
implies to_unix * NANOSECONDS_PER_SECOND < Int64::MIN
.
This isn't a problem for to_unix_ms
because Int64
's range far exceeds the representable range of milliseconds (the earliest being Time.unix_ms(-62135596800000)
).
@garymardell Would you mind adressing the review comments? |
Sorry for the delay, fell off my radar. I have pushed 2 changes:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is the first instance of a Int128 return type in a non-numeric API. But it's probably fine?
That would be my only concern for other reviewers to consider.
I am currently working on an application that requires nanosecond time precision (telemetry data). I receive the number of nanoseconds since the unix epoch. There are already convenience methods when the value is milliseconds (
unix_ms
andto_unix_ms
) however as far as I can tell there is nothing for nanoseconds despiteTime
supporting the precision.In my application I have patched in these methods to
Time
. These methods might be too niche for being included in the standard library though.