]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/CDate.py
d900e0d203a4b059f83a5c5a4951d380bd0addad
[wxWidgets.git] / wxPython / wx / lib / CDate.py
1 # Name: CDate.py
2 # Purpose: Date and Calendar classes
3 #
4 # Author: Lorne White (email: lwhite1@planet.eon.net)
5 #
6 # Created:
7 # Version 0.2 1999/11/08
8 # Licence: wxWindows license
9 #----------------------------------------------------------------------------
10
11 import time
12
13 Month = {2: 'February', 3: 'March', None: 0, 'July': 7, 11:
14 'November', 'December': 12, 'June': 6, 'January': 1, 'September': 9,
15 'August': 8, 'March': 3, 'November': 11, 'April': 4, 12: 'December',
16 'May': 5, 10: 'October', 9: 'September', 8: 'August', 7: 'July', 6:
17 'June', 5: 'May', 4: 'April', 'October': 10, 'February': 2, 1:
18 'January', 0: None}
19
20 # Number of days per month (except for February in leap years)
21 mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
22
23 # Full and abbreviated names of weekdays
24 day_name = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
25 day_abbr = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', ]
26
27 # Return number of leap years in range [y1, y2)
28 # Assume y1 <= y2 and no funny (non-leap century) years
29
30 def leapdays(y1, y2):
31 return (y2+3)/4 - (y1+3)/4
32
33 # Return 1 for leap years, 0 for non-leap years
34 def isleap(year):
35 return year % 4 == 0 and (year % 100 <> 0 or year % 400 == 0)
36
37 def FillDate(val):
38 s = str(val)
39 if len(s) < 2:
40 s = '0' + s
41 return s
42
43
44 def julianDay(year, month, day):
45 b = 0L
46 year, month, day = long(year), long(month), long(day)
47 if month > 12L:
48 year = year + month/12L
49 month = month%12
50 elif month < 1L:
51 month = -month
52 year = year - month/12L - 1L
53 month = 12L - month%12L
54 if year > 0L:
55 yearCorr = 0L
56 else:
57 yearCorr = 3L
58 if month < 3L:
59 year = year - 1L
60 month = month + 12L
61 if year*10000L + month*100L + day > 15821014L:
62 b = 2L - year/100L + year/400L
63 return (1461L*year - yearCorr)/4L + 306001L*(month + 1L)/10000L + day + 1720994L + b
64
65
66 def TodayDay():
67 date = time.localtime(time.time())
68 year = date[0]
69 month = date[1]
70 day = date[2]
71 julian = julianDay(year, month, day)
72 daywk = dayOfWeek(julian)
73 daywk = day_name[daywk]
74 return(daywk)
75
76 def FormatDay(value):
77 date = FromFormat(value)
78 daywk = DateCalc.dayOfWeek(date)
79 daywk = day_name[daywk]
80 return(daywk)
81
82 def FromJulian(julian):
83 julian = long(julian)
84 if (julian < 2299160L):
85 b = julian + 1525L
86 else:
87 alpha = (4L*julian - 7468861L)/146097L
88 b = julian + 1526L + alpha - alpha/4L
89 c = (20L*b - 2442L)/7305L
90 d = 1461L*c/4L
91 e = 10000L*(b - d)/306001L
92 day = int(b - d - 306001L*e/10000L)
93 if e < 14L:
94 month = int(e - 1L)
95 else:
96 month = int(e - 13L)
97 if month > 2:
98 year = c - 4716L
99 else:
100 year = c - 4715L
101 year = int(year)
102 return year, month, day
103
104 def dayOfWeek(julian):
105 return int((julian + 1L)%7L)
106
107 def daysPerMonth(month, year):
108 ndays = mdays[month] + (month == 2 and isleap(year))
109 return ndays
110
111 class now:
112 def __init__(self):
113 self.date = time.localtime(time.time())
114 self.year = self.date[0]
115 self.month = self.date[1]
116 self.day = self.date[2]
117
118 class Date:
119 def __init__(self, year, month, day):
120 self.julian = julianDay(year, month, day)
121 self.month = month
122 self.year = year
123 self.day_of_week = dayOfWeek(self.julian)
124 self.days_in_month = daysPerMonth(self.month, self.year)
125