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.

datetime_safe.py 3.0KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # These classes override date and datetime to ensure that strftime('%Y')
  2. # returns four digits (with leading zeros) on years < 1000.
  3. # https://bugs.python.org/issue13305
  4. #
  5. # Based on code submitted to comp.lang.python by Andrew Dalke
  6. #
  7. # >>> datetime_safe.date(10, 8, 2).strftime("%Y/%m/%d was a %A")
  8. # '0010/08/02 was a Monday'
  9. import time
  10. import warnings
  11. from datetime import date as real_date
  12. from datetime import datetime as real_datetime
  13. from django.utils.deprecation import RemovedInDjango50Warning
  14. from django.utils.regex_helper import _lazy_re_compile
  15. warnings.warn(
  16. "The django.utils.datetime_safe module is deprecated.",
  17. category=RemovedInDjango50Warning,
  18. stacklevel=2,
  19. )
  20. class date(real_date):
  21. def strftime(self, fmt):
  22. return strftime(self, fmt)
  23. class datetime(real_datetime):
  24. def strftime(self, fmt):
  25. return strftime(self, fmt)
  26. @classmethod
  27. def combine(cls, date, time):
  28. return cls(
  29. date.year,
  30. date.month,
  31. date.day,
  32. time.hour,
  33. time.minute,
  34. time.second,
  35. time.microsecond,
  36. time.tzinfo,
  37. )
  38. def date(self):
  39. return date(self.year, self.month, self.day)
  40. def new_date(d):
  41. "Generate a safe date from a datetime.date object."
  42. return date(d.year, d.month, d.day)
  43. def new_datetime(d):
  44. """
  45. Generate a safe datetime from a datetime.date or datetime.datetime object.
  46. """
  47. kw = [d.year, d.month, d.day]
  48. if isinstance(d, real_datetime):
  49. kw.extend([d.hour, d.minute, d.second, d.microsecond, d.tzinfo])
  50. return datetime(*kw)
  51. # This library does not support strftime's "%s" or "%y" format strings.
  52. # Allowed if there's an even number of "%"s because they are escaped.
  53. _illegal_formatting = _lazy_re_compile(r"((^|[^%])(%%)*%[sy])")
  54. def _findall(text, substr):
  55. # Also finds overlaps
  56. sites = []
  57. i = 0
  58. while True:
  59. i = text.find(substr, i)
  60. if i == -1:
  61. break
  62. sites.append(i)
  63. i += 1
  64. return sites
  65. def strftime(dt, fmt):
  66. if dt.year >= 1000:
  67. return super(type(dt), dt).strftime(fmt)
  68. illegal_formatting = _illegal_formatting.search(fmt)
  69. if illegal_formatting:
  70. raise TypeError(
  71. "strftime of dates before 1000 does not handle " + illegal_formatting[0]
  72. )
  73. year = dt.year
  74. # For every non-leap year century, advance by
  75. # 6 years to get into the 28-year repeat cycle
  76. delta = 2000 - year
  77. off = 6 * (delta // 100 + delta // 400)
  78. year = year + off
  79. # Move to around the year 2000
  80. year = year + ((2000 - year) // 28) * 28
  81. timetuple = dt.timetuple()
  82. s1 = time.strftime(fmt, (year,) + timetuple[1:])
  83. sites1 = _findall(s1, str(year))
  84. s2 = time.strftime(fmt, (year + 28,) + timetuple[1:])
  85. sites2 = _findall(s2, str(year + 28))
  86. sites = []
  87. for site in sites1:
  88. if site in sites2:
  89. sites.append(site)
  90. s = s1
  91. syear = "%04d" % dt.year
  92. for site in sites:
  93. s = s[:site] + syear + s[site + 4 :]
  94. return s