blob: 12468b51d3a0d56f29be57dff1c89da9b4127727 [file] [log] [blame]
Guido van Rossume7b146f2000-02-04 15:28:42 +00001"""Parse a timezone specification."""
2
Guido van Rossum2db91351992-10-18 17:09:59 +00003# XXX Unfinished.
4# XXX Only the typical form "XXXhhYYY;ddd/hh,ddd/hh" is currently supported.
5
Guido van Rossumd0926942001-03-20 18:36:48 +00006import warnings
7warnings.warn(
8 "The tzparse module is obsolete and will disappear in the future",
9 DeprecationWarning)
10
Guido van Rossum9694fca1997-10-22 21:00:49 +000011tzpat = ('^([A-Z][A-Z][A-Z])([-+]?[0-9]+)([A-Z][A-Z][A-Z]);'
Tim Petersb90f89a2001-01-15 03:26:36 +000012 '([0-9]+)/([0-9]+),([0-9]+)/([0-9]+)$')
Guido van Rossum2db91351992-10-18 17:09:59 +000013
14tzprog = None
15
16def tzparse(tzstr):
Tim Petersb90f89a2001-01-15 03:26:36 +000017 """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 Rossum2db91351992-10-18 17:09:59 +000037
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000038def tzlocaltime(secs, params):
Tim Petersb90f89a2001-01-15 03:26:36 +000039 """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 Rossum2db91351992-10-18 17:09:59 +000050
51def tzset():
Tim Petersb90f89a2001-01-15 03:26:36 +000052 """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 Rossum2db91351992-10-18 17:09:59 +000061
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000062def isdst(secs):
Tim Petersb90f89a2001-01-15 03:26:36 +000063 """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 Rossum2db91351992-10-18 17:09:59 +000071
72tzset()
73
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000074def localtime(secs):
Tim Petersb90f89a2001-01-15 03:26:36 +000075 """Get the local time in the current timezone."""
76 return tzlocaltime(secs, tzparams)
Guido van Rossum2db91351992-10-18 17:09:59 +000077
78def test():
Tim Petersb90f89a2001-01-15 03:26:36 +000079 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]