You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tzparse.py 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. """Parse a timezone specification."""
  2. # XXX Unfinished.
  3. # XXX Only the typical form "XXXhhYYY;ddd/hh,ddd/hh" is currently supported.
  4. import warnings
  5. warnings.warn(
  6. "The tzparse module is obsolete and will disappear in the future",
  7. DeprecationWarning)
  8. tzpat = ('^([A-Z][A-Z][A-Z])([-+]?[0-9]+)([A-Z][A-Z][A-Z]);'
  9. '([0-9]+)/([0-9]+),([0-9]+)/([0-9]+)$')
  10. tzprog = None
  11. def tzparse(tzstr):
  12. """Given a timezone spec, return a tuple of information
  13. (tzname, delta, dstname, daystart, hourstart, dayend, hourend),
  14. where 'tzname' is the name of the timezone, 'delta' is the offset
  15. in hours from GMT, 'dstname' is the name of the daylight-saving
  16. timezone, and 'daystart'/'hourstart' and 'dayend'/'hourend'
  17. specify the starting and ending points for daylight saving time."""
  18. global tzprog
  19. if tzprog is None:
  20. import re
  21. tzprog = re.compile(tzpat)
  22. match = tzprog.match(tzstr)
  23. if not match:
  24. raise ValueError, 'not the TZ syntax I understand'
  25. subs = []
  26. for i in range(1, 8):
  27. subs.append(match.group(i))
  28. for i in (1, 3, 4, 5, 6):
  29. subs[i] = eval(subs[i])
  30. [tzname, delta, dstname, daystart, hourstart, dayend, hourend] = subs
  31. return (tzname, delta, dstname, daystart, hourstart, dayend, hourend)
  32. def tzlocaltime(secs, params):
  33. """Given a Unix time in seconds and a tuple of information about
  34. a timezone as returned by tzparse(), return the local time in the
  35. form (year, month, day, hour, min, sec, yday, wday, tzname)."""
  36. import time
  37. (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = params
  38. year, month, days, hours, mins, secs, yday, wday, isdst = \
  39. time.gmtime(secs - delta*3600)
  40. if (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend):
  41. tzname = dstname
  42. hours = hours + 1
  43. return year, month, days, hours, mins, secs, yday, wday, tzname
  44. def tzset():
  45. """Determine the current timezone from the "TZ" environment variable."""
  46. global tzparams, timezone, altzone, daylight, tzname
  47. import os
  48. tzstr = os.environ['TZ']
  49. tzparams = tzparse(tzstr)
  50. timezone = tzparams[1] * 3600
  51. altzone = timezone - 3600
  52. daylight = 1
  53. tzname = tzparams[0], tzparams[2]
  54. def isdst(secs):
  55. """Return true if daylight-saving time is in effect for the given
  56. Unix time in the current timezone."""
  57. import time
  58. (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = \
  59. tzparams
  60. year, month, days, hours, mins, secs, yday, wday, isdst = \
  61. time.gmtime(secs - delta*3600)
  62. return (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend)
  63. tzset()
  64. def localtime(secs):
  65. """Get the local time in the current timezone."""
  66. return tzlocaltime(secs, tzparams)
  67. def test():
  68. from time import asctime, gmtime
  69. import time, sys
  70. now = time.time()
  71. x = localtime(now)
  72. tm = x[:-1] + (0,)
  73. print 'now =', now, '=', asctime(tm), x[-1]
  74. now = now - now % (24*3600)
  75. if sys.argv[1:]: now = now + eval(sys.argv[1])
  76. x = gmtime(now)
  77. tm = x[:-1] + (0,)
  78. print 'gmtime =', now, '=', asctime(tm), 'yday =', x[-2]
  79. jan1 = now - x[-2]*24*3600
  80. x = localtime(jan1)
  81. tm = x[:-1] + (0,)
  82. print 'jan1 =', jan1, '=', asctime(tm), x[-1]
  83. for d in range(85, 95) + range(265, 275):
  84. t = jan1 + d*24*3600
  85. x = localtime(t)
  86. tm = x[:-1] + (0,)
  87. print 'd =', d, 't =', t, '=', asctime(tm), x[-1]