diff --git a/CHANGELOG.md b/CHANGELOG.md index 11b597e2878b..257f05f8461a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: diff --git a/spec/tags/core/time/ceil_tags.txt b/spec/tags/core/time/ceil_tags.txt deleted file mode 100644 index dd5276a2473b..000000000000 --- a/spec/tags/core/time/ceil_tags.txt +++ /dev/null @@ -1,7 +0,0 @@ -fails:Time#ceil defaults to ceiling to 0 places -fails:Time#ceil ceils to 0 decimal places with an explicit argument -fails:Time#ceil ceils to 2 decimal places with an explicit argument -fails:Time#ceil ceils to 4 decimal places with an explicit argument -fails:Time#ceil ceils to 7 decimal places with an explicit argument -fails:Time#ceil returns an instance of Time, even if #ceil is called on a subclass -fails:Time#ceil copies own timezone to the returning value diff --git a/spec/tags/core/time/floor_tags.txt b/spec/tags/core/time/floor_tags.txt deleted file mode 100644 index ebe945111e12..000000000000 --- a/spec/tags/core/time/floor_tags.txt +++ /dev/null @@ -1,5 +0,0 @@ -fails:Time#floor defaults to flooring to 0 places -fails:Time#floor floors to 0 decimal places with an explicit argument -fails:Time#floor floors to 7 decimal places with an explicit argument -fails:Time#floor returns an instance of Time, even if #floor is called on a subclass -fails:Time#floor copies own timezone to the returning value diff --git a/src/main/ruby/truffleruby/core/time.rb b/src/main/ruby/truffleruby/core/time.rb index 650436b97aa1..de6521ebc643 100644 --- a/src/main/ruby/truffleruby/core/time.rb +++ b/src/main/ruby/truffleruby/core/time.rb @@ -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