Datetime utility functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Moore

    Datetime utility functions

    I was just writing some code which did date/time manipulations, and I
    found that the Python 2.3 datetime module does not supply a number of
    fairly basic functions. I understand the reasoning (the datetime
    module provides a set of datatypes, and doesn't attempt to get into
    the murky waters of date algorithms) but as these things can be quite
    tricky to get right, I was wondering if anyone has already implemented
    any date algorithms, or alternatively if I'd missed some easy way of
    doing what I'm after.

    My specific requirements were:

    1. Get the last day of the month contatining a given date(time). I
    really was surprised to find this one missing, as it seems to me that
    the datetime module must know what the last day for each month is, so
    exposing it wouldn't have been hard.

    2. Add a number of months to a date. This is messy, as there are
    options (what is one month after 31st Jan). The trouble is that the
    calculation, while simple, is tricky to get right (month is 1-based,
    so off-by-1 errors are easy to make, and divmod isn't as useful as
    you'd first think).

    Other "nice to have" functions which I didn't need for this program,
    but which I have found useful in the past, are

    - Round a date(time) to a {year,month,day ,quarter,week,. ..} Or
    truncate. Or (occasionally) chop to the next higher boundary
    (ceiling).
    - Calculate the number of {years,months,d ays,...} between two dates.
    (Which is more or less the equivalent of rounding a timedelta).

    These latter two aren't very well defined right now, because I have no
    immediate use case.

    In the absence of anything else, I'll probably write a "date
    utilities" module for myself at some point. But I'd hate to be
    reinventing the wheel.

    Paul.
  • John Roth

    #2
    Re: Datetime utility functions


    "Paul Moore" <paul.moore@ato sorigin.com> wrote in message
    news:182bcf76.0 309150707.3a8c0 482@posting.goo gle.com...[color=blue]
    > I was just writing some code which did date/time manipulations, and I
    > found that the Python 2.3 datetime module does not supply a number of
    > fairly basic functions. I understand the reasoning (the datetime
    > module provides a set of datatypes, and doesn't attempt to get into
    > the murky waters of date algorithms) but as these things can be quite
    > tricky to get right, I was wondering if anyone has already implemented
    > any date algorithms, or alternatively if I'd missed some easy way of
    > doing what I'm after.
    >
    > My specific requirements were:
    >
    > 1. Get the last day of the month contatining a given date(time). I
    > really was surprised to find this one missing, as it seems to me that
    > the datetime module must know what the last day for each month is, so
    > exposing it wouldn't have been hard.[/color]

    I'm kind of surprised to see that it's missing, too.
    [color=blue]
    > 2. Add a number of months to a date. This is messy, as there are
    > options (what is one month after 31st Jan). The trouble is that the
    > calculation, while simple, is tricky to get right (month is 1-based,
    > so off-by-1 errors are easy to make, and divmod isn't as useful as
    > you'd first think).[/color]

    That's application dependent. If a bond, for example, has interest
    payable on the 30th of the month, you probably want the 30th,
    except in February you want the last day of the month. However,
    the contract may specify something else. And in no case do you
    want the date to suddenly change to the 28th because you went
    through February.
    [color=blue]
    > Other "nice to have" functions which I didn't need for this program,
    > but which I have found useful in the past, are
    >
    > - Round a date(time) to a {year,month,day ,quarter,week,. ..} Or
    > truncate. Or (occasionally) chop to the next higher boundary
    > (ceiling).
    > - Calculate the number of {years,months,d ays,...} between two dates.
    > (Which is more or less the equivalent of rounding a timedelta).
    >
    > These latter two aren't very well defined right now, because I have no
    > immediate use case.[/color]

    Most of these don't have well defined, globally useful use cases.
    It's heavily application dependent what you want out of these.
    [color=blue]
    > In the absence of anything else, I'll probably write a "date
    > utilities" module for myself at some point. But I'd hate to be
    > reinventing the wheel.[/color]

    There's a very well regarded date module out there in the
    Vaults of Parnassus. The name escapes me at the moment,
    but a bit of spelunking through the Vaults will turn up several
    date routines that may do what you want.

    John Roth
    [color=blue]
    >
    > Paul.[/color]


    Comment

    • Paul Moore

      #3
      Re: Datetime utility functions

      "John Roth" <newsgroups@jhr othjr.com> writes:
      [color=blue]
      > "Paul Moore" <paul.moore@ato sorigin.com> wrote in message
      > news:182bcf76.0 309150707.3a8c0 482@posting.goo gle.com...[/color]
      [color=blue][color=green]
      >> 1. Get the last day of the month contatining a given date(time). I
      >> really was surprised to find this one missing, as it seems to me that
      >> the datetime module must know what the last day for each month is, so
      >> exposing it wouldn't have been hard.[/color]
      >
      > I'm kind of surprised to see that it's missing, too.[/color]

      The best solution I could find was

      def month_end(dt):
      # Get the next month
      y, m = dt.year, dt.month
      if m == 12:
      y += 1
      m = 1
      else:
      m += 1

      # Use replace to cater for both datetime and date types. This
      # leaves the time component of a datetime unchanged - it's
      # arguable whether this is the right thing.

      return dt.replace(year =y, month=m, day=1) - datetime.timede lta(days=1)

      It's not hard - but it's mildly tricky (I made a few false starts and
      some silly off-by-one errors) and I'd much rather grab it from a
      library than make the same mistakes next time I need it.
      [color=blue][color=green]
      >> 2. Add a number of months to a date. This is messy, as there are
      >> options (what is one month after 31st Jan). The trouble is that the
      >> calculation, while simple, is tricky to get right (month is 1-based,
      >> so off-by-1 errors are easy to make, and divmod isn't as useful as
      >> you'd first think).[/color]
      >
      > That's application dependent.[/color]

      True. But for "naive" use, a simple definition does. This is in line
      with the datetime module's philosophy of not trying to cater for
      "advanced" uses, but to provide something useful for straightforward
      use. In this particular case, I'd argue that the obvious definition
      (same day number N months on) where applicable, plus a well-documented
      "reasonable " answer for the edge cases (eg, Jan 31 plus 1 month) is
      useful. In practice, I suspect that 99% of cases involve adding a
      number of months to either the first or the last of a month.
      [color=blue]
      > If a bond, for example, has interest payable on the 30th of the
      > month, you probably want the 30th, except in February you want the
      > last day of the month. However, the contract may specify something
      > else. And in no case do you want the date to suddenly change to the
      > 28th because you went through February.[/color]

      But that's not so much a case of adding a month, as a more complex
      concept, a "repeating date". Nevertheless, I take your point.
      [color=blue][color=green]
      >> In the absence of anything else, I'll probably write a "date
      >> utilities" module for myself at some point. But I'd hate to be
      >> reinventing the wheel.[/color]
      >
      > There's a very well regarded date module out there in the
      > Vaults of Parnassus. The name escapes me at the moment,
      > but a bit of spelunking through the Vaults will turn up several
      > date routines that may do what you want.[/color]

      I guess you're thinking of mxDateTime. I'm aware of this, and agree
      that it's pretty comprehensive. I don't really know why I prefer not
      to use it - partly it's just a case of reducing dependencies, also
      there are already so many date types in my code (COM, cx_Oracle,
      datetime) that I am reluctant to add another - there is already far
      too much code devoted to converting representations ...

      Thanks for the comments,
      Paul.
      --
      This signature intentionally left blank

      Comment

      • John Roth

        #4
        Re: Datetime utility functions


        "Paul Moore" <paul.moore@ato sorigin.com> wrote in message
        news:brtl7s6v.f sf@yahoo.co.uk. ..[color=blue]
        > "John Roth" <newsgroups@jhr othjr.com> writes:
        >[color=green]
        > > "Paul Moore" <paul.moore@ato sorigin.com> wrote in message
        > > news:182bcf76.0 309150707.3a8c0 482@posting.goo gle.com...[/color]
        >[color=green][color=darkred]
        > >> 1. Get the last day of the month contatining a given date(time). I
        > >> really was surprised to find this one missing, as it seems to me that
        > >> the datetime module must know what the last day for each month is, so
        > >> exposing it wouldn't have been hard.[/color]
        > >
        > > I'm kind of surprised to see that it's missing, too.[/color]
        >
        > The best solution I could find was
        >
        > def month_end(dt):
        > # Get the next month
        > y, m = dt.year, dt.month
        > if m == 12:
        > y += 1
        > m = 1
        > else:
        > m += 1[/color]

        This looks incomplete. You could use this to get the sequential date
        for the first of the next month, but you still have to subtract one day
        and then print out the day.
        [color=blue]
        >
        > # Use replace to cater for both datetime and date types. This
        > # leaves the time component of a datetime unchanged - it's
        > # arguable whether this is the right thing.
        >
        > return dt.replace(year =y, month=m, day=1) - datetime.timede lta(days=1)
        >
        > It's not hard - but it's mildly tricky (I made a few false starts and
        > some silly off-by-one errors) and I'd much rather grab it from a
        > library than make the same mistakes next time I need it.[/color]

        As I said, I don't see any obvious reason why they wouldn't accept
        a patch, if you want to submit it.
        [color=blue][color=green][color=darkred]
        > >> 2. Add a number of months to a date. This is messy, as there are
        > >> options (what is one month after 31st Jan). The trouble is that the
        > >> calculation, while simple, is tricky to get right (month is 1-based,
        > >> so off-by-1 errors are easy to make, and divmod isn't as useful as
        > >> you'd first think).[/color]
        > >
        > > That's application dependent.[/color]
        >
        > True. But for "naive" use, a simple definition does. This is in line
        > with the datetime module's philosophy of not trying to cater for
        > "advanced" uses, but to provide something useful for straightforward
        > use. In this particular case, I'd argue that the obvious definition
        > (same day number N months on) where applicable, plus a well-documented
        > "reasonable " answer for the edge cases (eg, Jan 31 plus 1 month) is
        > useful. In practice, I suspect that 99% of cases involve adding a
        > number of months to either the first or the last of a month.[/color]

        Except for the edge case I mention below, this is really too simple;
        it's just add one to the month and keep the same date. Hardly worth
        a method at all, especially if you have the "last day of month" method.
        [color=blue][color=green]
        > > If a bond, for example, has interest payable on the 30th of the
        > > month, you probably want the 30th, except in February you want the
        > > last day of the month. However, the contract may specify something
        > > else. And in no case do you want the date to suddenly change to the
        > > 28th because you went through February.[/color]
        >
        > But that's not so much a case of adding a month, as a more complex
        > concept, a "repeating date". Nevertheless, I take your point.[/color]

        Yes. When you've got something application dependent, they tend
        not to put in "naive" definitions. What's a 'naive' definition for one
        person
        is simply wrong for another.

        John Roth

        [color=blue]
        > Thanks for the comments,
        > Paul.
        > --[/color]


        Comment

        • Christos TZOTZIOY Georgiou

          #5
          Re: Datetime utility functions

          On 15 Sep 2003 08:07:07 -0700, rumours say that
          paul.moore@atos origin.com (Paul Moore) might have written:

          [about missing datetime functionality]
          [color=blue]
          >1. Get the last day of the month contatining a given date(time). I
          >really was surprised to find this one missing, as it seems to me that
          >the datetime module must know what the last day for each month is, so
          >exposing it wouldn't have been hard.[/color]

          I would guess that the simple ones were not included just because they
          are only a few lines each:

          def end_of_month(a_ date):
          year_inc, month_inc = divmod(a_date.m onth, 12)
          return a_date.__class_ _(a_date.year+y ear_inc, 1+month_inc, 1) - \
          datetime.timede lta(days=1)

          def month_days(a_da te):
          return end_of_month(a_ date).day

          Examples:
          [color=blue][color=green][color=darkred]
          >>> end_of_month(da tetime.date.tod ay())[/color][/color][/color]
          datetime.date(2 003, 9, 30)[color=blue][color=green][color=darkred]
          >>> end_of_month(da tetime.datetime (1972,2,11))[/color][/color][/color]
          datetime.dateti me(1972, 2, 29, 0, 0)[color=blue][color=green][color=darkred]
          >>> month_days(date time.date.today ())[/color][/color][/color]
          30[color=blue][color=green][color=darkred]
          >>> end_of_month(da tetime.date(197 7,12,15))[/color][/color][/color]
          datetime.date(1 977, 12, 31)

          The add_months_to_d ate seems not that hard too, but I don't have a need
          for one so far, so I haven't coded it :) My needs for datetime
          intervals are usually of the 'one month start - another month end' kind,
          so end_of_month is handy. If you do think these would be useful, feel
          free to use them (or your own versions) for a patch.

          Date / timedelta rounding / truncating to various units would have to
          take account of many different rules, and none would be very generic
          IMHO.
          --
          TZOTZIOY, I speak England very best,
          Microsoft Security Alert: the Matrix began as open source.

          Comment

          • Christos TZOTZIOY Georgiou

            #6
            Re: Datetime utility functions

            On Mon, 15 Sep 2003 20:44:08 +0100, rumours say that Paul Moore
            <paul.moore@ato sorigin.com> might have written:

            [find the end of the month]
            [color=blue]
            >The best solution I could find was
            >
            >def month_end(dt):
            > # Get the next month
            > y, m = dt.year, dt.month
            > if m == 12:
            > y += 1
            > m = 1
            > else:
            > m += 1
            >
            > # Use replace to cater for both datetime and date types. This
            > # leaves the time component of a datetime unchanged - it's
            > # arguable whether this is the right thing.
            >
            > return dt.replace(year =y, month=m, day=1) - datetime.timede lta(days=1)[/color]

            I sent my own version without having seen your own --and mine might seem
            obfuscated, compared to yours. Only a minor suggestion: don't use
            dt.replace, use dt.__class__ instead, since you wouldn't want your
            function to have side-effects (that is, don't destroy the actual object
            that dt is bound to.)
            --
            TZOTZIOY, I speak England very best,
            Microsoft Security Alert: the Matrix began as open source.

            Comment

            • Gustavo Niemeyer

              #7
              Re: Datetime utility functions

              > I was just writing some code which did date/time manipulations, and I[color=blue]
              > found that the Python 2.3 datetime module does not supply a number of[/color]
              [...]

              Your message arrived in the exact time.. :-)

              Have a look at this:



              I'll publish it somewhere an announce here once I get the
              time to do so.
              [color=blue]
              > My specific requirements were:
              >
              > 1. Get the last day of the month contatining a given date(time). I
              > really was surprised to find this one missing, as it seems to me that
              > the datetime module must know what the last day for each month is, so
              > exposing it wouldn't have been hard.[/color]

              I don't understand this one. The last day of the month containing
              a given datetime? A datetime is absolute, how would it be "contained"
              in something else?

              --
              Gustavo Niemeyer
              The owner of this domain has not yet uploaded their website.


              Comment

              • Christos TZOTZIOY Georgiou

                #8
                Re: Datetime utility functions

                On Tue, 16 Sep 2003 11:28:58 -0400, rumours say that "Tim Peters"
                <tim.one@comcas t.net> might have written:
                [color=blue]
                >[Christos TZOTZIOY Georgiou][color=green]
                >> I sent my own version without having seen your own --and mine might
                >> seem obfuscated, compared to yours. Only a minor suggestion: don't
                >> use dt.replace, use dt.__class__ instead, since you wouldn't want your
                >> function to have side-effects (that is, don't destroy the actual
                >> object that dt is bound to.)[/color]
                >
                >It's OK to use replace: datetime and date (also time and timedelta) objects
                >are immutable -- their values can't be changed after initialization. In
                >particular, x.replace() doesn't mutate x.[/color]

                In some strange, unprecedented and inexplicable way, you are right...
                <sigh> ...again :) RTFM-F virus not removed yet.

                PS That bang thingie (!) in Ruby is pythonic.
                --
                TZOTZIOY, I speak England very best,
                Microsoft Security Alert: the Matrix began as open source.

                Comment

                • Carel Fellinger

                  #9
                  Re: Datetime utility functions

                  On Tue, Sep 16, 2003 at 05:19:50PM +0300, Christos TZOTZIOY Georgiou wrote:
                  ....[color=blue]
                  > I sent my own version without having seen your own --and mine might seem
                  > obfuscated, compared to yours.[/color]

                  you both use the same indirect way of getting to the end of the month.
                  why not do what datetime.c does, like:

                  _days_in_month = [
                  0, # unused; this vector uses 1-based indexing */
                  31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
                  ]

                  def _is_leap_year(y ear):
                  return year % 4 == 0 and (year % 100 !=0 or year % 400 == 0)

                  def days_in_month(y ear, month):
                  if month == 2 and _is_leap_year(y ear):
                  return 29
                  else:
                  return _days_in_month[month]

                  def end_of_month(d) :
                  date(d.y, d.m, days_in_month(d .y, d.m))


                  --
                  groetjes, carel

                  Comment

                  • Paul Moore

                    #10
                    Re: Datetime utility functions

                    Christos "TZOTZIOY" Georgiou <tzot@sil-tec.gr> writes:
                    [color=blue]
                    > I sent my own version without having seen your own --and mine might seem
                    > obfuscated, compared to yours. Only a minor suggestion: don't use
                    > dt.replace, use dt.__class__ instead, since you wouldn't want your
                    > function to have side-effects (that is, don't destroy the actual object
                    > that dt is bound to.)[/color]

                    dt.replace doesn't have side effects - it creates a new object. Also,
                    by using replace(), my version works with date, datetime, or
                    subclasses. Yours does too, but it sets the time component of a
                    datetime to zero. It's a matter of preference which behaviour is
                    "better", I guess.

                    Paul.
                    --
                    This signature intentionally left blank

                    Comment

                    • Paul Moore

                      #11
                      Re: Datetime utility functions

                      Carel Fellinger <carel.fellinge r@chello.nl> writes:
                      [color=blue]
                      > On Tue, Sep 16, 2003 at 05:19:50PM +0300, Christos TZOTZIOY Georgiou wrote:
                      > ...[color=green]
                      >> I sent my own version without having seen your own --and mine might seem
                      >> obfuscated, compared to yours.[/color]
                      >
                      > you both use the same indirect way of getting to the end of the month.
                      > why not do what datetime.c does, like:[/color]

                      Mainly because that seems even more like reinventing the wheel.
                      Writing code that already exists is something I dislike doing at the
                      best of times. Writing *tricky* code that already exists feels even
                      more unpleasant. (Yes, I know the leap year calculation isn't that
                      hard - but lots of people have got it wrong in the past...)

                      I guess that's my real issue. I know the datetime module already has
                      this information. But persuading it to tell me requires me to jump
                      through hoops, whereas reimplementing it feels like admitting defeat.

                      None of it's hard, though. I've spent more time on emails about the
                      issue than I'd ever need to spend in implementing anything :-)

                      Paul.
                      --
                      This signature intentionally left blank

                      Comment

                      • Christos TZOTZIOY Georgiou

                        #12
                        Re: Datetime utility functions

                        On Tue, 16 Sep 2003 20:26:01 +0100, rumours say that Paul Moore
                        <pf_moore@yahoo .co.uk> might have written:

                        [I suggesting dt.__class__() instead of dt.replace()]
                        [color=blue]
                        >dt.replace doesn't have side effects - it creates a new object. Also,
                        >by using replace(), my version works with date, datetime, or
                        >subclasses. Yours does too, but it sets the time component of a
                        >datetime to zero. It's a matter of preference which behaviour is
                        >"better", I guess.[/color]

                        You are right about dt.replace not having side-effects, just like some
                        anonymous<wink> major python contributor commented. Also .replace feels
                        "better" since it doesn't lose the time information.

                        PS ...but I still like my use of divmod() ;-)
                        --
                        TZOTZIOY, I speak England very best,
                        Microsoft Security Alert: the Matrix began as open source.

                        Comment

                        • Gustavo Niemeyer

                          #13
                          Re: Datetime utility functions

                          > > you both use the same indirect way of getting to the end of the month.[color=blue][color=green]
                          > > why not do what datetime.c does, like:[/color]
                          >
                          > Mainly because that seems even more like reinventing the wheel.
                          > Writing code that already exists is something I dislike doing at the[/color]
                          [...]

                          Why not using calendar.monthr ange()?

                          --
                          Gustavo Niemeyer
                          The owner of this domain has not yet uploaded their website.


                          Comment

                          • Dan Bishop

                            #14
                            Re: Datetime utility functions

                            paul.moore@atos origin.com (Paul Moore) wrote in message news:<182bcf76. 0309150707.3a8c 0482@posting.go ogle.com>...[color=blue]
                            > I was just writing some code which did date/time manipulations, and I
                            > found that the Python 2.3 datetime module does not supply a number of
                            > fairly basic functions. I understand the reasoning (the datetime
                            > module provides a set of datatypes, and doesn't attempt to get into
                            > the murky waters of date algorithms) but as these things can be quite
                            > tricky to get right, I was wondering if anyone has already implemented
                            > any date algorithms, or alternatively if I'd missed some easy way of
                            > doing what I'm after.
                            >
                            > [description of needed functions][/color]

                            I wrote a module just like that at my last job. Given a date object,
                            you could find the first or last day of the month, quarter, or year.
                            There were also functions to answer questions like "What was the date
                            5 months ago?" or "What date is the first Monday after October 8?"

                            Unfortunately, I don't have a copy of the source code here.

                            Comment

                            • Invalid User

                              #15
                              Re: Datetime utility functions

                              Try 'mxDateTime' available at

                              eGenix.com is specialized in high-performance, professional quality Python database products and extensions.


                              It most likely has what you are looking for.

                              Paul Moore wrote:[color=blue]
                              > I was just writing some code which did date/time manipulations, and I
                              > found that the Python 2.3 datetime module does not supply a number of
                              > fairly basic functions. I understand the reasoning (the datetime
                              > module provides a set of datatypes, and doesn't attempt to get into
                              > the murky waters of date algorithms) but as these things can be quite
                              > tricky to get right, I was wondering if anyone has already implemented
                              > any date algorithms, or alternatively if I'd missed some easy way of
                              > doing what I'm after.
                              >
                              > My specific requirements were:
                              >
                              > 1. Get the last day of the month contatining a given date(time). I
                              > really was surprised to find this one missing, as it seems to me that
                              > the datetime module must know what the last day for each month is, so
                              > exposing it wouldn't have been hard.
                              >
                              > 2. Add a number of months to a date. This is messy, as there are
                              > options (what is one month after 31st Jan). The trouble is that the
                              > calculation, while simple, is tricky to get right (month is 1-based,
                              > so off-by-1 errors are easy to make, and divmod isn't as useful as
                              > you'd first think).
                              >
                              > Other "nice to have" functions which I didn't need for this program,
                              > but which I have found useful in the past, are
                              >
                              > - Round a date(time) to a {year,month,day ,quarter,week,. ..} Or
                              > truncate. Or (occasionally) chop to the next higher boundary
                              > (ceiling).
                              > - Calculate the number of {years,months,d ays,...} between two dates.
                              > (Which is more or less the equivalent of rounding a timedelta).
                              >
                              > These latter two aren't very well defined right now, because I have no
                              > immediate use case.
                              >
                              > In the absence of anything else, I'll probably write a "date
                              > utilities" module for myself at some point. But I'd hate to be
                              > reinventing the wheel.
                              >
                              > Paul.[/color]

                              Comment

                              Working...