"""Parse a timezone specification."""
# XXX Only the typical form "XXXhhYYY;ddd/hh,ddd/hh" is currently supported.
"The tzparse module is obsolete and will disappear in the future",
tzpat
= ('^([A-Z][A-Z][A-Z])([-+]?[0-9]+)([A-Z][A-Z][A-Z]);'
'([0-9]+)/([0-9]+),([0-9]+)/([0-9]+)$')
"""Given a timezone spec, return a tuple of information
(tzname, delta, dstname, daystart, hourstart, dayend, hourend),
where 'tzname' is the name of the timezone, 'delta' is the offset
in hours from GMT, 'dstname' is the name of the daylight-saving
timezone, and 'daystart'/'hourstart' and 'dayend'/'hourend'
specify the starting and ending points for daylight saving time."""
tzprog
= re
.compile(tzpat
)
match
= tzprog
.match(tzstr
)
raise ValueError, 'not the TZ syntax I understand'
subs
.append(match
.group(i
))
for i
in (1, 3, 4, 5, 6):
[tzname
, delta
, dstname
, daystart
, hourstart
, dayend
, hourend
] = subs
return (tzname
, delta
, dstname
, daystart
, hourstart
, dayend
, hourend
)
def tzlocaltime(secs
, params
):
"""Given a Unix time in seconds and a tuple of information about
a timezone as returned by tzparse(), return the local time in the
form (year, month, day, hour, min, sec, yday, wday, tzname)."""
(tzname
, delta
, dstname
, daystart
, hourstart
, dayend
, hourend
) = params
year
, month
, days
, hours
, mins
, secs
, yday
, wday
, isdst
= \
time
.gmtime(secs
- delta
*3600)
if (daystart
, hourstart
) <= (yday
+1, hours
) < (dayend
, hourend
):
return year
, month
, days
, hours
, mins
, secs
, yday
, wday
, tzname
"""Determine the current timezone from the "TZ" environment variable."""
global tzparams
, timezone
, altzone
, daylight
, tzname
tzparams
= tzparse(tzstr
)
timezone
= tzparams
[1] * 3600
altzone
= timezone
- 3600
tzname
= tzparams
[0], tzparams
[2]
"""Return true if daylight-saving time is in effect for the given
Unix time in the current timezone."""
(tzname
, delta
, dstname
, daystart
, hourstart
, dayend
, hourend
) = \
year
, month
, days
, hours
, mins
, secs
, yday
, wday
, isdst
= \
time
.gmtime(secs
- delta
*3600)
return (daystart
, hourstart
) <= (yday
+1, hours
) < (dayend
, hourend
)
"""Get the local time in the current timezone."""
return tzlocaltime(secs
, tzparams
)
from time
import asctime
, gmtime
print 'now =', now
, '=', asctime(tm
), x
[-1]
now
= now
- now
% (24*3600)
if sys
.argv
[1:]: now
= now
+ eval(sys
.argv
[1])
print 'gmtime =', now
, '=', asctime(tm
), 'yday =', x
[-2]
jan1
= now
- x
[-2]*24*3600
print 'jan1 =', jan1
, '=', asctime(tm
), x
[-1]
for d
in range(85, 95) + range(265, 275):
print 'd =', d
, 't =', t
, '=', asctime(tm
), x
[-1]