annotate Lib/tzparse.py @ 15842:f6cd36498a50 legacy-trunk

Document extensions to .pth files.
author Martin v. Löwis <martin@v.loewis.de>
date Sat, 13 Jan 2001 09:54:41 +0000
parents 785155e2cfca
children 3dd217fbdd59
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
1 """Parse a timezone specification."""
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
2
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # XXX Unfinished.
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 # XXX Only the typical form "XXXhhYYY;ddd/hh,ddd/hh" is currently supported.
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
6519
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
6 tzpat = ('^([A-Z][A-Z][A-Z])([-+]?[0-9]+)([A-Z][A-Z][A-Z]);'
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
7 '([0-9]+)/([0-9]+),([0-9]+)/([0-9]+)$')
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 tzprog = None
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 def tzparse(tzstr):
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
12 """Given a timezone spec, return a tuple of information
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
13 (tzname, delta, dstname, daystart, hourstart, dayend, hourend),
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
14 where 'tzname' is the name of the timezone, 'delta' is the offset
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
15 in hours from GMT, 'dstname' is the name of the daylight-saving
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
16 timezone, and 'daystart'/'hourstart' and 'dayend'/'hourend'
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
17 specify the starting and ending points for daylight saving time."""
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 global tzprog
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 11707
diff changeset
19 if tzprog is None:
6519
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
20 import re
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
21 tzprog = re.compile(tzpat)
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
22 match = tzprog.match(tzstr)
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
23 if not match:
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 raise ValueError, 'not the TZ syntax I understand'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 subs = []
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 for i in range(1, 8):
6519
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
27 subs.append(match.group(i))
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 for i in (1, 3, 4, 5, 6):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 subs[i] = eval(subs[i])
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 [tzname, delta, dstname, daystart, hourstart, dayend, hourend] = subs
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 return (tzname, delta, dstname, daystart, hourstart, dayend, hourend)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
1316
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1315
diff changeset
33 def tzlocaltime(secs, params):
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
34 """Given a Unix time in seconds and a tuple of information about
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
35 a timezone as returned by tzparse(), return the local time in the
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
36 form (year, month, day, hour, min, sec, yday, wday, tzname)."""
1316
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1315
diff changeset
37 import time
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = params
1316
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1315
diff changeset
39 year, month, days, hours, mins, secs, yday, wday, isdst = \
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1315
diff changeset
40 time.gmtime(secs - delta*3600)
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 if (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 tzname = dstname
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 hours = hours + 1
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 return year, month, days, hours, mins, secs, yday, wday, tzname
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
45
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 def tzset():
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
47 """Determine the current timezone from the "TZ" environment variable."""
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 global tzparams, timezone, altzone, daylight, tzname
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 import os
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 tzstr = os.environ['TZ']
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 tzparams = tzparse(tzstr)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 timezone = tzparams[1] * 3600
1260
b120cdd30fbc * Fix bug in tzparse.py for DST timezone
Guido van Rossum <guido@python.org>
parents: 1078
diff changeset
53 altzone = timezone - 3600
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 daylight = 1
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 tzname = tzparams[0], tzparams[2]
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
1316
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1315
diff changeset
57 def isdst(secs):
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
58 """Return true if daylight-saving time is in effect for the given
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
59 Unix time in the current timezone."""
1316
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1315
diff changeset
60 import time
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = \
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 tzparams
1315
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1260
diff changeset
63 year, month, days, hours, mins, secs, yday, wday, isdst = \
1316
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1315
diff changeset
64 time.gmtime(secs - delta*3600)
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 return (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 tzset()
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
1316
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1315
diff changeset
69 def localtime(secs):
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
70 """Get the local time in the current timezone."""
1316
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1315
diff changeset
71 return tzlocaltime(secs, tzparams)
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 def test():
1316
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1315
diff changeset
74 from time import asctime, gmtime
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 import time, sys
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 now = time.time()
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 x = localtime(now)
1316
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1315
diff changeset
78 tm = x[:-1] + (0,)
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1315
diff changeset
79 print 'now =', now, '=', asctime(tm), x[-1]
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 now = now - now % (24*3600)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 if sys.argv[1:]: now = now + eval(sys.argv[1])
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 x = gmtime(now)
1316
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1315
diff changeset
83 tm = x[:-1] + (0,)
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1315
diff changeset
84 print 'gmtime =', now, '=', asctime(tm), 'yday =', x[-2]
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 jan1 = now - x[-2]*24*3600
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 x = localtime(jan1)
1316
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1315
diff changeset
87 tm = x[:-1] + (0,)
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1315
diff changeset
88 print 'jan1 =', jan1, '=', asctime(tm), x[-1]
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 for d in range(85, 95) + range(265, 275):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 t = jan1 + d*24*3600
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 x = localtime(t)
1316
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1315
diff changeset
92 tm = x[:-1] + (0,)
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1315
diff changeset
93 print 'd =', d, 't =', t, '=', asctime(tm), x[-1]