Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert How to convert time in mililse...

Replytotopic

How to convert time in mililsecond

Posted in Forums : Ask a Rails expert

 
Profile

Authority 0
Posting Rating 45
Sign in to rate this post

Hi I am trying to convert time into milisecond Is there is any ruby method

 
Me

Authority 62
Posting Rating 100
Sign in to rate this post

Not sure, but couldn’t you just do Time.now.to_i * 1000? If so, you could always monkeypatch it into the Time class:

Time.class_eval do
  def milliseconds
    self.to_i * 1000
  end
end

This way, you could say Time.now.milliseconds.

HTH

 
Cf720f06b

Authority 25
Posting Rating 0
Sign in to rate this post

why not use core Time class ?

irb(main):001:0> require “Time”
=> true
irb(main):002:0> t = Time.now
=> Tue Jul 29 19:56:28 +0200 2008
irb(main):003:0> t.strftime(“%s”)
=> “1217354188”
irb(main):004:0> t.strftime(“%s”).to_i
=> 1217354188

 
Me

Authority 62
Posting Rating 100
Sign in to rate this post

Christian: Isn’t this the value in seconds instead of milliseconds?

 
Cf720f06b

Authority 25
Posting Rating 0
Sign in to rate this post

clemens,
strftime parameter “%S” shows the seconds – part of a time.
however, “%s” does not seem to be correct either, because it shows only the seconds since the Epoch, UTC.

so extending time or creating helpers would be it.

Replytotopic

Other Recent Topics

Ask a Rails expert : How to work with ror 2.1.1 using Netbeans IDE 6.1

Ask a Rails expert : json gem error

Ask a Rails expert : Problem with break

Ask a Rails expert : activesupport string first method error

Ask a Rails expert : url_for broken ?

Ask a Rails expert : map.routes.rb pls help

Ask a Rails expert : how to h tag in controller

Ask a Rails expert : Problem with Restful routing and partial form

Ask a Rails expert : will_paginate, search and ajax

Ask a Rails expert : captcha

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel