Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 1 | # Parse a timezone specification. |
| 2 | # XXX Unfinished. |
| 3 | # XXX Only the typical form "XXXhhYYY;ddd/hh,ddd/hh" is currently supported. |
| 4 | |
| 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]+\)$' |
| 7 | |
| 8 | tzprog = None |
| 9 | |
| 10 | def tzparse(tzstr): |
| 11 | global tzprog |
| 12 | if tzprog == None: |
| 13 | import regex |
| 14 | tzprog = regex.compile(tzpat) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 15 | if tzprog.match(tzstr) < 0: |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 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) |
| 26 | |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 27 | def tzlocaltime(secs, params): |
| 28 | import time |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 29 | (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = params |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 30 | year, month, days, hours, mins, secs, yday, wday, isdst = \ |
| 31 | time.gmtime(secs - delta*3600) |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 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 |
| 36 | |
| 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 |
Guido van Rossum | e7113b6 | 1993-03-29 11:30:50 +0000 | [diff] [blame] | 43 | altzone = timezone - 3600 |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 44 | daylight = 1 |
| 45 | tzname = tzparams[0], tzparams[2] |
| 46 | |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 47 | def isdst(secs): |
| 48 | import time |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 49 | (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = \ |
| 50 | tzparams |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 51 | year, month, days, hours, mins, secs, yday, wday, isdst = \ |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 52 | time.gmtime(secs - delta*3600) |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 53 | return (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend) |
| 54 | |
| 55 | tzset() |
| 56 | |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 57 | def localtime(secs): |
| 58 | return tzlocaltime(secs, tzparams) |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 59 | |
| 60 | def test(): |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 61 | from time import asctime, gmtime |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 62 | import time, sys |
| 63 | now = time.time() |
| 64 | x = localtime(now) |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 65 | tm = x[:-1] + (0,) |
| 66 | print 'now =', now, '=', asctime(tm), x[-1] |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 67 | now = now - now % (24*3600) |
| 68 | if sys.argv[1:]: now = now + eval(sys.argv[1]) |
| 69 | x = gmtime(now) |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 70 | tm = x[:-1] + (0,) |
| 71 | print 'gmtime =', now, '=', asctime(tm), 'yday =', x[-2] |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 72 | jan1 = now - x[-2]*24*3600 |
| 73 | x = localtime(jan1) |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 74 | tm = x[:-1] + (0,) |
| 75 | print 'jan1 =', jan1, '=', asctime(tm), x[-1] |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 76 | for d in range(85, 95) + range(265, 275): |
| 77 | t = jan1 + d*24*3600 |
| 78 | x = localtime(t) |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 79 | tm = x[:-1] + (0,) |
| 80 | print 'd =', d, 't =', t, '=', asctime(tm), x[-1] |