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

View File

@@ -6,38 +6,51 @@
# Created:
# Version 0.2 08-Nov-1999
# Licence: wxWindows license
# Tags: phoenix-port, py3-port, documented, unittest
#----------------------------------------------------------------------------
# Updated: 01-Dec-2004
# 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
# 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
Month = {2: 'February', 3: 'March', None: 0, 'July': 7, 11:
'November', 'December': 12, 'June': 6, 'January': 1, 'September': 9,
'August': 8, 'March': 3, 'November': 11, 'April': 4, 12: 'December',
'May': 5, 10: 'October', 9: 'September', 8: 'August', 7: 'July', 6:
'June', 5: 'May', 4: 'April', 'October': 10, 'February': 2, 1:
'January', 0: None}
Month = {0: None,
1: 'January', 2: 'February', 3: 'March',
4: 'April', 5: 'May', 6: 'June',
7: 'July', 8: 'August', 9: 'September',
10: 'October', 11: 'November', 12: 'December'}
# 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]
# Full and abbreviated names of weekdays
day_name = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
day_abbr = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', ]
day_name = ['Sunday', 'Monday', 'Tuesday', 'Wednesday',
'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):
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):
"""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)
def FillDate(val):
s = str(val)
if len(s) < 2:
@@ -46,25 +59,33 @@ def FillDate(val):
def julianDay(year, month, day):
b = 0L
year, month, day = long(year), long(month), long(day)
if month > 12L:
year = year + month/12L
month = month%12
elif month < 1L:
"""Convert a date to Julian
:param int `year`: the year
:param int `month`: the month
:param int `day`: the day
:returns: the julian date number
"""
b = 0
if month > 12:
year = year + month / 12
month = month % 12
elif month < 1:
month = -month
year = year - month/12L - 1L
month = 12L - month%12L
if year > 0L:
yearCorr = 0L
year = year - month / 12 - 1
month = 12 - month % 12
if year > 0:
yearCorr = 0
else:
yearCorr = 3L
if month < 3L:
year = year - 1L
month = month + 12L
if year*10000L + month*100L + day > 15821014L:
b = 2L - year/100L + year/400L
return (1461L*year - yearCorr)/4L + 306001L*(month + 1L)/10000L + day + 1720994L + b
yearCorr = 3
if month < 3:
year = year - 1
month = month + 12
if year * 10000 + month * 100 + day > 15821014:
b = 2 - year / 100 + year / 400
return (1461 * year - yearCorr) / 4 + 306001 * (month + 1) / 10000 + day + 1720994 + b
def TodayDay():
@@ -77,53 +98,99 @@ def TodayDay():
daywk = day_name[daywk]
return(daywk)
def FormatDay(value):
date = FromFormat(value)
daywk = DateCalc.dayOfWeek(date)
daywk = day_name[daywk]
return(daywk)
def FromJulian(julian):
julian = long(julian)
if (julian < 2299160L):
b = julian + 1525L
"""Convert a julian date
:param int `julian`: the julian date to convert
:returns: year, month day as integers
"""
if (julian < 2299160):
b = julian + 1525
else:
alpha = (4L*julian - 7468861L)/146097L
b = julian + 1526L + alpha - alpha/4L
c = (20L*b - 2442L)/7305L
d = 1461L*c/4L
e = 10000L*(b - d)/306001L
day = int(b - d - 306001L*e/10000L)
if e < 14L:
month = int(e - 1L)
alpha = (4 * julian - 7468861) / 146097
b = julian + 1526 + alpha - alpha / 4
c = (20 * b - 2442) / 7305
d = 1461 * c / 4
e = 10000 * (b - d) / 306001
day = int(b - d - 306001 * e / 10000)
if e < 14:
month = int(e - 1)
else:
month = int(e - 13L)
month = int(e - 13)
if month > 2:
year = c - 4716L
year = c - 4716
else:
year = c - 4715L
year = c - 4715
year = int(year)
return year, month, day
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):
"""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))
return ndays
class now(object):
"""A now date class"""
def __init__(self):
"""
Default class constructor.
"""
self.date = time.localtime(time.time())
self.year = self.date[0]
self.month = self.date[1]
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):
"""A date class"""
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.month = month
self.month = int(month)
self.year = int(year)
self.day = int(day)
self.day_of_week = dayOfWeek(self.julian)
self.days_in_month = daysPerMonth(self.month, self.year)