blob: 38e6af18c6c52ed854af6ee8dff5f5b5e33fec88 [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 Rossum9694fca1997-10-22 21:00:49 +00006tzpat = ('^([A-Z][A-Z][A-Z])([-+]?[0-9]+)([A-Z][A-Z][A-Z]);'
Tim Petersb90f89a2001-01-15 03:26:36 +00007 '([0-9]+)/([0-9]+),([0-9]+)/([0-9]+)$')
Guido van Rossum2db91351992-10-18 17:09:59 +00008
9tzprog = None
10
11def tzparse(tzstr):
Tim Petersb90f89a2001-01-15 03:26:36 +000012 """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."""
18 global tzprog
19 if tzprog is None:
20 import re
21 tzprog = re.compile(tzpat)
22 match = tzprog.match(tzstr)
23 if not match:
24 raise ValueError, 'not the TZ syntax I understand'
25 subs = []
26 for i in range(1, 8):
27 subs.append(match.group(i))
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)
Guido van Rossum2db91351992-10-18 17:09:59 +000032
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000033def tzlocaltime(secs, params):
Tim Petersb90f89a2001-01-15 03:26:36 +000034 """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)."""
37 import time
38 (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = params
39 year, month, days, hours, mins, secs, yday, wday, isdst = \
40 time.gmtime(secs - delta*3600)
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
Guido van Rossum2db91351992-10-18 17:09:59 +000045
46def tzset():
Tim Petersb90f89a2001-01-15 03:26:36 +000047 """Determine the current timezone from the "TZ" environment variable."""
48 global tzparams, timezone, altzone, daylight, tzname
49 import os
50 tzstr = os.environ['TZ']
51 tzparams = tzparse(tzstr)
52 timezone = tzparams[1] * 3600
53 altzone = timezone - 3600
54 daylight = 1
55 tzname = tzparams[0], tzparams[2]
Guido van Rossum2db91351992-10-18 17:09:59 +000056
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000057def isdst(secs):
Tim Petersb90f89a2001-01-15 03:26:36 +000058 """Return true if daylight-saving time is in effect for the given
59 Unix time in the current timezone."""
60 import time
61 (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = \
62 tzparams
63 year, month, days, hours, mins, secs, yday, wday, isdst = \
64 time.gmtime(secs - delta*3600)
65 return (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend)
Guido van Rossum2db91351992-10-18 17:09:59 +000066
67tzset()
68
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000069def localtime(secs):
Tim Petersb90f89a2001-01-15 03:26:36 +000070 """Get the local time in the current timezone."""
71 return tzlocaltime(secs, tzparams)
Guido van Rossum2db91351992-10-18 17:09:59 +000072
73def test():
Tim Petersb90f89a2001-01-15 03:26:36 +000074 from time import asctime, gmtime
75 import time, sys
76 now = time.time()
77 x = localtime(now)
78 tm = x[:-1] + (0,)
79 print 'now =', now, '=', asctime(tm), x[-1]
80 now = now - now % (24*3600)
81 if sys.argv[1:]: now = now + eval(sys.argv[1])
82 x = gmtime(now)
83 tm = x[:-1] + (0,)
84 print 'gmtime =', now, '=', asctime(tm), 'yday =', x[-2]
85 jan1 = now - x[-2]*24*3600
86 x = localtime(jan1)
87 tm = x[:-1] + (0,)
88 print 'jan1 =', jan1, '=', asctime(tm), x[-1]
89 for d in range(85, 95) + range(265, 275):
90 t = jan1 + d*24*3600
91 x = localtime(t)
92 tm = x[:-1] + (0,)
93 print 'd =', d, 't =', t, '=', asctime(tm), x[-1]