Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'output': 0.04; 'true,': 0.04; 'case.': 0.05; '(using': 0.07; 'incompatible': 0.07; 'alternatives': 0.09; 'formatted': 0.09; 'result)': 0.09; 'string)': 0.09; 'timestamps': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'advance': 0.10; 'def': 0.10; '(read': 0.16; 'assumed,': 0.16; 'separator,': 0.16; 'subject:values': 0.16; 'values?': 0.16; 'wrote:': 0.17; 'code,': 0.18; 'input': 0.18; 'equivalent': 0.20; 'mostly': 0.20; 'all,': 0.21; 'import': 0.21; 'constant': 0.22; 'delta': 0.22; 'hours,': 0.22; 'libraries': 0.22; 'simpler': 0.22; 'wednesday,': 0.22; "i'd": 0.22; 'cc:2**0': 0.23; 'minutes.': 0.23; 'solutions.': 0.23; 'seems': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'possibility': 0.27; 'i.e.': 0.27; 'object,': 0.27; "doesn't": 0.28; 'received:209.85.212': 0.28; 'surprised': 0.29; 'probably': 0.29; 'skip:( 40': 0.30; 'basic': 0.30; 'function': 0.30; 'code': 0.31; 'file': 0.32; 'print': 0.32; 'received:google.com': 0.34; 'text': 0.34; 'thanks': 0.34; 'doing': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'add': 0.36; 'should': 0.36; 'enough': 0.36; 'possible': 0.37; 'one,': 0.37; '(for': 0.37; 'rather': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'several': 0.39; 'where': 0.40; 'skip:" 10': 0.40; 'july': 0.60; 'skip:a 30': 0.60; 'from:no real name:2**0': 0.60; 'leading': 0.61; 'first': 0.61; 'times': 0.63; 'more': 0.63; 'video': 0.65; 'hours': 0.66; '(is': 0.84; '9.15': 0.84; 'disappointed': 0.84; 'notion': 0.84; 'received:209.85.212.56': 0.91; 'yours.': 0.93 Newsgroups: comp.lang.python Date: Thu, 5 Jul 2012 07:11:38 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=65.183.81.59; posting-account=h7ZmAgoAAAChnNHfIDF1M6YGHuonryl5 References: User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 65.183.81.59 MIME-Version: 1.0 Subject: Re: simpler increment of time values? From: rurpy@yahoo.com To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1341497507 news.xs4all.nl 6923 [2001:888:2000:d::a6]:49737 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:24916 On Wednesday, July 4, 2012 6:29:10 PM UTC-6, 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 If it's any consolation, I had to add a small constant time delta to all the times in a video subtitles file and my code ended up looking very much like yours. What should have take five minutes to write took several hours, I remain surprised and disappointed that doing something so simple (read time text into time object, add timedelta, print result) was so awkward in Python.