Project

General

Profile

Actions

Bug #20334

closed

Time.to_i truncates a fractional timestamp instead of rounding up

Added by werelnon (Malcolm Patterson) 8 months ago. Updated 8 months ago.

Status:
Feedback
Assignee:
-
Target version:
-
[ruby-core:117132]

Description

Simple steps that can be executed in a ruby interactive shell

irb(main):007> t = Time.now
=> 2024-03-14 00:23:55.983885525 +0000
irb(main):008> t.to_f
=> 1710375835.9838855
irb(main):009> t.to_i
=> 1710375835

Based on the example the result of t.to_f.round is the better result?

Updated by nobu (Nobuyoshi Nakada) 8 months ago

  • Status changed from Open to Feedback

It is the intended behavior.

http://ruby-doc.com/3.2.2/Time.html#class-Time-label-Epoch+Seconds

Other retrieval methods such as Time#to_i and Time#to_f
may return a value that rounds or truncates subseconds.

Updated by werelnon (Malcolm Patterson) 8 months ago

Bit vague to say in the docs that to_i and to_f "may return a value that rounds or truncates". Why not just say it truncates? Regardless, I can live with it :)

Actions

Also available in: Atom PDF

Like0
Like1Like0