Feature #10544
closedTime#to_i(:millisecond)
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
Updated by nobu (Nobuyoshi Nakada) almost 10 years ago
- Description updated (diff)
Do you need unix time in milliseconds so often?
Updated by Glass_saga (Masaki Matsushita) almost 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) almost 10 years ago
How about a more general Time#to_i(scale=1)
?
Examples:
- Passing
1
forscale
(or passing nothing) would return seconds. - Passing
1000
forscale
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.