Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.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.001 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; 'string)': 0.09; 'timestamps': 0.09; 'advance': 0.10; 'def': 0.10; 'assumed,': 0.16; 'separator,': 0.16; 'subject:values': 0.16; 'values?': 0.16; 'code,': 0.18; 'input': 0.18; 'equivalent': 0.20; 'mostly': 0.20; 'all,': 0.21; 'import': 0.21; 'received:209.85.214.174': 0.21; 'libraries': 0.22; 'simpler': 0.22; "i'd": 0.22; 'minutes.': 0.23; 'solutions.': 0.23; 'seems': 0.23; 'possibility': 0.27; 'i.e.': 0.27; 'object,': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'probably': 0.29; 'skip:( 40': 0.30; 'basic': 0.30; 'function': 0.30; 'code': 0.31; 'print': 0.32; 'to:addr :python-list': 0.33; 'received:google.com': 0.34; 'thanks': 0.34; 'subject:?': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'add': 0.36; 'enough': 0.36; 'possible': 0.37; 'one,': 0.37; '(for': 0.37; 'rather': 0.37; 'received:209': 0.37; 'some': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=LzPq6YN221JL16Wt31M9wIDMWPibnJtjjuZ6edTfrsU=; b=Y/CvNlr4PJ6g3OMOxqvdMlgUaYzqhUBXgJopu8/Uo8+FAC9GhZSkMDkjR6Ju/319s4 xhgzonP69xkq9qvysA92GwVuqlr5BxOO2wXA7YpU9LOFuAhOLi9DnWGii+IgGyw9iaBR CHWfQe7oscIwuI4yIlKoGcIna/rpqeUCI/vMpfrj0fthT5bUBqPx6t//2FPw/WMLfnmW VdLxKfz1Oll4XyFXOKYZBJddUbqI4tKf27rOdhwz6fd7A1wsRN43darHzUJIPpHBGIcu cahBAeRjoRV7A2U0LLF04EVqzRxZrC6NQSW6i7ZC4etEtdq5CwGvKPomL8mnAdd2qWRg G2gA== MIME-Version: 1.0 Date: Thu, 5 Jul 2012 02:29:10 +0200 Subject: simpler increment of time values? From: Vlastimil Brom To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1341448153 news.xs4all.nl 6934 [2001:888:2000:d::a6]:37041 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:24887 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