This commit was manufactured by cvs2svn to create tag 'cnrisync'.
[python/dscho.git] / Lib / tzparse.py
blobef325e9ff097e8f7af4ff2fd3ba3f9e7850c34c3
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 regex
14 tzprog = regex.compile(tzpat)
15 if tzprog.match(tzstr) < 0:
16 raise ValueError, 'not the TZ syntax I understand'
17 regs = tzprog.regs
18 subs = []
19 for i in range(1, 8):
20 a, b = regs[i]
21 subs.append(tzstr[a:b])
22 for i in (1, 3, 4, 5, 6):
23 subs[i] = eval(subs[i])
24 [tzname, delta, dstname, daystart, hourstart, dayend, hourend] = subs
25 return (tzname, delta, dstname, daystart, hourstart, dayend, hourend)
27 def tzlocaltime(secs, params):
28 import time
29 (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = params
30 year, month, days, hours, mins, secs, yday, wday, isdst = \
31 time.gmtime(secs - delta*3600)
32 if (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend):
33 tzname = dstname
34 hours = hours + 1
35 return year, month, days, hours, mins, secs, yday, wday, tzname
37 def tzset():
38 global tzparams, timezone, altzone, daylight, tzname
39 import os
40 tzstr = os.environ['TZ']
41 tzparams = tzparse(tzstr)
42 timezone = tzparams[1] * 3600
43 altzone = timezone - 3600
44 daylight = 1
45 tzname = tzparams[0], tzparams[2]
47 def isdst(secs):
48 import time
49 (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = \
50 tzparams
51 year, month, days, hours, mins, secs, yday, wday, isdst = \
52 time.gmtime(secs - delta*3600)
53 return (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend)
55 tzset()
57 def localtime(secs):
58 return tzlocaltime(secs, tzparams)
60 def test():
61 from time import asctime, gmtime
62 import time, sys
63 now = time.time()
64 x = localtime(now)
65 tm = x[:-1] + (0,)
66 print 'now =', now, '=', asctime(tm), x[-1]
67 now = now - now % (24*3600)
68 if sys.argv[1:]: now = now + eval(sys.argv[1])
69 x = gmtime(now)
70 tm = x[:-1] + (0,)
71 print 'gmtime =', now, '=', asctime(tm), 'yday =', x[-2]
72 jan1 = now - x[-2]*24*3600
73 x = localtime(jan1)
74 tm = x[:-1] + (0,)
75 print 'jan1 =', jan1, '=', asctime(tm), x[-1]
76 for d in range(85, 95) + range(265, 275):
77 t = jan1 + d*24*3600
78 x = localtime(t)
79 tm = x[:-1] + (0,)
80 print 'd =', d, 't =', t, '=', asctime(tm), x[-1]