Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.

_tzhelper.py 3.1KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # -*- test-case-name: twisted.python.test.test_tzhelper -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Time zone utilities.
  6. """
  7. from datetime import datetime as DateTime, timedelta as TimeDelta, tzinfo as TZInfo
  8. from typing import Optional
  9. __all__ = [
  10. "FixedOffsetTimeZone",
  11. "UTC",
  12. ]
  13. class FixedOffsetTimeZone(TZInfo):
  14. """
  15. Represents a fixed timezone offset (without daylight saving time).
  16. @ivar name: A L{str} giving the name of this timezone; the name just
  17. includes how much time this offset represents.
  18. @ivar offset: A L{TimeDelta} giving the amount of time this timezone is
  19. offset.
  20. """
  21. def __init__(self, offset: TimeDelta, name: Optional[str] = None) -> None:
  22. """
  23. Construct a L{FixedOffsetTimeZone} with a fixed offset.
  24. @param offset: a delta representing the offset from UTC.
  25. @param name: A name to be given for this timezone.
  26. """
  27. self.offset = offset
  28. self.name = name
  29. @classmethod
  30. def fromSignHoursMinutes(
  31. cls, sign: str, hours: int, minutes: int
  32. ) -> "FixedOffsetTimeZone":
  33. """
  34. Construct a L{FixedOffsetTimeZone} from an offset described by sign
  35. ('+' or '-'), hours, and minutes.
  36. @note: For protocol compatibility with AMP, this method never uses 'Z'
  37. @param sign: A string describing the positive or negative-ness of the
  38. offset.
  39. @param hours: The number of hours in the offset.
  40. @param minutes: The number of minutes in the offset
  41. @return: A time zone with the given offset, and a name describing the
  42. offset.
  43. """
  44. name = "%s%02i:%02i" % (sign, hours, minutes)
  45. if sign == "-":
  46. hours = -hours
  47. minutes = -minutes
  48. elif sign != "+":
  49. raise ValueError(f"Invalid sign for timezone {sign!r}")
  50. return cls(TimeDelta(hours=hours, minutes=minutes), name)
  51. @classmethod
  52. def fromLocalTimeStamp(cls, timeStamp: float) -> "FixedOffsetTimeZone":
  53. """
  54. Create a time zone with a fixed offset corresponding to a time stamp in
  55. the system's locally configured time zone.
  56. """
  57. offset = DateTime.fromtimestamp(timeStamp) - DateTime.utcfromtimestamp(
  58. timeStamp
  59. )
  60. return cls(offset)
  61. def utcoffset(self, dt: Optional[DateTime]) -> TimeDelta:
  62. """
  63. Return the given timezone's offset from UTC.
  64. """
  65. return self.offset
  66. def dst(self, dt: Optional[DateTime]) -> TimeDelta:
  67. """
  68. Return a zero L{TimeDelta} for the daylight saving time
  69. offset, since there is never one.
  70. """
  71. return TimeDelta(0)
  72. def tzname(self, dt: Optional[DateTime]) -> str:
  73. """
  74. Return a string describing this timezone.
  75. """
  76. if self.name is not None:
  77. return self.name
  78. # XXX this is wrong; the tests are
  79. dt = DateTime.fromtimestamp(0, self)
  80. return dt.strftime("UTC%z")
  81. UTC = FixedOffsetTimeZone.fromSignHoursMinutes("+", 0, 0)