blob: ef325e9ff097e8f7af4ff2fd3ba3f9e7850c34c3 [file] [log] [blame]
Guido van Rossum2db91351992-10-18 17:09:59 +00001# Parse a timezone specification.
2# XXX Unfinished.
3# XXX Only the typical form "XXXhhYYY;ddd/hh,ddd/hh" is currently supported.
4
5tzpat = '^\([A-Z][A-Z][A-Z]\)\([-+]?[0-9]+\)\([A-Z][A-Z][A-Z]\);' + \
6 '\([0-9]+\)/\([0-9]+\),\([0-9]+\)/\([0-9]+\)$'
7
8tzprog = None
9
10def tzparse(tzstr):
11 global tzprog
12 if tzprog == None:
13 import regex
14 tzprog = regex.compile(tzpat)
Guido van Rossumb6775db1994-08-01 11:34:53 +000015 if tzprog.match(tzstr) < 0:
Guido van Rossum2db91351992-10-18 17:09:59 +000016 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)
26
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000027def tzlocaltime(secs, params):
28 import time
Guido van Rossum2db91351992-10-18 17:09:59 +000029 (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = params
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000030 year, month, days, hours, mins, secs, yday, wday, isdst = \
31 time.gmtime(secs - delta*3600)
Guido van Rossum2db91351992-10-18 17:09:59 +000032 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
36
37def 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
Guido van Rossume7113b61993-03-29 11:30:50 +000043 altzone = timezone - 3600
Guido van Rossum2db91351992-10-18 17:09:59 +000044 daylight = 1
45 tzname = tzparams[0], tzparams[2]
46
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000047def isdst(secs):
48 import time
Guido van Rossum2db91351992-10-18 17:09:59 +000049 (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = \
50 tzparams
Guido van Rossum9b3bc711993-06-20 21:02:22 +000051 year, month, days, hours, mins, secs, yday, wday, isdst = \
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000052 time.gmtime(secs - delta*3600)
Guido van Rossum2db91351992-10-18 17:09:59 +000053 return (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend)
54
55tzset()
56
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000057def localtime(secs):
58 return tzlocaltime(secs, tzparams)
Guido van Rossum2db91351992-10-18 17:09:59 +000059
60def test():
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000061 from time import asctime, gmtime
Guido van Rossum2db91351992-10-18 17:09:59 +000062 import time, sys
63 now = time.time()
64 x = localtime(now)
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000065 tm = x[:-1] + (0,)
66 print 'now =', now, '=', asctime(tm), x[-1]
Guido van Rossum2db91351992-10-18 17:09:59 +000067 now = now - now % (24*3600)
68 if sys.argv[1:]: now = now + eval(sys.argv[1])
69 x = gmtime(now)
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000070 tm = x[:-1] + (0,)
71 print 'gmtime =', now, '=', asctime(tm), 'yday =', x[-2]
Guido van Rossum2db91351992-10-18 17:09:59 +000072 jan1 = now - x[-2]*24*3600
73 x = localtime(jan1)
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000074 tm = x[:-1] + (0,)
75 print 'jan1 =', jan1, '=', asctime(tm), x[-1]
Guido van Rossum2db91351992-10-18 17:09:59 +000076 for d in range(85, 95) + range(265, 275):
77 t = jan1 + d*24*3600
78 x = localtime(t)
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000079 tm = x[:-1] + (0,)
80 print 'd =', d, 't =', t, '=', asctime(tm), x[-1]