date diff calc

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

    #1

    date diff calc

    Hi,

    Is there a more pythonic way to write or do a date difference
    calculation? I have as input two date fields in the form 'YYYY-MM-DD'

    TIA
    Terius



    from datetime import date

    bdl = '2004-11-25'.split('-')
    edl = '2004-11-30'.split('-')
    bd = date(int(bdl[0]), int(bdl[1]), int(bdl[2]))
    ed = date(int(edl[0]), int(edl[1]), int(edl[2]))

    print ed , '-', bd , '=', (ed-bd).days
  • Diez B. Roggisch

    #2
    Re: date diff calc

    > bdl = '2004-11-25'.split('-')[color=blue]
    > edl = '2004-11-30'.split('-')
    > bd = date(int(bdl[0]), int(bdl[1]), int(bdl[2]))
    > ed = date(int(edl[0]), int(edl[1]), int(edl[2]))[/color]

    using time.strptime and datetime.date.f romtimestamp is surely the better
    alternative, as it lets specify you the format by a string.
    [color=blue]
    > print ed , '-', bd , '=', (ed-bd).days[/color]

    I can't imagine what could possibly be easier than subtracting two dates -
    in fact, most times one has to jump through much more hoops to achieve
    these results, e.g. in java.

    --
    Regards,

    Diez B. Roggisch

    Comment

    • tertius

      #3
      Re: date diff calc

      Diez B. Roggisch wrote:[color=blue]
      >
      > I can't imagine what could possibly be easier than subtracting two dates -
      > in fact, most times one has to jump through much more hoops to achieve
      > these results, e.g. in java.
      >[/color]
      I'll try that.

      Thanks,
      T

      Comment

      • Skip Montanaro

        #4
        Re: date diff calc


        tertius> Is there a more pythonic way to write or do a date difference
        tertius> calculation? I have as input two date fields in the form
        tertius> 'YYYY-MM-DD'

        How about:

        import datetime
        import time

        bd = "2004-11-25"
        ed = "2004-11-30"

        start = datetime.date.f romtimestamp(ti me.strptime("%Y-%m-%d", bd))
        end = datetime.date.f romtimestamp(ti me.strptime("%Y-%m-%d", ed))

        print ed , '-', bd , '=', (end-start).days

        I think that for completeness' sake it would be nice if datetime exposed a
        fromstring method which accepted a string representing a date and a format
        specifier, e.g.:

        start = datetime.date.f romstring("%Y-%m-%d", "2004-11-25")
        end = datetime.date.f romstring("%Y-%m-%d", "2004-11-30")

        (Maybe it should be called "strptime", since that's the call it's saving.)

        The datetime.date object already exposes a strftime method for generating a
        formatted string output and will create date objects from both time.time()
        output (fromtimestamp) and "proleptic Gregorian ordinal"s (fromordinal).
        Looking at the datetime module docs, it's not at all obvious to me that the
        latter would be used all that often. I think inputs from strings would be
        much more common.

        Skip

        Comment

        • Rick Holbert

          #5
          Re: date diff calc

          Here's how to do it as a one-liner:

          python -c "import datetime; import time; print 'Only %d days until
          Christmas' % (datetime.date( 2004, 12, 25) -
          datetime.date.f romtimestamp(ti me.time())).day s"

          Here's a slightly shorter way of doing it:

          python -c "import time; print 'Only %f more days until Christmas' %
          (float(time.mkt ime(time.strpti me('2004-12-25', '%Y-%m-%d')) -
          time.time()) / 86400)"

          Comment

          • Tim Peters

            #6
            Re: date diff calc

            [Skip Montanaro][color=blue]
            > ...
            > The datetime.date object already exposes a strftime method for
            > generating a formatted string output and will create date objects
            > from both time.time() output (fromtimestamp) and "proleptic
            > Gregorian ordinal"s (fromordinal). Looking at the datetime module
            > docs, it's not at all obvious to me that the latter would be used all
            > that often.[/color]

            Then the part of the docs you're overlooking is the part explaining
            that "Calendrica l Calculations" bases all its calendar conversions on
            proleptic Gregorian ordinals. They're for people who want something
            other than the Gregorian calendar, and want it enough to write some
            code.
            [color=blue]
            > I think inputs from strings would be much more common.[/color]

            Me too, although it's a bottomless pit.

            guess-6-intended-meanings-for-1/2/3-before-breakfast-ly y'rs

            Comment

            • Peter Hansen

              #7
              Re: date diff calc

              Tim Peters wrote:[color=blue]
              > [Skip Montanaro][color=green]
              >>I think inputs from strings would be much more common.[/color]
              >
              > Me too, although it's a bottomless pit.
              >
              > guess-6-intended-meanings-for-1/2/3-before-breakfast-ly y'rs[/color]

              I think Skip was intending that the format string be mandatory,
              to avoid such ambiguity. At least, that's what I inferred from
              his example, where the format string came before the date string:

              start = datetime.date.f romstring("%Y-%m-%d", "2004-11-25")

              -Peter

              Comment

              • Tim Peters

                #8
                Re: date diff calc

                [Skip Montanaro][color=blue][color=green][color=darkred]
                >>> I think inputs from strings would be much more common.[/color][/color][/color]

                [Tim Peters][color=blue][color=green]
                >> Me too, although it's a bottomless pit.
                >>
                >> guess-6-intended-meanings-for-1/2/3-before-breakfast-ly y'rs[/color][/color]

                [Peter Hansen][color=blue]
                > I think Skip was intending that the format string be mandatory,
                > to avoid such ambiguity. At least, that's what I inferred from
                > his example, where the format string came before the date string:
                >
                > start = datetime.date.f romstring("%Y-%m-%d", "2004-11-25")[/color]

                It's still a bottomless pit -- ask Brett, who implemented the Python
                strptime <wink>. But now that we *have* a portable strptime
                implementation, perhaps it would be OK to teach datetime about it.

                OTOH, is that what people really want? For all I know,
                rfc822.getdate( ) or rfc822.getdate_ tz() are what's really wanted, or
                maybe some DWIM thing like Zope's date guessers.

                Does anyone want any of those enough to write the code, tests, and
                docs? If so, the first person to do so will probably win the debate
                ....

                Comment

                • Peter Hansen

                  #9
                  Re: date diff calc

                  Tim Peters wrote:[color=blue]
                  > [Peter Hansen][color=green]
                  >>I think Skip was intending that the format string be mandatory,
                  >>to avoid such ambiguity.[/color]
                  >
                  > It's still a bottomless pit -- ask Brett, who implemented the Python
                  > strptime <wink>.[/color]

                  True, I did overlook timezones at the time.

                  On the other hand, that's because *my* use cases for "simple"
                  fromstring() behaviour are all involving local time. When
                  I'm interested in non-local time, I would be happy having
                  to specify that behaviour in a more complex manner.
                  [color=blue]
                  > OTOH, is that what people really want? For all I know,
                  > rfc822.getdate( ) or rfc822.getdate_ tz() are what's really wanted, or
                  > maybe some DWIM thing like Zope's date guessers.[/color]

                  To each his own, although I think there's a hope here that
                  for those who might need/want a really simple solution,
                  95% of people have this in mind (pseudo-code):

                  class datetime.date:
                  def fromstring(form at, string):
                  ts = time.mktime(tim e.strptime(stri ng, format))
                  return datetime.date.f romtimestamp(ts )

                  The .fromtimestamp( ) methods already work only for local
                  time, and I haven't heard anyone complaining about that
                  either. (Hmm... haven't been listening either though. :-)

                  -Peter

                  Comment

                  • David Fraser

                    #10
                    Re: date diff calc

                    Peter Hansen wrote:[color=blue]
                    > Tim Peters wrote:
                    >[color=green]
                    >> [Peter Hansen]
                    >>[color=darkred]
                    >>> I think Skip was intending that the format string be mandatory,
                    >>> to avoid such ambiguity.[/color]
                    >>
                    >>
                    >> It's still a bottomless pit -- ask Brett, who implemented the Python
                    >> strptime <wink>.[/color]
                    >
                    >
                    > True, I did overlook timezones at the time.
                    >
                    > On the other hand, that's because *my* use cases for "simple"
                    > fromstring() behaviour are all involving local time. When
                    > I'm interested in non-local time, I would be happy having
                    > to specify that behaviour in a more complex manner.
                    >[color=green]
                    >> OTOH, is that what people really want? For all I know,
                    >> rfc822.getdate( ) or rfc822.getdate_ tz() are what's really wanted, or
                    >> maybe some DWIM thing like Zope's date guessers.[/color]
                    >
                    >
                    > To each his own, although I think there's a hope here that
                    > for those who might need/want a really simple solution,
                    > 95% of people have this in mind (pseudo-code):
                    >
                    > class datetime.date:
                    > def fromstring(form at, string):
                    > ts = time.mktime(tim e.strptime(stri ng, format))
                    > return datetime.date.f romtimestamp(ts )
                    >[/color]

                    Hear, hear, the above would be great!

                    Comment

                    Working...