Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'one?': 0.05; 'none:': 0.07; '2004': 0.09; 'logic': 0.09; 'try:': 0.09; 'valueerror:': 0.09; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'assume': 0.14; '-tkc': 0.16; '8bit%:32': 0.16; 'adjusting': 0.16; 'finney': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'objects.': 0.16; 'result)': 0.16; 'sane': 0.16; 'throw': 0.16; 'valueerror': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'year,': 0.18; 'module': 0.19; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'case.': 0.24; 'integer': 0.24; 'parse': 0.24; 'initial': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; "i've": 0.25; 'header:In-Reply-To:1': 0.27; 'direction': 0.30; 'except': 0.35; 'date.': 0.36; 'dates': 0.36; 'subject:?': 0.36; 'should': 0.36; 'being': 0.38; 'ben': 0.38; 'whatever': 0.38; 'subject:can': 0.39; 'how': 0.40; 'even': 0.60; 'field': 0.63; 'skip:\xe2 10': 0.65; 'business': 0.70; '8bit%:43': 0.74; 'billing': 0.74; 'day': 0.76; 'subject:this': 0.83; 'dates:': 0.84; 'much,': 0.84; 'received:50.22': 0.84; 'hand,': 0.93 Date: Sun, 6 Apr 2014 07:31:05 -0500 From: Tim Chase To: Ben Finney Subject: Re: How can I parse this correctly? In-Reply-To: <85zjjz141k.fsf@benfinney.id.au> References: <85zjjz141k.fsf@benfinney.id.au> X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: authenticated_id: tim@thechases.com Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1396789496 news.xs4all.nl 2884 [2001:888:2000:d::a6]:60519 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69767 On 2014-04-06 14:21, Ben Finney wrote: > I assume you mean you will be creating =E2=80=98datetime.date=E2=80=99 ob= jects. What > will you set as the month and day? >=20 > Alternatively, if you just want to do integer arithmetic on the > year, you don't need the =E2=80=98datetime=E2=80=99 module at all. Even if you do the arithmetic by hand, it's still nice to use the datetime module to parse for sane dates: year =3D 2004 month =3D 2 day =3D 29 what should month & day be if you increment/decrement the year by one? The datetime module will throw a ValueError which is a nice check for a valid date. I've had to do things like this in a loop to sanitize dates (depending on which field is being inc/dec'ed, by how much, and which direction it's going) and it's nice to just have a y,m,d =3D initial =3D some_date.timetuple()[:3] # result =3D None while result is None: y,m,d =3D twiddle(y, m, d) try: result =3D datetime(y, m, d) except ValueError: result =3D None log.info("Shifted %r -> %r", initial, result) where twiddle() is whatever business logic I need for this particular case. For me usually, it's adjusting by one month for billing purposes. -tkc