Project

General

Profile

Actions

Feature #15772

closed

Proposal: Add Time#ceil

Added by osyo (manga osyo) over 5 years ago. Updated over 5 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:92306]

Description

Summary

proposal for a method that rounds up the decimal part of Time (nanosecond) to the specified digit.

Current status

  • Time#round and Time#floor (https://bugs.ruby-lang.org/issues/15653) are defined.
  • However, Time#ceil is not defined.
  • So, I would like to respond flexibly when I would like to control decimal precision.

Propose Time#ceil

Below is example code.

require 'time'

t = Time.utc(2010,3,30, 5,43,"25.0123456789".to_r)
p t.iso8601(10)          #=> "2010-03-30T05:43:25.0123456789Z"

p t.ceil(0).iso8601(10)  #=> "2010-03-30T05:43:26.0000000000Z"
p t.ceil(1).iso8601(10)  #=> "2010-03-30T05:43:25.1000000000Z"
p t.ceil(2).iso8601(10)  #=> "2010-03-30T05:43:25.0200000000Z"
p t.ceil(3).iso8601(10)  #=> "2010-03-30T05:43:25.0130000000Z"
p t.ceil(4).iso8601(10)  #=> "2010-03-30T05:43:25.0124000000Z"
p t.ceil(5).iso8601(10)  #=> "2010-03-30T05:43:25.0123500000Z"
p t.ceil(6).iso8601(10)  #=> "2010-03-30T05:43:25.0123460000Z"
p t.ceil(7).iso8601(10)  #=> "2010-03-30T05:43:25.0123457000Z"
p t.ceil(8).iso8601(10)  #=> "2010-03-30T05:43:25.0123456800Z"
p t.ceil(9).iso8601(10)  #=> "2010-03-30T05:43:25.0123456790Z"
p t.ceil(10).iso8601(10) #=> "2010-03-30T05:43:25.0123456790Z"

# default argument is 0
p t.ceil.iso8601(10)     #=> "2010-03-30T05:43:26.0000000000Z"

t = Time.utc(1999,12,31, 23,59,59)
p (t + 0.4).ceil.iso8601(3)    #=> "2000-01-01T00:00:00.000Z"
p (t + 0.49).ceil.iso8601(3)   #=> "2000-01-01T00:00:00.000Z"
p (t + 0.5).ceil.iso8601(3)    #=> "2000-01-01T00:00:00.000Z"
p (t + 1.4).ceil.iso8601(3)    #=> "2000-01-01T00:00:01.000Z"
p (t + 1.49).ceil.iso8601(3)   #=> "2000-01-01T00:00:01.000Z"
p (t + 1.5).ceil.iso8601(3)    #=> "2000-01-01T00:00:01.000Z"

t = Time.utc(1999,12,31, 23,59,59)
p (t + 0.123456789).ceil(4).iso8601(6)  #=> "1999-12-31T23:59:59.123500Z"

pull request : https://github.com/ruby/ruby/pull/2133

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0