Re: simpler increment of time values?

Discussion in 'Python' started by Mark Lawrence, Jul 5, 2012.

  1. On 05/07/2012 01:29, Vlastimil Brom wrote:
    > Hi all,
    > I'd like to ask about the possibilities to do some basic manipulation
    > on timestamps - such as incrementing a given time (hour.minute -
    > string) by some minutes.
    > Very basic notion of "time" is assumed, i.e. dateless,
    > timezone-unaware, DST-less etc.
    > I first thought, it would be possible to just add a timedelta to a
    > time object, but, it doesn't seem to be the case.
    >
    > The code I came up with (using time and datetime modules) seems rather
    > convoluted and I would like to ask about some possible more
    > straightforward alternatives I missed.
    > The equivalent function (lacking validation) without the (date)time
    > libraries seems simple enough (for this limited and individual task).
    > Although it is probably mostly throw-away code, which seems to do what
    > I need, I'd be interested in better/more elegant... solutions.
    >
    > # # #
    > import time
    > import datetime
    > import re
    >
    > print re.sub(r"^0","", (datetime.datetime(*list(time.strptime("8.45",
    > "%H.%M"))[:6]) + datetime.timedelta(minutes=30)).strftime("%H.%M"))
    > # 9.15
    >
    > # # # # # # # # #
    >
    > def add_minutes(hour_min_str, separator=".", minutes_to_add=0):
    > h, m = [int(s) for s in hour_min_str.split(separator)]
    > sum_minutes = h * 60 + m + minutes_to_add
    > h, m = divmod(sum_minutes, 60)
    > h = h % 24
    > return "%s%s%s" % (h, separator, m)
    >
    > print add_minutes(hour_min_str="8.45", separator='.', minutes_to_add=30)
    > # 9.15
    >
    > # # # # # # # # #
    >
    > Is it true, that timedelta cannot be used with dateless time values?
    > (Is there some other possibility than the current one, where strptime
    > actually infers 1. 1. 1900?)
    > Is there some simpler way to adapt the incompatible output of strptime
    > as the input of datetime?
    > Is it possible to get one-digit hours formatted without the leading zero?
    >
    > Thanks in advance for any suggestions or remarks;
    > regards,
    > Vlastimil Brom
    >


    from dateutil.relativedelta import relativedelta should simplify things
    for you

    google and ye shall find :)

    --
    Cheers.

    Mark Lawrence.
     
    Mark Lawrence, 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:
    233
    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:
    169
    Chris Angelico
    Jul 5, 2012
  3. Jason Friedman

    Re: simpler increment of time values?

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

    Re: simpler increment of time values?

    Devin Jeanpierre, Jul 5, 2012, in forum: Python
    Replies:
    0
    Views:
    190
    Devin Jeanpierre
    Jul 5, 2012
  5. Vlastimil Brom

    Re: simpler increment of time values?

    Vlastimil Brom, Jul 5, 2012, in forum: Python
    Replies:
    0
    Views:
    164
    Vlastimil Brom
    Jul 5, 2012
Loading...

Share This Page