1 # Parse a timezone specification.
3 # XXX Only the typical form "XXXhhYYY;ddd/hh,ddd/hh" is currently supported.
5 tzpat
= ('^([A-Z][A-Z][A-Z])([-+]?[0-9]+)([A-Z][A-Z][A-Z]);'
6 '([0-9]+)/([0-9]+),([0-9]+)/([0-9]+)$')
14 tzprog
= re
.compile(tzpat
)
15 match
= tzprog
.match(tzstr
)
17 raise ValueError, 'not the TZ syntax I understand'
20 subs
.append(match
.group(i
))
21 for i
in (1, 3, 4, 5, 6):
22 subs
[i
] = eval(subs
[i
])
23 [tzname
, delta
, dstname
, daystart
, hourstart
, dayend
, hourend
] = subs
24 return (tzname
, delta
, dstname
, daystart
, hourstart
, dayend
, hourend
)
26 def tzlocaltime(secs
, params
):
28 (tzname
, delta
, dstname
, daystart
, hourstart
, dayend
, hourend
) = params
29 year
, month
, days
, hours
, mins
, secs
, yday
, wday
, isdst
= \
30 time
.gmtime(secs
- delta
*3600)
31 if (daystart
, hourstart
) <= (yday
+1, hours
) < (dayend
, hourend
):
34 return year
, month
, days
, hours
, mins
, secs
, yday
, wday
, tzname
37 global tzparams
, timezone
, altzone
, daylight
, tzname
39 tzstr
= os
.environ
['TZ']
40 tzparams
= tzparse(tzstr
)
41 timezone
= tzparams
[1] * 3600
42 altzone
= timezone
- 3600
44 tzname
= tzparams
[0], tzparams
[2]
48 (tzname
, delta
, dstname
, daystart
, hourstart
, dayend
, hourend
) = \
50 year
, month
, days
, hours
, mins
, secs
, yday
, wday
, isdst
= \
51 time
.gmtime(secs
- delta
*3600)
52 return (daystart
, hourstart
) <= (yday
+1, hours
) < (dayend
, hourend
)
57 return tzlocaltime(secs
, tzparams
)
60 from time
import asctime
, gmtime
65 print 'now =', now
, '=', asctime(tm
), x
[-1]
66 now
= now
- now
% (24*3600)
67 if sys
.argv
[1:]: now
= now
+ eval(sys
.argv
[1])
70 print 'gmtime =', now
, '=', asctime(tm
), 'yday =', x
[-2]
71 jan1
= now
- x
[-2]*24*3600
74 print 'jan1 =', jan1
, '=', asctime(tm
), x
[-1]
75 for d
in range(85, 95) + range(265, 275):
79 print 'd =', d
, 't =', t
, '=', asctime(tm
), x
[-1]