PR 29 from Werner

- make CDate compatible with py2/py3
- add documentation to CDate and add Phoenix tags
- create some unittests for CDate

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxPython/Phoenix/trunk@75591 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2014-01-11 21:39:23 +00:00
parent bf7e8090d6
commit 6ad7622dec
2 changed files with 154 additions and 48 deletions

39
unittests/test_cdate.py Normal file
View File

@@ -0,0 +1,39 @@
import imp_unittest, unittest
import wtc
import wx.lib.CDate as cdate
import wx.lib.six as six
class CdateTests(wtc.WidgetTestCase):
def test_CdateDateCtor(self):
cdate.Date(2014, 1, 31)
def test_CdateNowCtor(self):
cdate.now()
def test_CdateIsleapTrue(self):
l1 = cdate.isleap(2012)
self.assertTrue(l1, msg='Expected a leap year')
def test_CdateIsleapFalse(self):
l2 = cdate.isleap(2013)
self.assertFalse(l2, msg='Expected a non leap year')
def test_CdateJulianday(self):
bd = cdate.Date(2014, 1, 10)
jd = cdate.julianDay(bd.year, bd.month, bd.day)
self.assertTrue(jd == bd.julian,
msg='Expected them to be equal')
def test_CdateDayofweek(self):
jd = cdate.julianDay(2014, 1, 10)
dw = cdate.dayOfWeek(jd)
self.assertTrue(dw == 4, msg='Expected "4" assuming Monday is 1, got %s' % dw)
#---------------------------------------------------------------------------
if __name__ == '__main__':
unittest.main()

View File

@@ -6,38 +6,51 @@
# Created: # Created:
# Version 0.2 08-Nov-1999 # Version 0.2 08-Nov-1999
# Licence: wxWindows license # Licence: wxWindows license
# Tags: phoenix-port, py3-port, documented, unittest
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# Updated: 01-Dec-2004 # Updated: 01-Dec-2004
# Action: Cast the year variable to an integer under the Date Class # Action: Cast the year variable to an integer under the Date Class
# Reason: When the year was compared in the isleap() function, if it was # Reason: When the year was compared in the isleap() function, if it was
# in a string format, then an error was raised. # in a string format, then an error was raised.
#
"""Date and calendar classes and date utitility methods."""
import time import time
Month = {2: 'February', 3: 'March', None: 0, 'July': 7, 11:
'November', 'December': 12, 'June': 6, 'January': 1, 'September': 9, Month = {0: None,
'August': 8, 'March': 3, 'November': 11, 'April': 4, 12: 'December', 1: 'January', 2: 'February', 3: 'March',
'May': 5, 10: 'October', 9: 'September', 8: 'August', 7: 'July', 6: 4: 'April', 5: 'May', 6: 'June',
'June', 5: 'May', 4: 'April', 'October': 10, 'February': 2, 1: 7: 'July', 8: 'August', 9: 'September',
'January', 0: None} 10: 'October', 11: 'November', 12: 'December'}
# Number of days per month (except for February in leap years) # Number of days per month (except for February in leap years)
mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# Full and abbreviated names of weekdays # Full and abbreviated names of weekdays
day_name = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] day_name = ['Sunday', 'Monday', 'Tuesday', 'Wednesday',
day_abbr = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', ] 'Thursday', 'Friday', 'Saturday']
day_abbr = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri',
'Sat']
# Return number of leap years in range [y1, y2)
# Assume y1 <= y2 and no funny (non-leap century) years
def leapdays(y1, y2): def leapdays(y1, y2):
return (y2+3)/4 - (y1+3)/4 """
Return number of leap years in range [y1, y2]
Assume y1 <= y2 and no funny (non-leap century) years
"""
return (y2 + 3) / 4 - (y1 + 3) / 4
# Return 1 for leap years, 0 for non-leap years
def isleap(year): def isleap(year):
"""Verify if year is a leap year.
:param int `year`: the year to check
:return: True or False
"""
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
def FillDate(val): def FillDate(val):
s = str(val) s = str(val)
if len(s) < 2: if len(s) < 2:
@@ -46,25 +59,33 @@ def FillDate(val):
def julianDay(year, month, day): def julianDay(year, month, day):
b = 0L """Convert a date to Julian
year, month, day = long(year), long(month), long(day)
if month > 12L: :param int `year`: the year
year = year + month/12L :param int `month`: the month
month = month%12 :param int `day`: the day
elif month < 1L:
:returns: the julian date number
"""
b = 0
if month > 12:
year = year + month / 12
month = month % 12
elif month < 1:
month = -month month = -month
year = year - month/12L - 1L year = year - month / 12 - 1
month = 12L - month%12L month = 12 - month % 12
if year > 0L: if year > 0:
yearCorr = 0L yearCorr = 0
else: else:
yearCorr = 3L yearCorr = 3
if month < 3L: if month < 3:
year = year - 1L year = year - 1
month = month + 12L month = month + 12
if year*10000L + month*100L + day > 15821014L: if year * 10000 + month * 100 + day > 15821014:
b = 2L - year/100L + year/400L b = 2 - year / 100 + year / 400
return (1461L*year - yearCorr)/4L + 306001L*(month + 1L)/10000L + day + 1720994L + b return (1461 * year - yearCorr) / 4 + 306001 * (month + 1) / 10000 + day + 1720994 + b
def TodayDay(): def TodayDay():
@@ -77,53 +98,99 @@ def TodayDay():
daywk = day_name[daywk] daywk = day_name[daywk]
return(daywk) return(daywk)
def FormatDay(value): def FormatDay(value):
date = FromFormat(value) date = FromFormat(value)
daywk = DateCalc.dayOfWeek(date) daywk = DateCalc.dayOfWeek(date)
daywk = day_name[daywk] daywk = day_name[daywk]
return(daywk) return(daywk)
def FromJulian(julian): def FromJulian(julian):
julian = long(julian) """Convert a julian date
if (julian < 2299160L):
b = julian + 1525L :param int `julian`: the julian date to convert
:returns: year, month day as integers
"""
if (julian < 2299160):
b = julian + 1525
else: else:
alpha = (4L*julian - 7468861L)/146097L alpha = (4 * julian - 7468861) / 146097
b = julian + 1526L + alpha - alpha/4L b = julian + 1526 + alpha - alpha / 4
c = (20L*b - 2442L)/7305L c = (20 * b - 2442) / 7305
d = 1461L*c/4L d = 1461 * c / 4
e = 10000L*(b - d)/306001L e = 10000 * (b - d) / 306001
day = int(b - d - 306001L*e/10000L) day = int(b - d - 306001 * e / 10000)
if e < 14L: if e < 14:
month = int(e - 1L) month = int(e - 1)
else: else:
month = int(e - 13L) month = int(e - 13)
if month > 2: if month > 2:
year = c - 4716L year = c - 4716
else: else:
year = c - 4715L year = c - 4715
year = int(year) year = int(year)
return year, month, day return year, month, day
def dayOfWeek(julian): def dayOfWeek(julian):
return int((julian + 1L)%7L) """Get day of week from a julian day
param `julian`: the julian day
returns: the day of week as an integer and Monday = 1
"""
return int((julian + 1) % 7)
def daysPerMonth(month, year): def daysPerMonth(month, year):
"""Get the number of days for the month.
:param int `month`: the month
:param int `year`: the year
:returns: the number of days in the requested month
"""
ndays = mdays[month] + (month == 2 and isleap(year)) ndays = mdays[month] + (month == 2 and isleap(year))
return ndays return ndays
class now(object): class now(object):
"""A now date class"""
def __init__(self): def __init__(self):
"""
Default class constructor.
"""
self.date = time.localtime(time.time()) self.date = time.localtime(time.time())
self.year = self.date[0] self.year = self.date[0]
self.month = self.date[1] self.month = self.date[1]
self.day = self.date[2] self.day = self.date[2]
self.hour = self.date[3]
self.minutes = self.date[4]
self.secondes = self.date[5]
self.day_of_week = self.date[6]
self.julian = julianDay(self.year, self.month, self.day)
class Date(object): class Date(object):
"""A date class"""
def __init__(self, year, month, day): def __init__(self, year, month, day):
"""
Default class constructor.
:param `year`: the year as an int or string
:param `month`: the month as an int or string
:param `day`: the day as an int or string
"""
self.julian = julianDay(year, month, day) self.julian = julianDay(year, month, day)
self.month = month self.month = int(month)
self.year = int(year) self.year = int(year)
self.day = int(day)
self.day_of_week = dayOfWeek(self.julian) self.day_of_week = dayOfWeek(self.julian)
self.days_in_month = daysPerMonth(self.month, self.year) self.days_in_month = daysPerMonth(self.month, self.year)