From: sawadatsuyoshi@... Date: 2019-05-27T10:17:17+00:00 Subject: [ruby-core:92860] [Ruby trunk Feature#15879] Proposal: Time#to_i accepts :unit keyword Issue #15879 has been updated by sawa (Tsuyoshi Sawada). With `Float#to_i`, the decimal part is truncated, not rounded: ```ruby 123.7.to_i # => 123 123.7.round # => 124 ``` In contrast to this, your proposal with `Time#to_i` seem to expect to get rounded result even though the method name is `to_i`. I think this is confusing. Is this your intension? ---------------------------------------- Feature #15879: Proposal: Time#to_i accepts :unit keyword https://bugs.ruby-lang.org/issues/15879#change-78241 * Author: joker1007 (Tomohiro Hashidate) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- I often need Unix time as microseconds or nanoseconds to serialize for other language environments. For example, Java uses milliseconds(nanoseconds) basically. In such a situation, current Ruby code is like below. ``` ruby { event_id: id, name: name, tracked_at: (tracked_at.to_f * 1000).round, tracked_at_micro: (tracked_at * 1000000 + tracked_at.usec) } ``` But this example is noisy. And it is easy to make a mistake. I want to write like below. ```ruby { event_id: id, name: name, tracked_at: tracked_at.to_i(unit: :milli), tracked_at_micro: tracked_at.to_i(unit: :micro) } # or { event_id: id, name: name, tracked_at: tracked_at.as_msec, tracked_at_micro: tracked_at.as_usec } ``` -- https://bugs.ruby-lang.org/ Unsubscribe: