tzinfo.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. """
  2. This module provides some datetime.tzinfo implementations.
  3. All those classes are taken from the Python documentation.
  4. """
  5. import time
  6. from datetime import timedelta, tzinfo
  7. ZERO = timedelta(0)
  8. # constant for zero time offset.
  9. class Utc(tzinfo):
  10. """UTC
  11. Universal time coordinated time zone.
  12. """
  13. def utcoffset(self, dt):
  14. """
  15. Return offset from UTC in minutes east of UTC, which is ZERO for UTC.
  16. """
  17. return ZERO
  18. def tzname(self, dt):
  19. """
  20. Return the time zone name corresponding to the datetime object dt,
  21. as a string.
  22. """
  23. return "UTC"
  24. def dst(self, dt):
  25. """
  26. Return the daylight saving time (DST) adjustment, in minutes east
  27. of UTC.
  28. """
  29. return ZERO
  30. def __reduce__(self):
  31. """
  32. When unpickling a Utc object, return the default instance below, UTC.
  33. """
  34. return _Utc, ()
  35. UTC = Utc()
  36. # the default instance for UTC.
  37. def _Utc():
  38. """
  39. Helper function for unpickling a Utc object.
  40. """
  41. return UTC
  42. class FixedOffset(tzinfo):
  43. """
  44. A class building tzinfo objects for fixed-offset time zones.
  45. Note that FixedOffset(0, 0, "UTC") or FixedOffset() is a different way to
  46. build a UTC tzinfo object.
  47. """
  48. def __init__(self, offset_hours=0, offset_minutes=0, name="UTC"):
  49. """
  50. Initialise an instance with time offset and name.
  51. The time offset should be positive for time zones east of UTC
  52. and negate for time zones west of UTC.
  53. """
  54. self.__offset = timedelta(hours=offset_hours, minutes=offset_minutes)
  55. self.__name = name
  56. def utcoffset(self, dt):
  57. """
  58. Return offset from UTC in minutes of UTC.
  59. """
  60. return self.__offset
  61. def tzname(self, dt):
  62. """
  63. Return the time zone name corresponding to the datetime object dt, as a
  64. string.
  65. """
  66. return self.__name
  67. def dst(self, dt):
  68. """
  69. Return the daylight saving time (DST) adjustment, in minutes east of
  70. UTC.
  71. """
  72. return ZERO
  73. def __repr__(self):
  74. """
  75. Return nicely formatted repr string.
  76. """
  77. return "<FixedOffset %r>" % self.__name
  78. STDOFFSET = timedelta(seconds=-time.timezone)
  79. # locale time zone offset
  80. # calculate local daylight saving offset if any.
  81. if time.daylight:
  82. DSTOFFSET = timedelta(seconds=-time.altzone)
  83. else:
  84. DSTOFFSET = STDOFFSET
  85. DSTDIFF = DSTOFFSET - STDOFFSET
  86. # difference between local time zone and local DST time zone
  87. class LocalTimezone(tzinfo):
  88. """
  89. A class capturing the platform's idea of local time.
  90. """
  91. def utcoffset(self, dt):
  92. """
  93. Return offset from UTC in minutes of UTC.
  94. """
  95. if self._isdst(dt):
  96. return DSTOFFSET
  97. else:
  98. return STDOFFSET
  99. def dst(self, dt):
  100. """
  101. Return daylight saving offset.
  102. """
  103. if self._isdst(dt):
  104. return DSTDIFF
  105. else:
  106. return ZERO
  107. def tzname(self, dt):
  108. """
  109. Return the time zone name corresponding to the datetime object dt, as a
  110. string.
  111. """
  112. return time.tzname[self._isdst(dt)]
  113. def _isdst(self, dt):
  114. """
  115. Returns true if DST is active for given datetime object dt.
  116. """
  117. tt = (
  118. dt.year,
  119. dt.month,
  120. dt.day,
  121. dt.hour,
  122. dt.minute,
  123. dt.second,
  124. dt.weekday(),
  125. 0,
  126. -1,
  127. )
  128. stamp = time.mktime(tt)
  129. tt = time.localtime(stamp)
  130. return tt.tm_isdst > 0
  131. # the default instance for local time zone.
  132. LOCAL = LocalTimezone()