# Time#round in 1.9

**URL:** <https://rubytalk.org/t/time-round-in-1-9/59831>\
**Category:** ruby-talk\
**Created:** [30 August 2010 18:42 UTC](https://rubytalk.org/t/time-round-in-1-9/59831 "2010-08-30T18:42:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [30 August 2010 18:42 UTC](https://rubytalk.org/t/time-round-in-1-9/59831/1 "2010-08-30T18:42:09Z")

</div>

Anyone know about Time#round in 1.9. Apparently it exists but it seems  
to do nothing but eat up cpu cycles.

&nbsp;&nbsp;ruby-1.9.2-p0 \> t = Time.new  
&nbsp;&nbsp;&nbsp;=\> 2010-08-30 14:34:03 -0400  
&nbsp;&nbsp;ruby-1.9.2-p0 \> t.round(100000)  
&nbsp;&nbsp;&nbsp;=\> 2010-08-30 14:34:03 -0400  
&nbsp;&nbsp;ruby-1.9.2-p0 \> t.round(1000000)  
&nbsp;&nbsp;&nbsp;=\> 2010-08-30 14:34:03 -0400

---

<div class="post-metadata">

**Author:** ![Kirk\_Haines2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/kirk_haines2/32/1992_2.png) [@Kirk\_Haines2](https://rubytalk.org/u/Kirk_Haines2)\
**Post date:** [30 August 2010 19:13 UTC](https://rubytalk.org/t/time-round-in-1-9/59831/2 "2010-08-30T19:13:48Z")

</div>

A voice was heard to utter: "Use the source, Luke! Errr, uh, Trans.  
Oh, and the docs!"

/\*  
\* call-seq:  
\* time.round([ndigits]) -\> new\_time

> **···**
>
> On Mon, Aug 30, 2010 at 12:42 PM, Intransition \<transfire@gmail.com\> wrote:
> 
> > Anyone know about Time#round in 1.9. Apparently it exists but it seems  
> > to do nothing but eat up cpu cycles.
> > 
> > ruby-1.9.2-p0 \> t = Time.new  
> > =\> 2010-08-30 14:34:03 -0400  
> > ruby-1.9.2-p0 \> t.round(100000)  
> > =\> 2010-08-30 14:34:03 -0400  
> > ruby-1.9.2-p0 \> t.round(1000000)  
> > =\> 2010-08-30 14:34:03 -0400
> 
> \*  
> \* Rounds sub seconds to a given precision in decimal digits (0 digits  
> by default).  
> \* It returns a new time object.  
> \* \_ndigits\_ should be zero or positive integer.  
> \*  
> \* require 'time'  
> \*  
> \* t = Time.utc(2010,3,30, 5,43,"25.123456789".to\_r)  
> \* p t.iso8601(10) #=\> "2010-03-30T05:43:25.1234567890Z"  
> \* p t.round.iso8601(10) #=\> "2010-03-30T05:43:25.0000000000Z"  
> \* p t.round(0).iso8601(10) #=\> "2010-03-30T05:43:25.0000000000Z"  
> \* p t.round(1).iso8601(10) #=\> "2010-03-30T05:43:25.1000000000Z"  
> \* p t.round(2).iso8601(10) #=\> "2010-03-30T05:43:25.1200000000Z"  
> \* p t.round(3).iso8601(10) #=\> "2010-03-30T05:43:25.1230000000Z"  
> \* p t.round(4).iso8601(10) #=\> "2010-03-30T05:43:25.1235000000Z"  
> \* p t.round(5).iso8601(10) #=\> "2010-03-30T05:43:25.1234600000Z"  
> \* p t.round(6).iso8601(10) #=\> "2010-03-30T05:43:25.1234570000Z"  
> \* p t.round(7).iso8601(10) #=\> "2010-03-30T05:43:25.1234568000Z"  
> \* p t.round(8).iso8601(10) #=\> "2010-03-30T05:43:25.1234567900Z"  
> \* p t.round(9).iso8601(10) #=\> "2010-03-30T05:43:25.1234567890Z"  
> \* p t.round(10).iso8601(10) #=\> "2010-03-30T05:43:25.1234567890Z"  
> \*  
> \* t = Time.utc(1999,12,31, 23,59,59)  
> \* p((t + 0.4).round.iso8601(3)) #=\> "1999-12-31T23:59:59.000Z"  
> \* p((t + 0.49).round.iso8601(3)) #=\> "1999-12-31T23:59:59.000Z"  
> \* p((t + 0.5).round.iso8601(3)) #=\> "2000-01-01T00:00:00.000Z"  
> \* p((t + 1.4).round.iso8601(3)) #=\> "2000-01-01T00:00:00.000Z"  
> \* p((t + 1.49).round.iso8601(3)) #=\> "2000-01-01T00:00:00.000Z"  
> \* p((t + 1.5).round.iso8601(3)) #=\> "2000-01-01T00:00:01.000Z"  
> \*  
> \* t = Time.utc(1999,12,31, 23,59,59)  
> \* p (t + 0.123456789).round(4).iso8601(6) #=\>  
> "1999-12-31T23:59:59.123500Z"  
> \*/
> 
> Below that is a short, simple to follow implementation. You are  
> telling it to round to 100000 or 1000000 digits. That's probably not  
> what you mean?
> 
> Kirk Haines

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [30 August 2010 19:45 UTC](https://rubytalk.org/t/time-round-in-1-9/59831/3 "2010-08-30T19:45:17Z")

</div>

> \> Anyone know about Time#round in 1.9. Apparently it exists but it seems  
> \> to do nothing but eat up cpu cycles.
> 
> \> ruby-1.9.2-p0 \> t = Time.new  
> \> =\> 2010-08-30 14:34:03 -0400  
> \> ruby-1.9.2-p0 \> t.round(100000)  
> \> =\> 2010-08-30 14:34:03 -0400  
> \> ruby-1.9.2-p0 \> t.round(1000000)  
> \> =\> 2010-08-30 14:34:03 -0400
> 
> A voice was heard to utter: "Use the source, Luke! Errr, uh, Trans.  
> Oh, and the docs!"

The docs would be nice, but ri has been broken for months[1] and I  
could not find any mention of the method online.

But you are right I could have checked the source.

> /\*  
> \* call-seq:  
> \* time.round([ndigits]) -\> new\_time  
> \*  
> \* Rounds sub seconds to a given precision in decimal digits (0 digits  
> by default).  
> \* It returns a new time object.  
> \* \_ndigits\_ should be zero or positive integer.  
> \*  
> \* require 'time'  
> \*  
> \* t = Time.utc(2010,3,30, 5,43,"25.123456789".to\_r)  
> \* p t.iso8601(10) #=\> "2010-03-30T05:43:25.1234567890Z"  
> \* p t.round.iso8601(10) #=\> "2010-03-30T05:43:25.0000000000Z"  
> \* p t.round(0).iso8601(10) #=\> "2010-03-30T05:43:25.0000000000Z"  
> \* p t.round(1).iso8601(10) #=\> "2010-03-30T05:43:25.1000000000Z"  
> \* p t.round(2).iso8601(10) #=\> "2010-03-30T05:43:25.1200000000Z"  
> \* p t.round(3).iso8601(10) #=\> "2010-03-30T05:43:25.1230000000Z"  
> \* p t.round(4).iso8601(10) #=\> "2010-03-30T05:43:25.1235000000Z"  
> \* p t.round(5).iso8601(10) #=\> "2010-03-30T05:43:25.1234600000Z"  
> \* p t.round(6).iso8601(10) #=\> "2010-03-30T05:43:25.1234570000Z"  
> \* p t.round(7).iso8601(10) #=\> "2010-03-30T05:43:25.1234568000Z"  
> \* p t.round(8).iso8601(10) #=\> "2010-03-30T05:43:25.1234567900Z"  
> \* p t.round(9).iso8601(10) #=\> "2010-03-30T05:43:25.1234567890Z"  
> \* p t.round(10).iso8601(10) #=\> "2010-03-30T05:43:25.1234567890Z"  
> \*  
> \* t = Time.utc(1999,12,31, 23,59,59)  
> \* p((t + 0.4).round.iso8601(3)) #=\> "1999-12-31T23:59:59.000Z"  
> \* p((t + 0.49).round.iso8601(3)) #=\> "1999-12-31T23:59:59.000Z"  
> \* p((t + 0.5).round.iso8601(3)) #=\> "2000-01-01T00:00:00.000Z"  
> \* p((t + 1.4).round.iso8601(3)) #=\> "2000-01-01T00:00:00.000Z"  
> \* p((t + 1.49).round.iso8601(3)) #=\> "2000-01-01T00:00:00.000Z"  
> \* p((t + 1.5).round.iso8601(3)) #=\> "2000-01-01T00:00:01.000Z"  
> \*  
> \* t = Time.utc(1999,12,31, 23,59,59)  
> \* p (t + 0.123456789).round(4).iso8601(6) #=\>  
> "1999-12-31T23:59:59.123500Z"  
> \*/
> 
> Below that is a short, simple to follow implementation. You are  
> telling it to round to 100000 or 1000000 digits. That's probably not  
> what you mean?

I see, so it only rounds sub-seconds per digit. That's too bad,  
Facets' #round method could round to any second. I have to rename.

Thanks.

[1] /home/trans/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/rdoc/ri/  
driver.rb:909:in `load_method': undefined method `' for nil:NilClass  
(NoMethodError)

> **···**
>
> On Aug 30, 3:13 pm, Kirk Haines \<wyhai...@gmail.com\> wrote:
> 
> > On Mon, Aug 30, 2010 at 12:42 PM, Intransition \<transf...@gmail.com\> wrote:
