Skip to content

Commit

Permalink
[GR-19220] Implement Time#floor and #ceil (#2201)
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/2300
  • Loading branch information
eregon committed Jan 4, 2021
2 parents 5011dc1 + eba4899 commit 9d259b9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Compatibility:
* Add support for `Integer#[Range]` and `Integer#[start, length]` (#2182, @gogainda).
* Allow private calls with `self` as an explicit receiver (#2196, @wildmaples).
* Fixed `:perm` parameter for `File.write`.
* Implemented `Time#floor` and `#ceil` (#2201, @wildmaples).

Performance:

Expand Down
7 changes: 0 additions & 7 deletions spec/tags/core/time/ceil_tags.txt

This file was deleted.

5 changes: 0 additions & 5 deletions spec/tags/core/time/floor_tags.txt

This file was deleted.

18 changes: 18 additions & 0 deletions src/main/ruby/truffleruby/core/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,24 @@ def round(places = 0)
time + (rounded - original)
end

def floor(places = 0)
original = to_i + subsec.to_r
floored = original.floor(places)

time = Time.allocate
time.send(:initialize_copy, self)
time + (floored - original)
end

def ceil(places = 0)
original = to_i + subsec.to_r
ceiled = original.ceil(places)

time = Time.allocate
time.send(:initialize_copy, self)
time + (ceiled - original)
end

def self._load(data)
# TODO: doesn't load ivars
raise TypeError, 'marshaled time format differ' unless data.bytesize == 8
Expand Down

0 comments on commit 9d259b9

Please sign in to comment.