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

Add Time.unix_ns and #to_unix_ns #13359

Merged
merged 3 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions spec/std/time/time_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,32 @@ describe Time do
time.utc?.should be_true
end

describe ".unix_ns" do
it "supports Int64 values" do
nanoseconds = 1439404155001457425i64
time = Time.unix_ns(nanoseconds)
time.should eq(Time.utc(2015, 8, 12, 18, 29, 15, nanosecond: 1457425))
time.to_unix_ns.should eq(nanoseconds)
time.utc?.should be_true
end

it "supports maximum valid time" do
nanoseconds = Int128.new("253402300799999999999")
time = Time.unix_ns(nanoseconds)
time.should eq(Time.utc(9999, 12, 31, 23, 59, 59, nanosecond: 999999999))
time.to_unix_ns.should eq(nanoseconds)
time.utc?.should be_true
end

it "supports minimum valid time" do
nanoseconds = Int128.new("-62135596800000000000")
time = Time.unix_ns(nanoseconds)
time.should eq(Time.utc(1, 1, 1, 0, 0, 0, nanosecond: 0))
time.to_unix_ns.should eq(nanoseconds)
time.utc?.should be_true
end
end

describe ".local without arguments" do
it "current time is similar in different locations" do
(Time.local - Time.utc).should be_close(0.seconds, 1.second)
Expand Down
26 changes: 26 additions & 0 deletions src/time.cr
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,21 @@ struct Time
utc(seconds: seconds, nanoseconds: nanoseconds.to_i)
end

# Creates a new `Time` instance that corresponds to the number of
# *nanoseconds* elapsed since the Unix epoch (`1970-01-01 00:00:00.000000000 UTC`).
#
# The time zone is always UTC.
#
# ```
# time = Time.unix_ns(981173106789479273) # => 2001-02-03 04:05:06.789479273 UTC
# time.nanosecond # => 789479273
# ```
def self.unix_ns(nanoseconds : Int) : Time
seconds = UNIX_EPOCH.total_seconds + (nanoseconds // 1_000_000_000)
nanoseconds = nanoseconds % 1_000_000_000
utc(seconds: seconds, nanoseconds: nanoseconds.to_i)
end

# Creates a new `Time` instance with the same local date-time representation
# (wall clock) in a different *location*.
#
Expand Down Expand Up @@ -1254,6 +1269,17 @@ struct Time
to_unix * 1_000 + (nanosecond // NANOSECONDS_PER_MILLISECOND)
end

# Returns the number of nanoseconds since the Unix epoch
# (`1970-01-01 00:00:00.000000000 UTC`).
#
# ```
# time = Time.utc(2016, 1, 12, 3, 4, 5, nanosecond: 678_910_123)
# time.to_unix_ns # => 1452567845678910123
# ```
def to_unix_ns : Int128
(to_unix.to_i128 * NANOSECONDS_PER_SECOND) + nanosecond
end

# Returns the number of seconds since the Unix epoch
# (`1970-01-01 00:00:00 UTC`) as `Float64` with nanosecond precision.
#
Expand Down