Operations with Time objects

Discussion in 'Ruby' started by Stefano Bortolotti, Mar 13, 2008.

  1. Hi guys.
    I want to know the time remain before an expiration.
    If I do t_remained = Time.now - expiration I get a float object.
    how can I convert it back to Time(days hours and minute)?

    thanks
    --
    Posted via http://www.ruby-forum.com/.
     
    Stefano Bortolotti, Mar 13, 2008
    #1
    1. Advertising

  2. I have tried this:
    >> t = Time.now

    => Thu Mar 13 12:57:47 +0100 2008
    >> t1 = Time.now + 1.hours

    => Thu Mar 13 13:58:03 +0100 2008
    >> t2 = t1 - t

    => 3615.968256
    >> tot = Time.at(t2)

    => Thu Jan 01 02:00:15 +0100 1970

    but I don't know why the difference between t1 e t is 2 hours and not 1
    hour?
    --
    Posted via http://www.ruby-forum.com/.
     
    Stefano Bortolotti, Mar 13, 2008
    #2
    1. Advertising

  3. Stefano Bortolotti

    Gareth Adams Guest

    Stefano Bortolotti wrote:
    > I have tried this:
    >>> t = Time.now

    > => Thu Mar 13 12:57:47 +0100 2008
    >>> t1 = Time.now + 1.hours

    > => Thu Mar 13 13:58:03 +0100 2008
    >>> t2 = t1 - t

    > => 3615.968256
    >>> tot = Time.at(t2)

    > => Thu Jan 01 02:00:15 +0100 1970
    >
    > but I don't know why the difference between t1 e t is 2 hours and not 1
    > hour?


    a Time object doesn't represent a duration (2 hours) it represents a
    point in time (Jan 1st 1970, 2am). There's no standard Ruby object
    representing a time duration, but it seems that a Float (number of
    seconds) is good enough.

    you can always do (num_of_seconds / 1.hour) to give you a fractional
    number of hours etc.

    Gareth
     
    Gareth Adams, Mar 13, 2008
    #3
  4. On Mar 13, 2008, at 8:02 AM, Stefano Bortolotti wrote:

    > I have tried this:
    >>> t = Time.now

    > => Thu Mar 13 12:57:47 +0100 2008
    >>> t1 = Time.now + 1.hours

    > => Thu Mar 13 13:58:03 +0100 2008
    >>> t2 = t1 - t

    > => 3615.968256
    >>> tot = Time.at(t2)

    > => Thu Jan 01 02:00:15 +0100 1970
    >
    > but I don't know why the difference between t1 e t is 2 hours and
    > not 1
    > hour?



    irb> t = Time.now
    => Thu Mar 13 09:26:37 -0400 2008
    irb> t1 = Time.now + 3600
    => Thu Mar 13 10:26:56 -0400 2008
    irb> t2 = t1 - t
    => 3618.385132
    irb> Time.at(t2)
    => Wed Dec 31 20:00:18 -0500 1969

    Because you're not looking at the time zone (+0100).

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
     
    Rob Biedenharn, Mar 13, 2008
    #4
  5. Thanks guys! I have done this:

    def remaining_time
    if expiration != nil
    diff = expiration - Time.now

    days = (diff / 1.day).to_i
    hours = ((diff / 1.hour) % 24).to_i
    mins = ((diff / 1.minute) 60).to_i

    if diff < 7.days
    return days.to_s + "gg, " + hours.to_s + ":" + mins.to_s
    else
    return "more than 7 days"
    end
    end
    end

    I'll try to improve it! If you have some advices, they are wellcomes..
    --
    Posted via http://www.ruby-forum.com/.
     
    Stefano Bortolotti, Mar 13, 2008
    #5
  6. On Mar 13, 2008, at 12:11 PM, Stefano Bortolotti wrote:

    > Thanks guys! I have done this:
    >
    > def remaining_time
    > if expiration != nil
    > diff = expiration - Time.now
    >
    > days = (diff / 1.day).to_i
    > hours = ((diff / 1.hour) % 24).to_i
    > mins = ((diff / 1.minute) 60).to_i
    >
    > if diff < 7.days
    > return days.to_s + "gg, " + hours.to_s + ":" + mins.to_s
    > else
    > return "more than 7 days"
    > end
    > end
    > end
    >
    > I'll try to improve it! If you have some advices, they are wellcomes..



    Since you have 1.day, 1.hour, 1.min, I assume that you have
    ActiveSupport and likely have Rails. In any case, you can use
    distance_of_time_in_words

    http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#M001006

    If you want to clean up your own method:

    days, dayfrac = diff.divmod(1.day)
    hours, hrfrac = dayfrac.divmod(1.hour)
    mins, secs = hrfrac.divmod(1.min)

    if days >= 7
    return "more than 7 days"
    else
    result = []
    result << "%sgg,"%days unless days.zero?
    result << "%d:%0d"%[hours,mins]
    return result.join(' ')
    end

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
     
    Rob Biedenharn, Mar 13, 2008
    #6
  7. Siep Korteling, Mar 13, 2008
    #7
  8. Rob Biedenharn wrote:
    > If you want to clean up your own method:
    >
    > days, dayfrac = diff.divmod(1.day)
    > hours, hrfrac = dayfrac.divmod(1.hour)
    > mins, secs = hrfrac.divmod(1.min)
    >
    > if days >= 7
    > return "more than 7 days"
    > else
    > result = []
    > result << "%sgg,"%days unless days.zero?
    > result << "%d:%0d"%[hours,mins]
    > return result.join(' ')
    > end
    >
    > -Rob
    >
    > Rob Biedenharn http://agileconsultingllc.com
    >


    Thanks Rob, I knew about distance_of_time_in_words but I want my own
    method.
    Cool the divmod..
    --
    Posted via http://www.ruby-forum.com/.
     
    Stefano Bortolotti, Mar 13, 2008
    #8
    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. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,306
    robert
    Feb 11, 2006
  2. Leslaw Bieniasz
    Replies:
    4
    Views:
    365
  3. flamesrock
    Replies:
    8
    Views:
    519
    Hendrik van Rooyen
    Nov 24, 2006
  4. 7stud
    Replies:
    11
    Views:
    729
    Dennis Lee Bieber
    Mar 20, 2007
  5. Robson Carvalho Machado

    How to execute operations on dynamic objects?

    Robson Carvalho Machado, Aug 7, 2004, in forum: ASP .Net Web Controls
    Replies:
    6
    Views:
    155
    Robson Carvalho Machado
    Aug 7, 2004
Loading...

Share This Page