[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-core
Subject:    [ruby-core:92870] [Ruby trunk Feature#15879] Proposal: Time#to_i accepts :unit keyword
From:       kakyoin.hierophant () gmail ! com
Date:       2019-05-28 5:28:11
Message-ID: redmine.journal-78249.20190528052811.b700d5e2f65bf69a () ruby-lang ! org
[Download RAW message or body]

Issue #15879 has been updated by joker1007 (Tomohiro Hashidate).


> In contrast to this, your proposal with Time#to_i seems to return rounded results \
> even though the method name is to_i. I think this is confusing. Is this your \
> intention?

It is not important whether the result is rounded or truncated. I'm sorry, this \
examples is not good. The main problem that I must write `1_000_000` whenever I need \
time as microseconds. And so, Such a situaion is often.

----------------------------------------
Feature #15879: Proposal: Time#to_i accepts :unit keyword
https://bugs.ruby-lang.org/issues/15879#change-78249

* 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: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic