[Python-checkins] python/nondist/sandbox/datetime PSF.py,NONE,1.1 EU.py,1.1,1.2 US.py,1.11,1.12 dateutil.py,1.1,1.2

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Sat, 28 Dec 2002 10:16:49 -0800


Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory sc8-pr-cvs1:/tmp/cvs-serv17517

Modified Files:
	EU.py US.py dateutil.py 
Added Files:
	PSF.py 
Log Message:
A vital utility for forgetful PSF directors.


--- NEW FILE: PSF.py ---
#! /usr/bin/env python

"""PSF year [timezone] ...

Display the start times of PSF Board meetings for the given year, in the
given timezones.  The Eastern time is always shown.
"""

from datetime import datetimetz
from dateutil import TUESDAY, weekday_of_month

from US import Eastern, Central, Mountain, Pacific
from EU import UTC, London, Amsterdam

alltzs = {'Eastern': Eastern,
          'Central': Central,
          'Mountain': Mountain,
          'Pacific': Pacific,
          'UTC': UTC,
          'London': London,
          'Amsterdam': Amsterdam,
         }

# A vector of 12 datetimetzs, all in Eastern.
def psf_times_for_a_year(year):
    # 1pm Eastern on the second Tuesday of the month.
    base = datetimetz(year, 1, 1, 13, tzinfo=Eastern)
    return [weekday_of_month(TUESDAY, base.replace(month=i), 1)
            for i in range(1, 13)]

# Eastern is always displayed first.
def display_psf_times_for_a_year(year, tzs):
    tzs = list(tzs)
    if Eastern in tzs:
        tzs.remove(Eastern)
    print "PSF Board meeting times for", year
    print
    raw = psf_times_for_a_year(year)
    # Note that the use of strftime limits us to years no earlier than
    # 1900.  This may be a problem for Guido on extended time trips.
    for date in raw:
        print date.strftime("%a %b-%d %H:%M %Z%z "),
        for tz in tzs:
            local = date.astimezone(tz)
            if local.date() == date.date():
                print local.strftime("%H:%M %Z%z "),
            else:
                # It's on a different day for them.
                local.strftime("%a %b-%d %H:%M %Z%z "),
        print

def main():
    import sys
    if len(sys.argv) < 2:
        print >> sys.stderr, "need a year argument"
        sys.exit(-1)
    year = int(sys.argv[1])
    tznames = sys.argv[2:]
    tzs = [alltzs.get(name, None) for name in tznames]
    if None in tzs:
        print >> sys.stderr, "Sorry, the only timezone names I know are", \
                             alltzs.keys()
        sys.exit(-1)
    display_psf_times_for_a_year(year, tzs)

if __name__ == '__main__':
    main()
Index: EU.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/EU.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** EU.py	28 Dec 2002 03:53:53 -0000	1.1
--- EU.py	28 Dec 2002 18:16:46 -0000	1.2
***************
*** 18,29 ****
  
  from datetime import date, time, timedelta, datetime, datetimetz, tzinfo
! 
! def last_sunday_in(year, month):
!     assert month in [3, 10] # This only works for 31-day months!
!     d = date(year, month, 31)
!     w = d.weekday() # Mon = 0, Sun = 6
!     if w != 6:
!         d -= timedelta(days=w+1)
!     return d
  
  DAY = timedelta(days=1)
--- 18,22 ----
  
  from datetime import date, time, timedelta, datetime, datetimetz, tzinfo
! from dateutil import MARCH, OCTOBER, SUNDAY, weekday_of_month
  
  DAY = timedelta(days=1)
***************
*** 64,69 ****
  
      def dst(self, dt):
!         dston = last_sunday_in(dt.year, 3)
!         dstoff = last_sunday_in(dt.year, 10)
          d = dt.date()
          # Get a quick result on dates not near a DST switch:
--- 57,62 ----
  
      def dst(self, dt):
!         dston = weekday_of_month(SUNDAY, date(dt.year, MARCH, 1), -1)
!         dstoff = weekday_of_month(SUNDAY, date(dt.year, OCTOBER, 1), -1)
          d = dt.date()
          # Get a quick result on dates not near a DST switch:

Index: US.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/US.py,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -d -r1.11 -r1.12
*** US.py	27 Dec 2002 21:31:40 -0000	1.11
--- US.py	28 Dec 2002 18:16:46 -0000	1.12
***************
*** 3,11 ****
  from datetime import timedelta
  
! def _first_sunday_at_or_after(dt):
!     days_to_go = 6 - dt.weekday()
!     if days_to_go:
!         dt += timedelta(days_to_go)
!     return dt
  
  class USTimeZone(tzinfo):
--- 3,9 ----
  from datetime import timedelta
  
! from dateutil import SUNDAY, APRIL, OCTOBER, weekday_of_month
! 
! __all__ = ['USTimeZone', 'Eastern', 'Central', 'Mountain', 'Pacific']
  
  class USTimeZone(tzinfo):
***************
*** 51,57 ****
      zero = timedelta(0)
      # DST starts at 2am (standard time) on the first Sunday in April.
!     start = datetime(1, 4, 1, 2)
      # and ends at 2am (DST time; 1am standard time) on the last Sunday of Oct.
!     end = datetime(1, 10, 25, 2)
  
      def __init__(self, stdhours, stdname, dstname):
--- 49,55 ----
      zero = timedelta(0)
      # DST starts at 2am (standard time) on the first Sunday in April.
!     start = datetime(1, APRIL, 1, 2)
      # and ends at 2am (DST time; 1am standard time) on the last Sunday of Oct.
!     end = datetime(1, OCTOBER, 1, 2)
  
      def __init__(self, stdhours, stdname, dstname):
***************
*** 86,94 ****
  
          # Find first Sunday in April.
!         start = _first_sunday_at_or_after(self.start.replace(year=dt.year))
          assert start.weekday() == 6 and start.month == 4 and start.day <= 7
  
          # Find last Sunday in October.
!         end = _first_sunday_at_or_after(self.end.replace(year=dt.year))
          assert end.weekday() == 6 and end.month == 10 and end.day >= 25
  
--- 84,92 ----
  
          # Find first Sunday in April.
!         start = weekday_of_month(SUNDAY, self.start.replace(year=dt.year), 0)
          assert start.weekday() == 6 and start.month == 4 and start.day <= 7
  
          # Find last Sunday in October.
!         end = weekday_of_month(SUNDAY, self.end.replace(year=dt.year), -1)
          assert end.weekday() == 6 and end.month == 10 and end.day >= 25
  
***************
*** 104,112 ****
              return self.zero
  
  
  
  brainbuster_test = """
- >>> Eastern = USTimeZone(-5, "EST", "EDT")
- 
  >>> def printstuff(d):
  ...     print d
--- 102,112 ----
              return self.zero
  
+ Eastern  = USTimeZone(-5, "EST", "EDT")
+ Central  = USTimeZone(-6, "CST", "CDT")
+ Mountain = USTimeZone(-7, "MST", "MDT")
+ Pacific  = USTimeZone(-8, "PST", "PDT")
  
  
  brainbuster_test = """
  >>> def printstuff(d):
  ...     print d

Index: dateutil.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/dateutil.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** dateutil.py	28 Dec 2002 06:14:30 -0000	1.1
--- dateutil.py	28 Dec 2002 18:16:46 -0000	1.2
***************
*** 1,12 ****
  import datetime
  
  MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY = range(7)
! DAYORD2NAME = "Mon Tue Wed Thu Fri Sat Sun".split()
  
  _DAY = datetime.timedelta(1)
  
- # These all take dt arguments of type date, datetime, or datetimetz, and
- # those that return a date-like result return one of the same type as the
- # input dt.
  
  def is_leap_year(dt):
--- 1,15 ----
+ # These all take dt arguments of type date, datetime, or datetimetz, and
+ # those that return a date-like result return one of the same type as the
+ # input dt.
+ 
  import datetime
  
  MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY = range(7)
! 
! (JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNK,
!  JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER) = range(1, 13)
  
  _DAY = datetime.timedelta(1)
  
  
  def is_leap_year(dt):
***************
*** 39,46 ****
  
      if dt.month == 12:
!         next = datetime.date(dt.year+1, 1, 1)
      else:
          next = datetime.date(dt.year, dt.month+1, 1)
!     return next.toordinal() - dt.replace(day=1).toordinal()
  
  def first_weekday_on_or_after(weekday, dt):
--- 42,49 ----
  
      if dt.month == 12:
!         return 31
      else:
          next = datetime.date(dt.year, dt.month+1, 1)
!         return next.toordinal() - dt.replace(day=1).toordinal()
  
  def first_weekday_on_or_after(weekday, dt):
***************
*** 90,94 ****
      return dt
  
! def weekday_of_month(index, weekday, dt):
      """Return the index'th day of kind weekday in date's month.
  
--- 93,97 ----
      return dt
  
! def weekday_of_month(weekday, dt, index):
      """Return the index'th day of kind weekday in date's month.
  
***************
*** 103,107 ****
      >>> base = datetime.datetime(2002, 11, 25, 13, 22, 44)
      >>> for index in range(5):
!     ...     print index, weekday_of_month(index, SUNDAY, base).ctime()
      0 Sun Nov  3 13:22:44 2002
      1 Sun Nov 10 13:22:44 2002
--- 106,110 ----
      >>> base = datetime.datetime(2002, 11, 25, 13, 22, 44)
      >>> for index in range(5):
!     ...     print index, weekday_of_month(SUNDAY, base, index).ctime()
      0 Sun Nov  3 13:22:44 2002
      1 Sun Nov 10 13:22:44 2002
***************
*** 112,116 ****
      Start from the end of the month instead:
      >>> for index in range(-1, -6, -1):
!     ...     print index, weekday_of_month(index, SUNDAY, base).ctime()
      -1 Sun Nov 24 13:22:44 2002
      -2 Sun Nov 17 13:22:44 2002
--- 115,119 ----
      Start from the end of the month instead:
      >>> for index in range(-1, -6, -1):
!     ...     print index, weekday_of_month(SUNDAY, base, index).ctime()
      -1 Sun Nov 24 13:22:44 2002
      -2 Sun Nov 17 13:22:44 2002