Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!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; 'output': 0.04; 'true,': 0.04; 'case.': 0.05; '(using': 0.07; 'incompatible': 0.07; 'alternatives': 0.09; 'formatted': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'string)': 0.09; 'timestamps': 0.09; 'advance': 0.10; 'def': 0.10; 'assumed,': 0.16; 'message- id:@dough.gmane.org': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 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; 'libraries': 0.22; 'simpler': 0.22; "i'd": 0.22; 'minutes.': 0.23; 'solutions.': 0.23; 'seems': 0.23; '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; 'header:X -Complaints-To:1': 0.28; 'probably': 0.29; 'skip:( 40': 0.30; 'basic': 0.30; 'function': 0.30; 'code': 0.31; 'from:addr:yahoo.co.uk': 0.32; 'print': 0.32; 'to:addr:python- list': 0.33; 'skip:d 20': 0.34; 'thanks': 0.34; 'subject:?': 0.35; 'there': 0.35; 'add': 0.36; 'received:org': 0.36; 'should': 0.36; 'enough': 0.36; 'possible': 0.37; 'one,': 0.37; '(for': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'mark': 0.38; 'some': 0.38; 'things': 0.38; 'to:addr:python.org': 0.39; 'google': 0.39; 'where': 0.40; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'skip:a 30': 0.60; 'leading': 0.61; 'first': 0.61; 'more': 0.63; 'hours': 0.66; '(is': 0.84; '9.15': 0.84; 'notion': 0.84; 'received:2': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: simpler increment of time values? Date: Thu, 05 Jul 2012 03:35:29 +0100 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-2-98-197-188.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.0; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 In-Reply-To: X-Antivirus: avast! (VPS 120704-0, 04/07/2012), Outbound message X-Antivirus-Status: Clean 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: , Newsgroups: comp.lang.python Message-ID: Lines: 65 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1341455675 news.xs4all.nl 6933 [2001:888:2000:d::a6]:51166 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:24891 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.