Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'output': 0.04; 'seemed': 0.07; 'python': 0.09; 'minus': 0.09; 'modulo': 0.09; 'times,': 0.13; '(the': 0.15; 'arithmetic.': 0.16; 'epoch': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'seconds,': 0.16; 'subject:values': 0.16; 'well-known': 0.16; 'wrote:': 0.17; 'integer': 0.17; 'tend': 0.17; 'thu,': 0.17; 'translate': 0.20; 'trying': 0.21; 'received:209.85.216.46': 0.21; 'constant': 0.22; 'parse': 0.22; 'header:In-Reply-To:1': 0.25; 'first,': 0.27; 'replace': 0.27; 'message-id:@mail.gmail.com': 0.27; "i'm": 0.29; 'classes': 0.30; 'seconds': 0.30; 'asked': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'jason': 0.35; 'expected': 0.35; 'pm,': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'should': 0.36; 'correctly': 0.37; 'being': 0.37; 'received:209': 0.37; 'received:209.85.216': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; 'real': 0.61; 'free': 0.61; 'day.': 0.63; 'ever': 0.63; 'more': 0.63; 'jul': 0.65; 'hours': 0.66; 'stated': 0.69; 'bounding': 0.84; 'calculations': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=B/Q/KSvJKiDJ+ZrKk1r6gv/yTp65hTTtMHRJ7DktZlI=; b=uhWHgzAWITEy2T0JcLzdsInDGjb49jIBGHHtYpSngLBXhjxhKhTcT4IsSdk3K5EtjB 7HN0md+dx47uOmkt37/vA/HFRh1Ga7rfKmIFk96bcBAt3D4uONulGWqyyIBu7QSrS9So rVY5i0NQzyPoSB0Jl9eRH9yGgyKOeQqM69KjMzvleAXg/LI1rrecxxOhLXZ0kCy/FSyE 5ZufM8PVAk14Ip8Sbq9VQ2DR1rz8WXaly8XhYwsm9hDlzaRZW61Uq2+udjUzMn881+Kd AyU5oRZtLrwdOw1eRrdxP8CyT0JaCsKUitLDvJvevltEUguZw+Sp+dtt/AAQTwQZedyG dw+w== MIME-Version: 1.0 In-Reply-To: References: Date: Thu, 5 Jul 2012 23:56:37 +1000 Subject: Re: simpler increment of time values? From: Chris Angelico 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1341496599 news.xs4all.nl 6909 [2001:888:2000:d::a6]:44138 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:24913 On Thu, Jul 5, 2012 at 11:18 PM, Vlastimil Brom wrote: > 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. I'm not familiar with the Python classes (I tend to think in terms of language-agnostic algorithms first, and specific libraries/modules/etc second), but if you're working with simple integer seconds, your datelessness is just modulo arithmetic. time1 + time2 --> (time1 + time2) % 86400 time1 - time2 --> (time1 + 86400 - time2) % 86400 Or alternatively, bounding afterward: if time > 86400: time-=86400 if time < 86400: time+=86400 (The "magic number" 86400 is a well-known number, being seconds in a day. Feel free to replace it with 24*60*60 if it makes you feel better; I'm pretty sure Python will translate it into a constant at parse time. Or alternatively, have a module-level constant SECONDS_IN_A_DAY = 86400, in case that number should ever change.) ChrisA