-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add process extenstion to use Procss.clock_gettime with timecop
Signed-off-by: Yuta Iwama <[email protected]>
- Loading branch information
Showing
2 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'timecop' | ||
|
||
module Process | ||
class << self | ||
# this can be used with timecop | ||
def clock_gettime(clock_id, unit = :float_second) | ||
if Process::CLOCK_REALTIME | ||
t = Time.now | ||
|
||
case unit | ||
when :float_second | ||
t.to_i + t.nsec / 1_000_000_000.0 | ||
when :float_millisecond | ||
t.to_i * 1_000 + t.nsec / 1_000_000.0 | ||
when :float_microsecond | ||
t.to_i * 1_000_000 + t.nsec / 1_000.0 | ||
when :second | ||
t.to_i | ||
when :millisecond | ||
t.to_i * 1000 + t.nsec / 1_000_000 | ||
when :microsecond | ||
t.to_i * 1_000_000 + t.nsec / 1_000 | ||
when :nanosecond | ||
t.to_i * 1_000_000_000 + t.nsec | ||
end | ||
else | ||
# TODO | ||
super | ||
end | ||
end | ||
end | ||
end |