Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
tzparse.py
Go to the documentation of this file.
1 """Parse a timezone specification."""
2 
3 # XXX Unfinished.
4 # XXX Only the typical form "XXXhhYYY;ddd/hh,ddd/hh" is currently supported.
5 
6 import warnings
8  "The tzparse module is obsolete and will disappear in the future",
9  DeprecationWarning)
10 
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]+)$')
13 
14 tzprog = None
15 
16 def tzparse(tzstr):
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."""
23  global tzprog
24  if tzprog is None:
25  import re
26  tzprog = re.compile(tzpat)
27  match = tzprog.match(tzstr)
28  if not match:
29  raise ValueError, 'not the TZ syntax I understand'
30  subs = []
31  for i in range(1, 8):
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)
37 
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)."""
42  import time
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):
47  tzname = dstname
48  hours = hours + 1
49  return year, month, days, hours, mins, secs, yday, wday, tzname
50 
51 def tzset():
52  """Determine the current timezone from the "TZ" environment variable."""
53  global tzparams, timezone, altzone, daylight, tzname
54  import os
55  tzstr = os.environ['TZ']
56  tzparams = tzparse(tzstr)
57  timezone = tzparams[1] * 3600
58  altzone = timezone - 3600
59  daylight = 1
60  tzname = tzparams[0], tzparams[2]
61 
62 def isdst(secs):
63  """Return true if daylight-saving time is in effect for the given
64  Unix time in the current timezone."""
65  import time
66  (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = \
67  tzparams
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)
71 
72 tzset()
73 
74 def localtime(secs):
75  """Get the local time in the current timezone."""
76  return tzlocaltime(secs, tzparams)
77 
78 def test():
79  from time import asctime, gmtime
80  import time, sys
81  now = time.time()
82  x = localtime(now)
83  tm = x[:-1] + (0,)
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])
87  x = gmtime(now)
88  tm = x[:-1] + (0,)
89  print 'gmtime =', now, '=', asctime(tm), 'yday =', x[-2]
90  jan1 = now - x[-2]*24*3600
91  x = localtime(jan1)
92  tm = x[:-1] + (0,)
93  print 'jan1 =', jan1, '=', asctime(tm), x[-1]
94  for d in range(85, 95) + range(265, 275):
95  t = jan1 + d*24*3600
96  x = localtime(t)
97  tm = x[:-1] + (0,)
98  print 'd =', d, 't =', t, '=', asctime(tm), x[-1]