Re: Datetime utility functions

Discussion in 'Python' started by Gustavo Niemeyer, Sep 16, 2003.

  1. > > Have a look at this:
    >
    > I like that. I'm not entirely happy with the name of the class, but as
    > I can't come up with a better suggestion, I shouldn't criticise...


    The name explains it pretty well. It's like timedelta, but it's
    a "relative" delta, since how much it will advance will depend on
    the date you apply it to.

    [...]
    > Sorry, I didn't explain that very well. Imagine an application to report
    > stats over a period. The user can specify a date, and the report will be
    > for a calendar month, containing the user's required date. So my code
    > will be something like
    >
    > start = user_date.replace(day=1) # Start of the month
    > end = "Last day of the month"


    Oh! Ok.. I understand now.

    [...]
    > With your "relativedelta" I could do this pretty easily, by
    >
    > end = start + relativedelta(months=+1, days=-1)


    Hummm.. no. Look:

    >>> date.today()+relativedelta(months=+1, days=-1)

    datetime.date(2003, 10, 15)

    Instead, it should be done by:

    >>> date.today()+relativedelta(months=+1, day=31)


    Notice that it works also for other months:

    >>> date.today()+relativedelta(month=2, day=31)

    datetime.date(2003, 2, 28)

    This is described in step 4 of the documentation I pasted
    in the mail.

    > but I still feel that I'd need to have an explanatory comment. The
    > datetime module *knows* the number of days in the month - I'd just
    > like it to tell *me*.


    Have you noticed that this is also offered by the calendar module?

    >>> import calendar
    >>> calendar.monthrange(2003, 9)

    (0, 30)

    --
    Gustavo Niemeyer
    http://niemeyer.net
    Gustavo Niemeyer, Sep 16, 2003
    #1
    1. Advertising

  2. Gustavo Niemeyer

    Paul Moore Guest

    Gustavo Niemeyer <> writes:

    >> With your "relativedelta" I could do this pretty easily, by
    >>
    >> end = start + relativedelta(months=+1, days=-1)

    >
    > Hummm.. no. Look:
    >
    > >>> date.today()+relativedelta(months=+1, days=-1)

    > datetime.date(2003, 10, 15)


    Guess that proves I was right - I do need a comment. It's just that
    "this is wrong" is more appropriate than an explanation :)

    > Instead, it should be done by:
    >
    > >>> date.today()+relativedelta(months=+1, day=31)

    >
    > Notice that it works also for other months:
    >
    > >>> date.today()+relativedelta(month=2, day=31)

    > datetime.date(2003, 2, 28)


    Hmm. I can't work out whether I find this elegant or repellent. I
    think I'd prefer a ValueError, like datetime does, but that leaves the
    issue open again...

    >> The datetime module *knows* the number of days in the month - I'd
    >> just like it to tell *me*.

    >
    > Have you noticed that this is also offered by the calendar module?
    >
    > >>> import calendar
    > >>> calendar.monthrange(2003, 9)

    > (0, 30)


    No I hadn't! OK, consider the problem solved. I can't hope for better
    than something in the standard library.

    Paul.
    --
    This signature intentionally left blank
    Paul Moore, Sep 16, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    704
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    532
    Tim Peters
    Sep 9, 2003
  3. mp
    Replies:
    1
    Views:
    386
    John Machin
    Jul 28, 2006
  4. Martin
    Replies:
    0
    Views:
    333
    Martin
    Dec 27, 2008
  5. Replies:
    2
    Views:
    754
    M.-A. Lemburg
    Jan 6, 2009
Loading...

Share This Page