Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.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.015 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'sys': 0.05; 'python': 0.08; 'elegant,': 0.09; 'integers': 0.09; 'am,': 0.12; 'def': 0.13; '16,': 0.15; 'subject:skip:t 10': 0.15; '2.7.2': 0.16; 'eckhardt': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'cc:no real name:2**0': 0.20; 'trying': 0.21; 'later': 0.21; 'dec': 0.22; 'header:In-Reply-To:1': 0.22; 'cc:2**0': 0.24; 'sender:addr:gmail.com': 0.25; 'code': 0.25; "i'm": 0.26; 'import': 0.27; 'message-id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.29; 'second': 0.29; 'pm,': 0.29; 'chris': 0.30; 'break': 0.32; 'actually': 0.33; 'received:209.85.160': 0.33; 'fri,': 0.34; 'skip:# 10': 0.34; 'skip:i 40': 0.34; 'received:209.85.160.46': 0.35; 'received:mail- pw0-f46.google.com': 0.35; 'test': 0.35; 'but': 0.37; 'received:google.com': 0.37; 'some': 0.38; 'received:209.85': 0.38; 'e.g.': 0.39; "it's": 0.40; 'received:209': 0.40; '2011': 0.61; 'reached': 0.61; 'back': 0.62; 'order': 0.62; 'believe': 0.65; 'exact': 0.68; 'stated': 0.68; 'time?': 0.73; '10:44': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=4OVf+85Q/fywNmc5e4s8miWF5E+ovSmkR8W1+2e9K8Q=; b=h9RM2ZaQIqw+KfSAPNlBIwo/zNud57Aq00U2FM8Zec9InZerNFiQNhJXoz7qLI6apy jeS0JEwsG6DQ/tv8TYcFbJ5COwB5l7Xt1uT3yQzU+GKRVR1ownabOitXVrMJNoJJDefQ lxbmCLWuG+IzueRVfkLBpcSH/5im/+sMxvsSc= MIME-Version: 1.0 Sender: jsf80238@gmail.com In-Reply-To: References: Date: Sat, 24 Dec 2011 06:04:19 +0000 X-Google-Sender-Auth: 7MhAO_tFByKgdtfxJANVaEf5Mco Subject: Re: modifying a time.struct_time From: Jason Friedman To: Chris Angelico Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: , Newsgroups: comp.lang.python Message-ID: Lines: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324706664 news.xs4all.nl 6844 [2001:888:2000:d::a6]:46191 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17833 On Fri, Dec 16, 2011 at 10:44 AM, Chris Angelico wrote: > On Fri, Dec 16, 2011 at 8:45 PM, Ulrich Eckhardt > wrote: >> I'm trying to create a struct_time that is e.g. one year ahead or a mont= h >> back in order to test some parsing/formatting code with different dates. > > Do you need it to be one exact calendar year, or would it make sense > to add/subtract integers from a Unix time? > > t =3D time.time() + 365*86400 =A0 # Not actually a year ahead, it's 365 d= ays ahead > t =3D time.localtime(t) =A0# if you want a struct_time > > ChrisA > -- Not particularly elegant, but I believe accurate and relying only on the stated struct_time contract: #!/usr/bin/env python # 2.7.2 import time, itertools def is_local_time_different_by_one_year(time1, time2): if abs(time1.tm_year - time2.tm_year) !=3D 1: return False if abs(time1.tm_mon - time2.tm_mon ) !=3D 0: return False if abs(time1.tm_mday - time2.tm_mday) !=3D 0: return False if abs(time1.tm_hour - time2.tm_hour) !=3D 0: return False if abs(time1.tm_min - time2.tm_min ) !=3D 0: return False if abs(time1.tm_sec - time2.tm_sec ) !=3D 0: return False return True t =3D time.time() time1 =3D time.localtime(t) print("Local time is {}.".format(time1)) for i in itertools.count(0): t +=3D 1 # Add one second until we have reached next year time2 =3D time.localtime(t) if is_local_time_different_by_one_year(time1, time2): print("One year later is {}".format(time2)) break Not exactly a speed demon, either: $ time python timediff.py Local time is time.struct_time(tm_year=3D2011, tm_mon=3D12, tm_mday=3D24, tm_hour=3D5, tm_min=3D57, tm_sec=3D44, tm_wday=3D5, tm_yday=3D358, tm_isdst= =3D0). One year later is time.struct_time(tm_year=3D2012, tm_mon=3D12, tm_mday=3D24, tm_hour=3D5, tm_min=3D57, tm_sec=3D44, tm_wday=3D0, tm_yday= =3D359, tm_isdst=3D0) real 3m8.922s user 2m2.470s sys 1m1.760s