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