adding times

Discussion in 'Ruby' started by EB, Oct 24, 2006.

  1. EB

    EB Guest

    Ok, I try to use ruby for all sorts on-the-fly things as
    they come up so that I can practice & learn about it :)

    So, I'm looking up Ruby in a Nutshell, and amazon says
    if I order in the next 10 hours and 40 minutes I can get
    it soon.

    "Cool" I think .. "I can use Ruby to do the time math",
    and irb is my friend :)

    However, I can't get it to work. This is what I tried:

    irb(main):001:0> now=Time.now
    => Tue Oct 24 07:49:31 -0400 2006

    irb(main):002:0> hours = 10
    => 10

    irb(main):003:0> mins = 40
    => 40

    irb(main):004:0> total_minutes = hours * 60 + mins
    => 640

    irb(main):005:0> now
    => Tue Oct 24 07:49:31 -0400 2006

    irb(main):006:0> now.min
    => 49

    irb(main):007:0> now.min + total_minutes
    => 689

    irb(main):008:0> now + total_minutes
    => Tue Oct 24 08:00:11 -0400 2006

    irb(main):009:0> now
    => Tue Oct 24 07:49:31 -0400 2006

    Is there an easy way to add specific hours and minutes to a
    given time? I have a feeling this is easy to do, I just don't
    know how.

    Thanks,
    eb
    EB, Oct 24, 2006
    #1
    1. Advertising

  2. EB

    Kalman Noel Guest

    > irb(main):008:0> now + total_minutes

    The argument for Time#+ is the seconds to add.

    Kalman
    Kalman Noel, Oct 24, 2006
    #2
    1. Advertising

  3. EB

    EB Guest

    Kalman Noel wrote:
    >> irb(main):008:0> now + total_minutes

    >
    > The argument for Time#+ is the seconds to add.


    Hi,

    cool .. so I was somewhat close. Multiplying my total_mins
    by 60 to get seconds and then adding them seems to do the
    trick!

    Thanks Kalman,

    eb
    EB, Oct 24, 2006
    #3
  4. EB

    Ola Bini Guest

    EB wrote:
    > Is there an easy way to add specific hours and minutes to a
    > given time? I have a feeling this is easy to do, I just don't
    > know how.


    I'm not sure I understand exactly what you're trying to do, but
    something like this:

    class Fixnum
    def minutes
    self*60
    end
    def hours
    self*3600
    end
    end

    Time.now + 7.minutes
    x = Time.now
    x += 13.hours + 17.minutes

    Something like this can be found in Facets too.

    --
    Ola Bini (http://ola-bini.blogspot.com)
    JvYAML, RbYAML, JRuby and Jatha contributor
    System Developer, Karolinska Institutet (http://www.ki.se)
    OLogix Consulting (http://www.ologix.com)

    "Yields falsehood when quined" yields falsehood when quined.
    Ola Bini, Oct 24, 2006
    #4
  5. EB

    EB Guest

    Ola Bini wrote:
    > EB wrote:
    >> Is there an easy way to add specific hours and minutes to a
    >> given time? I have a feeling this is easy to do, I just don't
    >> know how.

    >
    > I'm not sure I understand exactly what you're trying to do, but
    > something like this:
    >
    > class Fixnum
    > def minutes
    > self*60
    > end
    > def hours
    > self*3600
    > end
    > end
    >
    > Time.now + 7.minutes
    > x = Time.now
    > x += 13.hours + 17.minutes


    Nice! Thanks!

    > Something like this can be found in Facets too.


    I had no idea about Ruby Facets, I just google for it, thanks for
    mentioning it.

    There is so much to learn, and such little time!! (love learning though)

    eb
    EB, Oct 24, 2006
    #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. =?Utf-8?B?bWF2cmlja18xMDE=?=

    SetAuthCookie works some times and fails some times?

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Mar 23, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    498
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Mar 23, 2006
  2. =?Utf-8?B?bWF2cmlja18xMDE=?=

    Forms Authentication Fails some times and not some times???

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Mar 28, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    481
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Mar 28, 2006
  3. djskrill
    Replies:
    9
    Views:
    687
    djskrill
    Oct 1, 2003
  4. Peng Yu
    Replies:
    17
    Views:
    672
    Peng Yu
    Sep 14, 2008
  5. Jack Steven
    Replies:
    2
    Views:
    416
    Chris Rebert
    Mar 9, 2009
Loading...

Share This Page