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 Crystal::System::Time.ticks #14620

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions src/crystal/system/time.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module Crystal::System::Time
# since `0001-01-01 00:00:00`.
# def self.compute_utc_seconds_and_nanoseconds : {Int64, Int32}

# Returns the current time from the monotonic clock in `{seconds,
# nanoseconds}`.
# def self.monotonic : {Int64, Int32}

# Returns a list of paths where time zone data should be looked up.
Expand Down
19 changes: 9 additions & 10 deletions src/crystal/system/win32/time.cr
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,20 @@ module Crystal::System::Time
((filetime.dwHighDateTime.to_u64 << 32) | filetime.dwLowDateTime.to_u64).to_f64 / FILETIME_TICKS_PER_SECOND.to_f64
end

@@performance_frequency : Int64 = begin
ret = LibC.QueryPerformanceFrequency(out frequency)
if ret == 0
raise RuntimeError.from_winerror("QueryPerformanceFrequency")
end
@@performance_frequency : Int64?

frequency
private def self.performance_frequency
@@performance_frequency ||= begin
LibC.QueryPerformanceFrequency(out frequency)
frequency
end
ysbaddaden marked this conversation as resolved.
Show resolved Hide resolved
end

def self.monotonic : {Int64, Int32}
if LibC.QueryPerformanceCounter(out ticks) == 0
raise RuntimeError.from_winerror("QueryPerformanceCounter")
end
LibC.QueryPerformanceCounter(out ticks)
frequency = performance_frequency
{ticks // frequency, (ticks.remainder(frequency) * NANOSECONDS_PER_SECOND / frequency).to_i32}

{ticks // @@performance_frequency, (ticks.remainder(@@performance_frequency) * NANOSECONDS_PER_SECOND / @@performance_frequency).to_i32}
end

def self.load_localtime : ::Time::Location?
Expand Down