calendar (date) iterator?

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

    #1

    calendar (date) iterator?

    Hi,

    I'm looking for useful starting points, suggestions, and sample code,
    to implement a calendar iterator. Simply, the iterator is seeded with
    an initial calendar date, e.g., "03-12-2006", and then subsequent
    calls to next return subsequent dates. The seed could be a standard
    calendar/datetime object.
    iter = calendarIterato r("03-12-2006")
    print iter.next()
    03-12-2006

    A useful extension would be to allow specifiation of iter intervals,
    e.g.,
    iter = calendarIterato r("03-12-2006 01:00:00", "minutes")
    print iter.next()
    03-12-2006 01:01:00

    Thanks in advance for pointers and suggestions!

  • Marcus

    #2
    Re: calendar (date) iterator?

    Oops-- the iter needs to work better than I do! :)
    iter = calendarIterato r("03-12-2006")
    print iter.next()
    >
    03-12-2006
    ^^^^^^^^^^
    03-13-2006

    iter = calendarIterato r("03-12-2006 01:00:00", "minutes")
    print iter.next()
    >
    03-12-2006 01:01:00
    ^^^^^^^^^^
    03-13-2006

    Comment

    • skip@pobox.com

      #3
      Re: calendar (date) iterator?


      MarcusI'm looking for useful starting points, suggestions, and sample
      Marcuscode, to implement a calendar iterator.

      Have you looked at dateutil?


      >>from dateutil.rrule import rrule, DAILY
      >>from dateutil.parser import parse
      >>rule = rrule(DAILY, count=5, dtstart=parse(" 2006-03-12"))
      >>for d in rule:
      ... print d
      ...
      2006-03-12 00:00:00
      2006-03-13 00:00:00
      2006-03-14 00:00:00
      2006-03-15 00:00:00
      2006-03-16 00:00:00

      Skip

      Comment

      • Marcus

        #4
        Re: calendar (date) iterator?

        On Mar 6, 3:19 pm, s...@pobox.com wrote:
        MarcusI'm looking for useful starting points, suggestions, and sample
        Marcuscode, to implement a calendar iterator.
        >
        Have you looked at dateutil?
        >

        >
        >>from dateutil.rrule import rrule, DAILY
        >>from dateutil.parser import parse
        >>rule = rrule(DAILY, count=5, dtstart=parse(" 2006-03-12"))
        >>for d in rule:
        ... print d
        ...
        2006-03-12 00:00:00
        2006-03-13 00:00:00
        2006-03-14 00:00:00
        2006-03-15 00:00:00
        2006-03-16 00:00:00
        >
        Skip

        This is exactly what I was looking for! Thanks so much!!

        Comment

        • Raymond Hettinger

          #5
          Re: calendar (date) iterator?

          [Marcus]
          I'm looking for useful starting points, suggestions, and sample code,
          to implement a calendar iterator. Simply, the iterator is seeded with
          an initial calendar date, e.g., "03-12-2006", and then subsequent
          calls to next return subsequent dates. The seed could be a standard
          calendar/datetime object.
          >
          iter = calendarIterato r("03-12-2006")
          print iter.next()
          >
          03-12-2006
          import datetime, time

          def calendarIterato r(start, fmt='%m-%d-%Y'):
          curr = datetime.date(* time.strptime(s tart, fmt)[:3])
          one = datetime.timede lta(1)
          while 1:
          curr += one
          yield curr

          if __name__ == '__main__':
          iter = calendarIterato r('3-12-2006')
          print iter.next()
          print iter.next()



          Comment

          Working...