Project

General

Profile

Feature #15772

Proposal: Add Time#ceil

Added by osyo (manga osyo) 8 months ago. Updated 7 months ago.

Status:
Closed
Priority:
Normal
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

History

Updated by matz (Yukihiro Matsumoto) 7 months ago

Accepted.

Matz.

Updated by osyo (manga osyo) 7 months ago

Thank you!

Updated by nobu (Nobuyoshi Nakada) 7 months ago

  • Status changed from Open to Closed

Closed by f5415a95ce1d393a3fd1d7f657ba85d85171356a

I missed the tag to close the ticket.

Also available in: Atom PDF