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 47
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 : HTML to XML ---> get title & description

Ask a Rails expert : Accessing controller actions from scripted page?

Ask a Rails expert : seledted option for select_tag

Ask a Rails expert : Merging fields from two tables into one, including duplicates

Ask a Rails expert : saving has_many :through

Ask a Rails expert : Use Rails to develop sites for both Designer and Programmer

Ask a Rails expert : Rails+RS232

Ask a Rails expert : Is this a good way to add Admin section

Ask a Rails expert : RSS feed maker in rails 2.1

Ask a Rails expert : Syncing with ugly legacy databases

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