blob: fa94bd549394c8a6f2e929d9bfd8bbaaf7d7fa18 [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]);'
7 '([0-9]+)/([0-9]+),([0-9]+)/([0-9]+)$')
Guido van Rossum2db91351992-10-18 17:09:59 +00008
9tzprog = None
10
11def tzparse(tzstr):
Guido van Rossume7b146f2000-02-04 15:28:42 +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."""
Guido van Rossum2db91351992-10-18 17:09:59 +000018 global tzprog
19 if tzprog == None:
Guido van Rossum9694fca1997-10-22 21:00:49 +000020 import re
21 tzprog = re.compile(tzpat)
22 match = tzprog.match(tzstr)
23 if not match:
Guido van Rossum2db91351992-10-18 17:09:59 +000024 raise ValueError, 'not the TZ syntax I understand'
Guido van Rossum2db91351992-10-18 17:09:59 +000025 subs = []
26 for i in range(1, 8):
Guido van Rossum9694fca1997-10-22 21:00:49 +000027 subs.append(match.group(i))
Guido van Rossum2db91351992-10-18 17:09:59 +000028 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 Rossum5cfa5df1993-06-23 09:30:50 +000033def tzlocaltime(secs, params):
Guido van Rossume7b146f2000-02-04 15:28:42 +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)."""
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000037 import time
Guido van Rossum2db91351992-10-18 17:09:59 +000038 (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = params
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000039 year, month, days, hours, mins, secs, yday, wday, isdst = \
40 time.gmtime(secs - delta*3600)
Guido van Rossum2db91351992-10-18 17:09:59 +000041 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
46def tzset():
Guido van Rossume7b146f2000-02-04 15:28:42 +000047 """Determine the current timezone from the "TZ" environment variable."""
Guido van Rossum2db91351992-10-18 17:09:59 +000048 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 Rossume7113b61993-03-29 11:30:50 +000053 altzone = timezone - 3600
Guido van Rossum2db91351992-10-18 17:09:59 +000054 daylight = 1
55 tzname = tzparams[0], tzparams[2]
56
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000057def isdst(secs):
Guido van Rossume7b146f2000-02-04 15:28:42 +000058 """Return true if daylight-saving time is in effect for the given
59 Unix time in the current timezone."""
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000060 import time
Guido van Rossum2db91351992-10-18 17:09:59 +000061 (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = \
62 tzparams
Guido van Rossum9b3bc711993-06-20 21:02:22 +000063 year, month, days, hours, mins, secs, yday, wday, isdst = \
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000064 time.gmtime(secs - delta*3600)
Guido van Rossum2db91351992-10-18 17:09:59 +000065 return (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend)
66
67tzset()
68
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000069def localtime(secs):
Guido van Rossume7b146f2000-02-04 15:28:42 +000070 """Get the local time in the current timezone."""
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000071 return tzlocaltime(secs, tzparams)
Guido van Rossum2db91351992-10-18 17:09:59 +000072
73def test():
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000074 from time import asctime, gmtime
Guido van Rossum2db91351992-10-18 17:09:59 +000075 import time, sys
76 now = time.time()
77 x = localtime(now)
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000078 tm = x[:-1] + (0,)
79 print 'now =', now, '=', asctime(tm), x[-1]
Guido van Rossum2db91351992-10-18 17:09:59 +000080 now = now - now % (24*3600)
81 if sys.argv[1:]: now = now + eval(sys.argv[1])
82 x = gmtime(now)
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000083 tm = x[:-1] + (0,)
84 print 'gmtime =', now, '=', asctime(tm), 'yday =', x[-2]
Guido van Rossum2db91351992-10-18 17:09:59 +000085 jan1 = now - x[-2]*24*3600
86 x = localtime(jan1)
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000087 tm = x[:-1] + (0,)
88 print 'jan1 =', jan1, '=', asctime(tm), x[-1]
Guido van Rossum2db91351992-10-18 17:09:59 +000089 for d in range(85, 95) + range(265, 275):
90 t = jan1 + d*24*3600
91 x = localtime(t)
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000092 tm = x[:-1] + (0,)
93 print 'd =', d, 't =', t, '=', asctime(tm), x[-1]