1 """Parse a timezone specification."""
4 # XXX Only the typical form "XXXhhYYY;ddd/hh,ddd/hh" is currently supported.
8 "The tzparse module is obsolete and will disappear in the future",
11 tzpat
= ('^([A-Z][A-Z][A-Z])([-+]?[0-9]+)([A-Z][A-Z][A-Z]);'
12 '([0-9]+)/([0-9]+),([0-9]+)/([0-9]+)$')
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."""
26 tzprog
= re
.compile(tzpat
)
27 match
= tzprog
.match(tzstr
)
29 raise ValueError, 'not the TZ syntax I understand'
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
)
38 def tzlocaltime(secs
, params
):
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)."""
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
):
49 return year
, month
, days
, hours
, mins
, secs
, yday
, wday
, tzname
52 """Determine the current timezone from the "TZ" environment variable."""
53 global tzparams
, timezone
, altzone
, daylight
, tzname
55 tzstr
= os
.environ
['TZ']
56 tzparams
= tzparse(tzstr
)
57 timezone
= tzparams
[1] * 3600
58 altzone
= timezone
- 3600
60 tzname
= tzparams
[0], tzparams
[2]
63 """Return true if daylight-saving time is in effect for the given
64 Unix time in the current timezone."""
66 (tzname
, delta
, dstname
, daystart
, hourstart
, dayend
, hourend
) = \
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
)
75 """Get the local time in the current timezone."""
76 return tzlocaltime(secs
, tzparams
)
79 from time
import asctime
, gmtime
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])
89 print 'gmtime =', now
, '=', asctime(tm
), 'yday =', x
[-2]
90 jan1
= now
- x
[-2]*24*3600
93 print 'jan1 =', jan1
, '=', asctime(tm
), x
[-1]
94 for d
in range(85, 95) + range(265, 275):
98 print 'd =', d
, 't =', t
, '=', asctime(tm
), x
[-1]