PyXR

c:\python24\lib \ tzparse.py



0001 """Parse a timezone specification."""
0002 
0003 # XXX Unfinished.
0004 # XXX Only the typical form "XXXhhYYY;ddd/hh,ddd/hh" is currently supported.
0005 
0006 import warnings
0007 warnings.warn(
0008     "The tzparse module is obsolete and will disappear in the future",
0009     DeprecationWarning)
0010 
0011 tzpat = ('^([A-Z][A-Z][A-Z])([-+]?[0-9]+)([A-Z][A-Z][A-Z]);'
0012           '([0-9]+)/([0-9]+),([0-9]+)/([0-9]+)$')
0013 
0014 tzprog = None
0015 
0016 def tzparse(tzstr):
0017     """Given a timezone spec, return a tuple of information
0018     (tzname, delta, dstname, daystart, hourstart, dayend, hourend),
0019     where 'tzname' is the name of the timezone, 'delta' is the offset
0020     in hours from GMT, 'dstname' is the name of the daylight-saving
0021     timezone, and 'daystart'/'hourstart' and 'dayend'/'hourend'
0022     specify the starting and ending points for daylight saving time."""
0023     global tzprog
0024     if tzprog is None:
0025         import re
0026         tzprog = re.compile(tzpat)
0027     match = tzprog.match(tzstr)
0028     if not match:
0029         raise ValueError, 'not the TZ syntax I understand'
0030     subs = []
0031     for i in range(1, 8):
0032         subs.append(match.group(i))
0033     for i in (1, 3, 4, 5, 6):
0034         subs[i] = eval(subs[i])
0035     [tzname, delta, dstname, daystart, hourstart, dayend, hourend] = subs
0036     return (tzname, delta, dstname, daystart, hourstart, dayend, hourend)
0037 
0038 def tzlocaltime(secs, params):
0039     """Given a Unix time in seconds and a tuple of information about
0040     a timezone as returned by tzparse(), return the local time in the
0041     form (year, month, day, hour, min, sec, yday, wday, tzname)."""
0042     import time
0043     (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = params
0044     year, month, days, hours, mins, secs, yday, wday, isdst = \
0045             time.gmtime(secs - delta*3600)
0046     if (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend):
0047         tzname = dstname
0048         hours = hours + 1
0049     return year, month, days, hours, mins, secs, yday, wday, tzname
0050 
0051 def tzset():
0052     """Determine the current timezone from the "TZ" environment variable."""
0053     global tzparams, timezone, altzone, daylight, tzname
0054     import os
0055     tzstr = os.environ['TZ']
0056     tzparams = tzparse(tzstr)
0057     timezone = tzparams[1] * 3600
0058     altzone = timezone - 3600
0059     daylight = 1
0060     tzname = tzparams[0], tzparams[2]
0061 
0062 def isdst(secs):
0063     """Return true if daylight-saving time is in effect for the given
0064     Unix time in the current timezone."""
0065     import time
0066     (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = \
0067             tzparams
0068     year, month, days, hours, mins, secs, yday, wday, isdst = \
0069             time.gmtime(secs - delta*3600)
0070     return (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend)
0071 
0072 tzset()
0073 
0074 def localtime(secs):
0075     """Get the local time in the current timezone."""
0076     return tzlocaltime(secs, tzparams)
0077 
0078 def test():
0079     from time import asctime, gmtime
0080     import time, sys
0081     now = time.time()
0082     x = localtime(now)
0083     tm = x[:-1] + (0,)
0084     print 'now =', now, '=', asctime(tm), x[-1]
0085     now = now - now % (24*3600)
0086     if sys.argv[1:]: now = now + eval(sys.argv[1])
0087     x = gmtime(now)
0088     tm = x[:-1] + (0,)
0089     print 'gmtime =', now, '=', asctime(tm), 'yday =', x[-2]
0090     jan1 = now - x[-2]*24*3600
0091     x = localtime(jan1)
0092     tm = x[:-1] + (0,)
0093     print 'jan1 =', jan1, '=', asctime(tm), x[-1]
0094     for d in range(85, 95) + range(265, 275):
0095         t = jan1 + d*24*3600
0096         x = localtime(t)
0097         tm = x[:-1] + (0,)
0098         print 'd =', d, 't =', t, '=', asctime(tm), x[-1]
0099 

Generated by PyXR 0.9.4
SourceForge.net Logo