Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1 | """Parse a timezone specification.""" |
| 2 | |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 3 | # XXX Unfinished. |
| 4 | # XXX Only the typical form "XXXhhYYY;ddd/hh,ddd/hh" is currently supported. |
| 5 | |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 6 | tzpat = ('^([A-Z][A-Z][A-Z])([-+]?[0-9]+)([A-Z][A-Z][A-Z]);' |
| 7 | '([0-9]+)/([0-9]+),([0-9]+)/([0-9]+)$') |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 8 | |
| 9 | tzprog = None |
| 10 | |
| 11 | def tzparse(tzstr): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 12 | """Given a timezone spec, return a tuple of information |
| 13 | (tzname, delta, dstname, daystart, hourstart, dayend, hourend), |
| 14 | where 'tzname' is the name of the timezone, 'delta' is the offset |
| 15 | in hours from GMT, 'dstname' is the name of the daylight-saving |
| 16 | timezone, and 'daystart'/'hourstart' and 'dayend'/'hourend' |
| 17 | specify the starting and ending points for daylight saving time.""" |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 18 | global tzprog |
| 19 | if tzprog == None: |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 20 | import re |
| 21 | tzprog = re.compile(tzpat) |
| 22 | match = tzprog.match(tzstr) |
| 23 | if not match: |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 24 | raise ValueError, 'not the TZ syntax I understand' |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 25 | subs = [] |
| 26 | for i in range(1, 8): |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 27 | subs.append(match.group(i)) |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 28 | for i in (1, 3, 4, 5, 6): |
| 29 | subs[i] = eval(subs[i]) |
| 30 | [tzname, delta, dstname, daystart, hourstart, dayend, hourend] = subs |
| 31 | return (tzname, delta, dstname, daystart, hourstart, dayend, hourend) |
| 32 | |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 33 | def tzlocaltime(secs, params): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 34 | """Given a Unix time in seconds and a tuple of information about |
| 35 | a timezone as returned by tzparse(), return the local time in the |
| 36 | form (year, month, day, hour, min, sec, yday, wday, tzname).""" |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 37 | import time |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 38 | (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = params |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 39 | year, month, days, hours, mins, secs, yday, wday, isdst = \ |
| 40 | time.gmtime(secs - delta*3600) |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 41 | if (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend): |
| 42 | tzname = dstname |
| 43 | hours = hours + 1 |
| 44 | return year, month, days, hours, mins, secs, yday, wday, tzname |
| 45 | |
| 46 | def tzset(): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 47 | """Determine the current timezone from the "TZ" environment variable.""" |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 48 | global tzparams, timezone, altzone, daylight, tzname |
| 49 | import os |
| 50 | tzstr = os.environ['TZ'] |
| 51 | tzparams = tzparse(tzstr) |
| 52 | timezone = tzparams[1] * 3600 |
Guido van Rossum | e7113b6 | 1993-03-29 11:30:50 +0000 | [diff] [blame] | 53 | altzone = timezone - 3600 |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 54 | daylight = 1 |
| 55 | tzname = tzparams[0], tzparams[2] |
| 56 | |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 57 | def isdst(secs): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 58 | """Return true if daylight-saving time is in effect for the given |
| 59 | Unix time in the current timezone.""" |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 60 | import time |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 61 | (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = \ |
| 62 | tzparams |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 63 | year, month, days, hours, mins, secs, yday, wday, isdst = \ |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 64 | time.gmtime(secs - delta*3600) |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 65 | return (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend) |
| 66 | |
| 67 | tzset() |
| 68 | |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 69 | def localtime(secs): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 70 | """Get the local time in the current timezone.""" |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 71 | return tzlocaltime(secs, tzparams) |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 72 | |
| 73 | def test(): |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 74 | from time import asctime, gmtime |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 75 | import time, sys |
| 76 | now = time.time() |
| 77 | x = localtime(now) |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 78 | tm = x[:-1] + (0,) |
| 79 | print 'now =', now, '=', asctime(tm), x[-1] |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 80 | now = now - now % (24*3600) |
| 81 | if sys.argv[1:]: now = now + eval(sys.argv[1]) |
| 82 | x = gmtime(now) |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 83 | tm = x[:-1] + (0,) |
| 84 | print 'gmtime =', now, '=', asctime(tm), 'yday =', x[-2] |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 85 | jan1 = now - x[-2]*24*3600 |
| 86 | x = localtime(jan1) |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 87 | tm = x[:-1] + (0,) |
| 88 | print 'jan1 =', jan1, '=', asctime(tm), x[-1] |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 89 | for d in range(85, 95) + range(265, 275): |
| 90 | t = jan1 + d*24*3600 |
| 91 | x = localtime(t) |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 92 | tm = x[:-1] + (0,) |
| 93 | print 'd =', d, 't =', t, '=', asctime(tm), x[-1] |