Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #69767

Re: How can I parse this correctly?

Date 2014-04-06 07:31 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Re: How can I parse this correctly?
References <CAJUMiQsoNbNzDgUOkaQxFLGptTqKriD7DcXSeFwwu_-v4TKJKQ@mail.gmail.com> <85zjjz141k.fsf@benfinney.id.au>
Newsgroups comp.lang.python
Message-ID <mailman.8953.1396789496.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 2014-04-06 14:21, Ben Finney wrote:
> I assume you mean you will be creating ‘datetime.date’ objects. What
> will you set as the month and day?
> 
> Alternatively, if you just want to do integer arithmetic on the
> year, you don't need the ‘datetime’ 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 = 2004
  month = 2
  day = 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 = initial = some_date.timetuple()[:3] #
  result = None
  while result is None:
    y,m,d = twiddle(y, m, d)
    try:
      result = datetime(y, m, d)
    except ValueError:
      result = 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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: How can I parse this correctly? Tim Chase <python.list@tim.thechases.com> - 2014-04-06 07:31 -0500

csiph-web