Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!feeder2.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.023 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'else:': 0.03; 'elegant,': 0.09; 'binary': 0.13; 'received:209.85.210.174': 0.13; 'received :mail-iy0-f174.google.com': 0.13; 'subject:skip:t 10': 0.15; 'cmp': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'wrote:': 0.18; 'later': 0.21; 'dec': 0.22; 'assume': 0.22; 'header:In-Reply-To:1': 0.22; 'sat,': 0.25; "i'm": 0.26; 'function': 0.27; 'exercise': 0.28; 'jason': 0.28; 'message- id:@mail.gmail.com': 0.28; '24,': 0.29; 'pm,': 0.29; '-1,': 0.30; "i'll": 0.31; 'does': 0.32; 'break': 0.32; 'to:addr:python-list': 0.34; 'dinner': 0.34; 'skip:i 40': 0.34; '(not': 0.35; 'but': 0.37; 'received:google.com': 0.37; 'could': 0.37; 'received:209.85': 0.38; 'difficult': 0.39; 'received:209': 0.40; 'to:addr:python.org': 0.40; 'more': 0.61; '2011': 0.61; 'family': 0.63; 'believe': 0.65; 'stated': 0.68; 'valid)': 0.84; 'christmas': 0.97 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=+WsDPsFLy/UBDnBpw5WuP5s++WWZ7s1SJXyE+fRL24Y=; b=WrdUgfRoYNdCfD2c+lh8DBV+eXT+JAOCd2FsEt6PE2mdn0uzUFBJjOo+emOXVDJiGf 8858L5Yl2VVM2jRwlxp0hLUP7EevZ6aJ9Ltj0kzdIXKH9MQTYviAQwrXqKBSPVbskKA8 rlRepqBXgcwf3glxZnqFd/QlIba9qJUyeoqJw= MIME-Version: 1.0 In-Reply-To: References: Date: Sat, 24 Dec 2011 18:13:13 +1100 Subject: Re: modifying a time.struct_time 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: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324710802 news.xs4all.nl 6949 [2001:888:2000:d::a6]:47337 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17834 On Sat, Dec 24, 2011 at 5:04 PM, Jason Friedman wrote: > Not particularly elegant, but I believe accurate and relying only on > the stated struct_time contract: Funny! But a binary search would be better, I think. t = time.time() time1 = time.localtime(t) print("Local time is {}.".format(time1)) est1 = t + 365*86400 est2 = est1 + 86400 # Assume that the year is between 365 and 366 days long (widen this if assumption not valid) while True: est = (est1 + est2) / 2 time2 = time.localtime(t) cmp = is_local_time_different_by_one_year(time1, time2): if cmp<0: est1 = est elif cmp>0: est2 = est else: print("One year later is {}".format(time2)) break Could do with a few more improvements. Also, it requires that the comparison function return -1, 0, or 1, in the same way that strcmp() does (not a difficult enhancement, but I'm off to Christmas dinner with the family so I'll leave it as an exercise for the reader). ChrisA