Class around PixMap objects that allows more python-like access. By Joe Strout.
[python/dscho.git] / Lib / tzparse.py
blob358e0cc25e276faa9cef1b5c86dcff427db356dd
1 # Parse a timezone specification.
2 # XXX Unfinished.
3 # XXX Only the typical form "XXXhhYYY;ddd/hh,ddd/hh" is currently supported.
5 tzpat = ('^([A-Z][A-Z][A-Z])([-+]?[0-9]+)([A-Z][A-Z][A-Z]);'
6 '([0-9]+)/([0-9]+),([0-9]+)/([0-9]+)$')
8 tzprog = None
10 def tzparse(tzstr):
11 global tzprog
12 if tzprog == None:
13 import re
14 tzprog = re.compile(tzpat)
15 match = tzprog.match(tzstr)
16 if not match:
17 raise ValueError, 'not the TZ syntax I understand'
18 subs = []
19 for i in range(1, 8):
20 subs.append(match.group(i))
21 for i in (1, 3, 4, 5, 6):
22 subs[i] = eval(subs[i])
23 [tzname, delta, dstname, daystart, hourstart, dayend, hourend] = subs
24 return (tzname, delta, dstname, daystart, hourstart, dayend, hourend)
26 def tzlocaltime(secs, params):
27 import time
28 (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = params
29 year, month, days, hours, mins, secs, yday, wday, isdst = \
30 time.gmtime(secs - delta*3600)
31 if (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend):
32 tzname = dstname
33 hours = hours + 1
34 return year, month, days, hours, mins, secs, yday, wday, tzname
36 def tzset():
37 global tzparams, timezone, altzone, daylight, tzname
38 import os
39 tzstr = os.environ['TZ']
40 tzparams = tzparse(tzstr)
41 timezone = tzparams[1] * 3600
42 altzone = timezone - 3600
43 daylight = 1
44 tzname = tzparams[0], tzparams[2]
46 def isdst(secs):
47 import time
48 (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = \
49 tzparams
50 year, month, days, hours, mins, secs, yday, wday, isdst = \
51 time.gmtime(secs - delta*3600)
52 return (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend)
54 tzset()
56 def localtime(secs):
57 return tzlocaltime(secs, tzparams)
59 def test():
60 from time import asctime, gmtime
61 import time, sys
62 now = time.time()
63 x = localtime(now)
64 tm = x[:-1] + (0,)
65 print 'now =', now, '=', asctime(tm), x[-1]
66 now = now - now % (24*3600)
67 if sys.argv[1:]: now = now + eval(sys.argv[1])
68 x = gmtime(now)
69 tm = x[:-1] + (0,)
70 print 'gmtime =', now, '=', asctime(tm), 'yday =', x[-2]
71 jan1 = now - x[-2]*24*3600
72 x = localtime(jan1)
73 tm = x[:-1] + (0,)
74 print 'jan1 =', jan1, '=', asctime(tm), x[-1]
75 for d in range(85, 95) + range(265, 275):
76 t = jan1 + d*24*3600
77 x = localtime(t)
78 tm = x[:-1] + (0,)
79 print 'd =', d, 't =', t, '=', asctime(tm), x[-1]