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