convert time to ruby time to calculate the time difference.

Discussion in 'Ruby' started by Ruwan Budha, Sep 20, 2010.

  1. Ruwan Budha

    Ruwan Budha Guest

    Hi all,

    I have following two time stamps,

    12:35:10:757 & 12:35:10:759

    I want to convert them to ruby time and find the time difference.

    which is a 2 micro seconds.

    I would appreciate any help..

    thanks in advance
    --
    Posted via http://www.ruby-forum.com/.
     
    Ruwan Budha, Sep 20, 2010
    #1
    1. Advertising

  2. Il 20/09/10 17.21, Ruwan Budha ha scritto:
    > Hi all,
    >
    > I have following two time stamps,
    >
    > 12:35:10:757& 12:35:10:759
    >
    > I want to convert them to ruby time and find the time difference.
    >
    > which is a 2 micro seconds.
    >

    Simply:

    require 'time'

    mT=Time.utc(2010,9,20,12,35,10,757)
    => Mon Sep 20 12:35:10 UTC 2010
    mT1=Time.utc(2010,9,20,12,35,10,759)
    => Mon Sep 20 12:35:10 UTC 2010
    mT1-mT
    => 2.0e-06

    > I would appreciate any help..
    >
    > thanks in advance
    >

    You're welcome, but before ask, try to google a bit...[1]


    [1] http://ruby-doc.org/core/classes/Time.html#M000252
     
    Francesco Vollero, Sep 20, 2010
    #2
    1. Advertising

  3. Alternative:

    >> require 'time'

    => true
    >> t1 = Time.parse("12:35:10.000757")

    => Mon Sep 20 12:35:10 +0100 2010
    >> t2 = Time.parse("12:35:10.000759")

    => Mon Sep 20 12:35:10 +0100 2010
    >> t2 - t1

    => 2.0e-06
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Sep 20, 2010
    #3
  4. Ruwan Budha

    ABHILASH M.A Guest

    Brian Candler wrote in post #942410:
    > Alternative:
    >
    >>> require 'time'

    > => true
    >>> t1 = Time.parse("12:35:10.000757")

    > => Mon Sep 20 12:35:10 +0100 2010
    >>> t2 = Time.parse("12:35:10.000759")

    > => Mon Sep 20 12:35:10 +0100 2010
    >>> t2 - t1

    > => 2.0e-06


    If you got the ruby time, you can use the gem 'time_diff' to get the
    time difference in a useful format like year, month, week, day, hour,
    minute, second

    https://rubygems.org/gems/time_diff

    --
    Posted via http://www.ruby-forum.com/.
     
    ABHILASH M.A, Mar 9, 2011
    #4
  5. Ruwan Budha

    Kirk Haines Guest

    On Wed, Mar 9, 2011 at 9:37 AM, ABHILASH M.A <> wrote:
    > Brian Candler wrote in post #942410:
    >> Alternative:
    >>
    >>>> require 'time'

    >> => true
    >>>> t1 = Time.parse("12:35:10.000757")

    >> => Mon Sep 20 12:35:10 +0100 2010
    >>>> t2 = Time.parse("12:35:10.000759")

    >> => Mon Sep 20 12:35:10 +0100 2010
    >>>> t2 - t1

    >> => 2.0e-06

    >
    > If you got the ruby time, you can use the gem 'time_diff' to get the
    > time difference in a useful format like year, month, week, day, hour,
    > minute, second


    I haven't looked at the gem, but....it's all just simple math, except
    for the month part, which takes a bit more work. Does the gem
    _actually_ calculate the difference in months between two dates, or
    does it just approximate it?

    Kirk Haines
    Software Engineer
    EngineYard
     
    Kirk Haines, Mar 9, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. In a little while

    how to calculate the difference between 2 addresses ?

    In a little while, Nov 19, 2006, in forum: C Programming
    Replies:
    2
    Views:
    342
    Richard Heathfield
    Nov 19, 2006
  2. In a little while
    Replies:
    13
    Views:
    870
    John Carson
    Nov 20, 2006
  3. Kenneth Johansen

    Calculate the difference between entries in recordset..

    Kenneth Johansen, May 5, 2004, in forum: ASP General
    Replies:
    1
    Views:
    125
    Ken Schaefer
    May 5, 2004
  4. nukeymusic
    Replies:
    10
    Views:
    550
    nukeymusic
    Dec 18, 2011
  5. Frank

    Calculate difference in dates

    Frank, Aug 29, 2004, in forum: Javascript
    Replies:
    26
    Views:
    238
    Mick White
    Sep 8, 2004
Loading...

Share This Page