Project

General

Profile

Feature #10544

Time#to_i(:millisecond)

Added by Glass_saga (Masaki Matsushita) about 5 years ago. Updated about 3 years ago.

Status:
Rejected
Priority:
Normal
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

History

Updated by nobu (Nobuyoshi Nakada) about 5 years ago

  • Description updated (diff)

Do you need unix time in milliseconds so often?

Updated by Glass_saga (Masaki Matsushita) about 5 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 5 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 3 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.

Also available in: Atom PDF