Project

General

Profile

Actions

Feature #15879

open

Proposal: Time#to_i accepts :unit keyword

Added by joker1007 (Tomohiro Hashidate) almost 5 years ago. Updated over 4 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:92857]

Description

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.

{
  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.

{
  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
}
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0