Re: simpler increment of time values?

Discussion in 'Python' started by Vlastimil Brom, Jul 5, 2012.

  1. Many thanks to all for your suggestions!

    @ChrisA
    Yes, the calculations with seconds since the Unix epoch is very
    convenient for real times, but trying to make it dateless seemed to
    make it more complicated for me.

    The expected output for the increments asked by Jason was already
    correctly stated by Devin; i.e.: 12:45 plus 12 hours is 0:45 and 12:45
    minus 13 hours is 23:45.

    Thanks for reminding me of dateutil.relativedelta, Mark, I didn't
    think of it in this context (I always thought, the "relative" stands
    for time and date calculations with regard to the current time and
    date). There doesn't seem to be a way to use dateless time either
    (unless I missed it),
    however, it turns out, that one can probably work with this naive
    "times" like with deltas (possibly ignoring other units than hours and
    minutes in the result):

    >>> td = dateutil.relativedelta.relativedelta(hours=9, minutes=45) + dateutil.relativedelta.relativedelta(minutes=30)
    >>> "%s.%s" % (td.hours, td.minutes)

    '10.15'
    >>>

    Which is probably the simplest and the most robust way, I found sofar.
    It likely isn't the expected use case for relativedelta, but it seems
    to work ok. (Are there maybe some drawbacks I am missing?)
    (Well I just found one possible pitfall , if floats are passed:
    >>> td = dateutil.relativedelta.relativedelta(hours=9, minutes=45) + dateutil.relativedelta.relativedelta(minutes=30.5)
    >>> "%s.%s" % (td.hours, td.minutes)

    '10.0.15.5'
    , but its beyond my current use case, and the validation can always be added.)


    The same would be doable using the built in timedelta too, but there
    are no hours and minutes in its output, hence these are to be
    converted from the seconds count.

    >>> dttd=datetime.timedelta(hours=9, minutes=45) + datetime.timedelta(minutes=30)
    >>> dttd

    datetime.timedelta(0, 36900)
    >>> dttd.seconds

    36900
    >>> s = dttd.seconds
    >>> h,s = divmod(s,3600)
    >>> m,s = divmod(s,60)
    >>> h,m,s

    (10, 15, 0)
    >>> "%s.%s" % (h, m)

    '10.15'
    >>>


    Any thoughts?
    thanks again,

    vbr
     
    Vlastimil Brom, Jul 5, 2012
    #1
    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. Vlastimil Brom

    simpler increment of time values?

    Vlastimil Brom, Jul 5, 2012, in forum: Python
    Replies:
    5
    Views:
    232
    Steven D'Aprano
    Jul 6, 2012
  2. Chris Angelico

    Re: simpler increment of time values?

    Chris Angelico, Jul 5, 2012, in forum: Python
    Replies:
    0
    Views:
    166
    Chris Angelico
    Jul 5, 2012
  3. Mark Lawrence

    Re: simpler increment of time values?

    Mark Lawrence, Jul 5, 2012, in forum: Python
    Replies:
    0
    Views:
    174
    Mark Lawrence
    Jul 5, 2012
  4. Jason Friedman

    Re: simpler increment of time values?

    Jason Friedman, Jul 5, 2012, in forum: Python
    Replies:
    0
    Views:
    159
    Jason Friedman
    Jul 5, 2012
  5. Devin Jeanpierre

    Re: simpler increment of time values?

    Devin Jeanpierre, Jul 5, 2012, in forum: Python
    Replies:
    0
    Views:
    184
    Devin Jeanpierre
    Jul 5, 2012
Loading...

Share This Page