Time Diff Module

Discussion in 'Ruby' started by David Clements, Jan 17, 2006.

  1. ------=_Part_50992_21857766.1137516497737
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    Anyone know of a module out there that will encapsulate differences in time=
    ?

    Ideally I would want something like this:

    d1 =3D DateTime.now + 2.hours + 2.minutes
    d2 =3D DateTime.now

    diff =3D d1-d2


    puts "Diff: #{diff.hours}:#{diff.minutes}"


    >> Diff: 2:02


    I have searched for examples and all of them seem to be doing this manually=
    David Clements, Jan 17, 2006
    #1
    1. Advertising

  2. On 1/17/06, David Clements <> wrote:
    > Anyone know of a module out there that will encapsulate differences in ti=

    me?
    >
    > Ideally I would want something like this:
    >
    > d1 =3D DateTime.now + 2.hours + 2.minutes
    > d2 =3D DateTime.now
    >
    > diff =3D d1-d2
    >
    >
    > puts "Diff: #{diff.hours}:#{diff.minutes}"
    >
    >
    > >> Diff: 2:02

    >
    > I have searched for examples and all of them seem to be doing this manual=

    ly.
    >
    > Dave


    Runt may have what you're looking for:
    http://runt.rubyforge.org/

    Tutorial:
    http://runt.rubyforge.org/doc/files/doc/tutorial_te_rdoc.html
    Wilson Bilkovich, Jan 17, 2006
    #2
    1. Advertising

  3. On Wed, 2006-01-18 at 01:48 +0900, David Clements wrote:
    > Anyone know of a module out there that will encapsulate differences in time?
    >
    > Ideally I would want something like this:
    >
    > d1 = DateTime.now + 2.hours + 2.minutes
    > d2 = DateTime.now
    >
    > diff = d1-d2
    >
    >
    > puts "Diff: #{diff.hours}:#{diff.minutes}"
    >
    >
    > >> Diff: 2:02

    >
    > I have searched for examples and all of them seem to be doing this manually.
    >
    > Dave


    Ruby on Rails' ActiveSupport::CoreExtensions::Numeric::Time module
    supports this. The gem is activesupport. You might want to extract it
    somehow to avoid getting the rest of the library if you don't need it.

    Jon

    --
    Jonathan Leighton
    http://turnipspatch.com/ | http://jonathanleighton.com/ | http://digital-proof.org/
    Jonathan Leighton, Jan 17, 2006
    #3
  4. David Clements

    Guest

    On Wed, 18 Jan 2006, David Clements wrote:

    > Anyone know of a module out there that will encapsulate differences in time?
    >
    > Ideally I would want something like this:
    >
    > d1 = DateTime.now + 2.hours + 2.minutes
    > d2 = DateTime.now
    >
    > diff = d1-d2
    >
    >
    > puts "Diff: #{diff.hours}:#{diff.minutes}"
    >
    >
    >>> Diff: 2:02

    >
    > I have searched for examples and all of them seem to be doing this manually.


    i'm guessing the reason this has not been done generically is that it is
    incredibly slippery. for instance, if i had

    a = DateTime::new 1582, 8, 4
    b = a + 1.day

    then b should the the day '1582-08-15'. and

    diff = b - a
    p diff.days

    should output either 10 or 1 - depending on how you look at it.

    the reason is the fact that DateTime makes all the calender reforms between
    julian and gregorian transparent and even handles them based on timezone. not
    saying it could not be done, but it's tough and, afaik, hasn't been. see
    ruby-1.8.4/lib/date.rb for details.

    of course, activesupport give you Numeric#days, etc, but that is quite easy to
    roll.

    if the code you posted is pretty close you what you actually need then why not
    simply use Time objects, converting to DateTime as needed using
    DateTime::new(t.year, t.month, t.day... etc)?

    harp:~ > cat a.rb
    class Time
    module Units
    def __less__() "/" end
    def __more__() "*" end
    def milliseconds() self.send(__less__,1000) end; alias_method "millisecond", "milliseconds"
    def seconds() self end; alias_method "second", "seconds"
    def minutes() seconds.send(__more__,60) end; alias_method "minute", "minutes"
    def hours() minutes.send(__more__,60) end; alias_method "hour", "hours"
    def days() hours.send(__more__,24) end; alias_method "day", "days"
    def weeks() days.send(__more__,7) end; alias_method "week", "weeks"
    def months() weeks.send(__more__,4) end; alias_method "month", "months"
    def years() months.send(__more__,12) end; alias_method "year", "years"
    def decades() years.send(__more__,10) end; alias_method "decade", "decades"
    def centuries() decades.send(__more__,10) end; alias_method "century", "centuries"
    end
    module DiffUnits
    include ::Time::Units
    def __less__() "*" end
    def __more__() "/" end
    end
    alias_method "__delta__", "-"
    def - other
    ret = __delta__ other
    ret.extend DiffUnits
    ret
    end
    end
    class Numeric
    include ::Time::Units
    end

    now = Time::now
    a = now
    b = now + 2.hours + 2.minutes
    d = b - a

    require "yaml"
    %w( seconds minutes hours days ).each{|m| y m => d.send(m)}


    harp:~ > ruby a.rb
    ---
    seconds: 7320.0
    ---
    minutes: 122.0
    ---
    hours: 2.03333333333333
    ---
    days: 0.0847222222222222


    hmmm. maybe i'll add this to alib...

    cheers.

    -a
    --
    strong and healthy, who thinks of sickness until it strikes like lightning?
    preoccupied with the world, who thinks of death, until it arrives like
    thunder? -- milarepa
    , Jan 17, 2006
    #4
  5. David Clements

    ABHILASH M.A Guest

    David Clements wrote in post #23880:
    > Anyone know of a module out there that will encapsulate differences in
    > time?
    >
    > Ideally I would want something like this:
    >
    > d1 = DateTime.now + 2.hours + 2.minutes
    > d2 = DateTime.now
    >
    > diff = d1-d2
    >
    >
    > puts "Diff: #{diff.hours}:#{diff.minutes}"
    >
    >
    >>> Diff: 2:02

    >
    > I have searched for examples and all of them seem to be doing this
    > manually.
    >
    > Dave


    There is a gem available to find the difference in a useful way.

    https://rubygems.org/gems/time_diff

    This will return a hash of difference in year, month, week, day, hour,
    minute, second

    --
    Posted via http://www.ruby-forum.com/.
    ABHILASH M.A, 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. Cyril Vi?ville

    diff Process under diff users

    Cyril Vi?ville, Jun 29, 2004, in forum: Perl
    Replies:
    1
    Views:
    509
    Joe Smith
    Jun 29, 2004
  2. Berrucho
    Replies:
    2
    Views:
    635
    Infant Newbie
    Dec 5, 2003
  3. A Traveler

    Diff CSS styles for diff INPUT TYPE='s?

    A Traveler, Aug 31, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    4,923
    Steve Fulton
    Aug 31, 2004
  4. flamesrock
    Replies:
    8
    Views:
    451
    Hendrik van Rooyen
    Nov 24, 2006
  5. Austin Ziegler

    [ANN] Diff::LCS 1.1.0, Diff::LCS 1.0.4

    Austin Ziegler, Aug 8, 2004, in forum: Ruby
    Replies:
    3
    Views:
    187
    Austin Ziegler
    Aug 9, 2004
Loading...

Share This Page