myPIM

Check-in [db342c088c]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Initial revision
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | master | trunk
Files: files | file ages | folders
SHA3-256:db342c088cea4bcd29e2f308a3ff02eb5314caaa8bfd82895bc430c8d537415c
User & Date: ajv-899-334-8894@vsta.org 2009-09-26 03:45:49
Context
2009-09-26
03:47
*** empty log message *** check-in: cc72d92390 user: ajv-899-334-8894@vsta.org tags: master, trunk
03:45
Initial revision check-in: db342c088c user: ajv-899-334-8894@vsta.org tags: master, trunk
2009-08-21
00:34
Expand error display check-in: a26f95e8ef user: ajv-899-334-8894@vsta.org tags: master, trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added TODO.











>
>
>
>
>
1
2
3
4
5
Add indefinite dates 1/13/?
Change week display to be day before current through fifth day beyond.
Add a -i interactive mode, so the DB loads and then you can type in
 multiple queries without eating the CPU for a reload--doesn't matter
 on a desktop but saves seconds on a PDA.

Added xcal_en.py.

































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
XXX

This version is a start on implementing England 1752 Gregorian
 conversion.  It still needs work on the leap year part.
Andy
Mon Feb 16 19:21:12 PST 2009

#
# xcal.py
#	Manipulation for calendar values from year 0 to 100000 and
#	beyond.
#
# Based on a library in C:
#
# Copyright (c) 1997 Wolfgang Helbig
# All rights reserved.
#
# Converted to Python by Andy Valencia 11/2008
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
import time

#
# For each month tabulate the number of days elapsed in a year before the
# month. This assumes the internal date representation, where a year
# starts on March 1st. So we don't need a special table for leap years.
# But we do need a special table for the year 1582, since 10 days are
# deleted in October. This is month1s[e] for the switch from Julian to
# Gregorian calendar.
#
month1 = \
    (0, 31, 61, 92, 122, 153, 184, 214, 245, 275, 306, 337)
#    F  M   A   M   J    J    A    S    O    N    D    J
month1s = \
    (0, 31, 61, 92, 122, 153, 184, 214, 235, 265, 296, 327)
month1se = \
    (0, 31, 61, 92, 122, 153, 184, 203, 234, 264, 295, 326)

# The last day of Julian calendar, in internal and ndays representation
# Dates in this format are stored in a tuple as (year, month, day)
nswitch = None

# The Catholic countries deleted ten days following October 4th, 1582
jiswitch = (1582, 7, 3, 10)
# England and such deleted eleven days following September 2nd, 1752
jiswitche = (1752, 6, 1, 11)

# Point to the appropriate switches: Gregorian or English adoption
swp = jiswitche
# swp = jiswitch
monthp = month1se
# monthp = month1s

# Names of days of week
daynames = ("Monday", "Tuesday", "Wednesday", "Thursday", \
    "Friday", "Saturday", "Sunday")

#
# Class wrapper around a (y, m, d) tuple
#
# y, m, d - Year month and day for this Date
#
class Date():
    def __init__(self, year, month, day):
	self.y = year
	self.m = month
	self.d = day
	self.wday = None
	self.nmonth = None

    # Sring rep
    def __str__(self):
	return "%s/%s/%s" % (self.m, self.d, self.y)

    # Comparisons
    def __eq__(self, other):
	if other == None:
	    return False
	return (self.y == other.y) and (self.m == other.m) \
	    and (self.d == other.d)
    def __ne__(self, other):
	if other == None:
	    return True
	return not (self == other)
    def __lt__(self, other):
	if self.y > other.y: return False
	if self.y < other.y: return True
	if self.m > other.m: return False
	if self.m < other.m: return True
	return self.d < other.d
    def __le__(self, other):
	return (self == other) or (self < other)
    def __gt__(self, other):
	return (not (self == other)) and (not (self < other))
    def __ge__(self, other):
	return (self == other) or (self > other)

    # Return the number of days since March 1st of the year zero.
    # The date is given according to Julian calendar.
    def ndaysj(self):
	    idt = self.date2idt()
	    return idt.ndaysji()

    # Same as above, where the Julian date is given in internal notation.
    # This formula shows the beauty of this notation.
    def ndaysji(self):
	return self.d + month1[self.m] + (self.y * 365) + (self.y / 4)

    # Return the number of days since March 1st of the year zero. The date is
    #  assumed Gregorian if younger than 1582-10-04 and Julian otherwise. This
    #  is the reverse of gdate.
    def ndaysg(self):
	idt = self.date2idt()
	return idt.ndaysgi()

    # Same as above, but with the Gregorian date given in internal
    #  representation.
    def ndaysgi(self):
	global nswitch

	# Cache nswitch if not already done
	if nswitch == None:
	    nswitch = Date(*(swp[0:3])).ndaysji()

	# Assume Julian calendar and adapt to Gregorian if necessary, i. e.
	# younger than nswitch. Gregori deleted
	# the ten days from Oct 5th to Oct 14th 1582.
	# Thereafter years which are multiples of 100 and not multiples
	# of 400 were not leap years anymore.
	# This makes the average length of a year
	# 365d +.25d - .01d + .0025d = 365.2425d. But the tropical
	# year measures 365.2422d. So in 10000/3 years we are
	# again one day ahead of the earth. Sigh :-)
	# (d is the average length of a day and tropical year is the
	# time from one spring point to the next.)
	#
	# "nd" is number of days--return value
	nd = self.ndaysji()
	if self.y >= 1600:
	    nd = (nd - 10 - (self.y - 1600) / 100 + (self.y - 1600) / 400)
	elif nd > nswitch:
	    nd -= 10
	return (nd);

    # Convert a date to internal date representation: The year starts on
    #  March 1st, month and day numbering start at zero. E. g. March 1st of
    #  year zero is written as y=0, m=0, d=0.
    def date2idt(self):
	d = self.d - 1
	if self.m > 2:
	    m = self.m - 3
	    y = self.y
	else:
	    m = self.m + 9
	    y = self.y - 1
	assert not ((m < 0) or (m > 11) or (y < 0))
	return Date(y, m, d)

    # Reverse of date2idt
    def idt2date(self):
	d = self.d + 1;
	if self.m < 10:
	    m = self.m + 3
	    y = self.y
	else:
	    m = self.m - 9
	    y = self.y + 1
	assert m >= 1
	return Date(y, m, d)

    # Tell our weekday
    def weekday(self):
	# Cache it on first reference
	if self.wday == None:
	    self.wday = weekday(self.ndaysg())
	return self.wday

    # Tell name of weekday
    def dayname(self):
	return daynames[self.weekday()]

    # Tell number of days to reach 1st of next month
    def days2next(self):
	# (The answer is cached)
	if self.nmonth == None:
	    y = self.y
	    m = self.m+1
	    if m > 12:
		y += 1
		m = 1
	    nd = Date(y, m, 1)
	    self.nmonth = nd.ndaysj() - self.ndaysj()
	return self.nmonth

# Compute the Julian date from the number of days elapsed since
#  March 1st of year zero.
def jdate(ndays):
    #
    # Compute the year by starting with an approximation not smaller
    #  than the answer and using linear search for the greatest
    #  year which does not begin after ndays.
    #
    # "idt" is our internal date representation,
    # "r" holds the rest of days
    #
    idt = Date(ndays / 365, 0, 0)
    while True:
	r = idt.ndaysji()
	if r <= ndays:
	    break
	idt.y -= 1
    
    # Set r to the days left in the year and compute the month by
    #  linear search as the largest month that does not begin after r
    #  days.
    r = ndays - r
    idt.m = 11
    while month1[idt.m] > r:
	idt.m -= 1

    # Compute the days left in the month
    idt.d = r - month1[idt.m]

    # Return external representation of the date
    return idt.idt2date()

# Compute the date according to the Gregorian calendar from the number of
#  days since March 1st, year zero. The date computed will be Julian if it
#  is older than 1582-10-05. This is the reverse of the function ndaysg().
def gdate(ndays):
    # Compute the year by starting with an approximation not smaller
    #  than the answer and search linearly for the greatest year not
    #  starting after ndays.
    # "idt" is our internal date representation,
    # "r" holds the rest of days
    idt = Date(ndays / 365, 0, 0)
    while True:
	r = idt.ndaysgi()
	if r <= ndays:
	    break
	idt.y -= 1

    # Set ndays to the number of days left and compute by linear
    #  search the greatest month which does not start after ndays. We
    #  use the table month1 which provides for each month the number
    #  of days that elapsed in the year before that month, and a
    #  special table for the Julian -> Gregorian conversion.
    ndays = ndays - r
    if idt.y == swp[0]: montht = monthp
    else: montht = month1

    idt.m = 11
    while montht[idt.m] > ndays:
	idt.m -= 1

    # The rest is the day in month
    idt.d = ndays - montht[idt.m]

    # Advance 10/11 days at the appropriate point in the Catholic
    #  or Protestant version of the calendar.
    if (idt.y == swp[0]) and (idt.m == swp[1]) and \
	    (swp[2] < idt.d):
	idt.d += swp[3];

    # Return external representation of found date
    return idt.idt2date()

# Compute the week number from the number of days since March 1st year 0.
# The weeks are numbered per year starting with 1. If the first
#  week of a year includes at least four days of that year it is week 1,
#  otherwise it gets the number of the last week of the previous year.
# Return value is (week #, Y)
#  Where Y is the year that contains the greater part of the week
def week(nd):
    dt = gdate(nd)
    y = dt.y + 1
    while True:
	fw = firstweek(y)
	if nd >= fw:
	    break
	y -= 1
    return ((nd - fw) / 7 + 1, y)

# Return the first day of week 1 of year y
def firstweek(y):
    # Internal representation of y-1-1
    idt = Date(y - 1, 10, 0)

    # If more than 3 days of this week are in the preceding year, the
    #  next week is week 1 (and the next monday is the answer),
    #  otherwise this week is week 1 and the last monday is the
    #  answer.
    nd = idt.ndaysgi()
    wd = weekday(nd)
    if wd > 3:
	return nd - wd + 7
    return nd - wd

# Cache daynumber of one Monday
nmonday = None

# Return the weekday (Mo = 0 .. Su = 6)
def weekday(nd):
    global nmonday

    # Cache the daynumber of one monday
    if nmonday == None:
	# Internal repr. of 1997-11-17
	dmondaygi = Date(1997, 8, 16)
	nmonday = dmondaygi.ndaysgi()

    # Return (nd - nmonday) modulo 7 which is the weekday
    nd = (nd - nmonday) % 7
    if nd < 0:
	return nd + 7
    return nd

# Return a Date() for today
def today():
    return Date(*time.localtime()[0:3])

# Parse M/D/Y into a Date
def parse(str):
    parts = str.split("/")
    assert len(parts) == 3
    m = int(parts[0])
    assert (m >= 1) and (m <= 12)
    d = int(parts[1])
    assert (d >= 0) and (d <= 31)
    y = int(parts[2])
    assert (y >= 1) and (y <= 100000)
    return Date(y, m, d)