Project

General

Profile

Actions

Feature #10544

closed

Time#to_i(:millisecond)

Added by Glass_saga (Masaki Matsushita) about 10 years ago. Updated about 8 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:66466]

Description

Currently, we have to take an indirect way to get unix_time in milliseconds.

time = Time.now
milliseconds = (time.to_i * 1000) + (time.usec / 1000.0).round

I think it would be convenient if Time#to_i accepts unit parameter like following.

time = Time.now
milliseconds = time.to_i(:millisecond)

Files

patch.diff (2.71 KB) patch.diff Time#to_i(:millisecond) implementation Glass_saga (Masaki Matsushita), 11/26/2014 01:46 AM

Updated by nobu (Nobuyoshi Nakada) about 10 years ago

  • Description updated (diff)

Do you need unix time in milliseconds so often?

Updated by Glass_saga (Masaki Matsushita) about 10 years ago

I want this feature to give unix time to other languages like JavaScript.
Some languages expect unix time in milliseconds.

Updated by david_macmahon (David MacMahon) about 10 years ago

How about a more general Time#to_i(scale=1)?

Examples:

  • Passing 1 for scale (or passing nothing) would return seconds.
  • Passing 1000 for scale would return milliseconds.
  • Passing 1/60r for scale would return minutes.

Masaki Matsushita wrote:

milliseconds = (time.to_i * 1000) + (time.usec / 1000.0).round

IMHO, it is generally preferable to avoid time travel by truncating to the beginning of the "present" millisecond rather than possibly rounding to the beginning of the "future" millisecond.

Updated by akr (Akira Tanaka) about 8 years ago

  • Status changed from Open to Rejected

We have Process.clock_gettime since Ruby 2.1.

% ruby -e 'p Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond)'    
1478362786099

This can be used as Time.now.to_i(:millisecond) which is shown in this issue.

If it is not enough, please reopen.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0