2018-08-02 22:20:24 +08:00
|
|
|
# These classes override date and datetime to ensure that strftime('%Y')
|
|
|
|
# returns four digits (with leading zeros) on years < 1000.
|
|
|
|
# https://bugs.python.org/issue13305
|
2008-07-18 11:47:27 +08:00
|
|
|
#
|
|
|
|
# Based on code submitted to comp.lang.python by Andrew Dalke
|
|
|
|
#
|
2018-08-02 22:20:24 +08:00
|
|
|
# >>> datetime_safe.date(10, 8, 2).strftime("%Y/%m/%d was a %A")
|
|
|
|
# '0010/08/02 was a Monday'
|
2008-07-18 11:47:27 +08:00
|
|
|
|
2021-05-11 21:03:55 +08:00
|
|
|
import time
|
2021-05-08 01:20:14 +08:00
|
|
|
import warnings
|
2021-05-11 21:03:55 +08:00
|
|
|
from datetime import date as real_date, datetime as real_datetime
|
2008-07-18 11:47:27 +08:00
|
|
|
|
2021-05-08 01:20:14 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango50Warning
|
2019-10-26 22:42:32 +08:00
|
|
|
from django.utils.regex_helper import _lazy_re_compile
|
|
|
|
|
2021-05-08 01:20:14 +08:00
|
|
|
warnings.warn(
|
|
|
|
'The django.utils.datetime_safe module is deprecated.',
|
|
|
|
category=RemovedInDjango50Warning,
|
|
|
|
stacklevel=2,
|
|
|
|
)
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2008-07-18 11:47:27 +08:00
|
|
|
class date(real_date):
|
|
|
|
def strftime(self, fmt):
|
|
|
|
return strftime(self, fmt)
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2008-07-18 11:47:27 +08:00
|
|
|
class datetime(real_datetime):
|
|
|
|
def strftime(self, fmt):
|
|
|
|
return strftime(self, fmt)
|
|
|
|
|
2013-10-14 01:09:26 +08:00
|
|
|
@classmethod
|
|
|
|
def combine(cls, date, time):
|
|
|
|
return cls(date.year, date.month, date.day,
|
|
|
|
time.hour, time.minute, time.second,
|
|
|
|
time.microsecond, time.tzinfo)
|
2008-07-18 11:47:27 +08:00
|
|
|
|
|
|
|
def date(self):
|
|
|
|
return date(self.year, self.month, self.day)
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2008-07-18 11:47:27 +08:00
|
|
|
def new_date(d):
|
|
|
|
"Generate a safe date from a datetime.date object."
|
|
|
|
return date(d.year, d.month, d.day)
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2008-07-18 11:47:27 +08:00
|
|
|
def new_datetime(d):
|
|
|
|
"""
|
|
|
|
Generate a safe datetime from a datetime.date or datetime.datetime object.
|
|
|
|
"""
|
|
|
|
kw = [d.year, d.month, d.day]
|
|
|
|
if isinstance(d, real_datetime):
|
|
|
|
kw.extend([d.hour, d.minute, d.second, d.microsecond, d.tzinfo])
|
|
|
|
return datetime(*kw)
|
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2008-07-18 11:47:27 +08:00
|
|
|
# This library does not support strftime's "%s" or "%y" format strings.
|
|
|
|
# Allowed if there's an even number of "%"s because they are escaped.
|
2019-10-26 22:42:32 +08:00
|
|
|
_illegal_formatting = _lazy_re_compile(r"((^|[^%])(%%)*%[sy])")
|
2008-07-18 11:47:27 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2008-07-18 11:47:27 +08:00
|
|
|
def _findall(text, substr):
|
|
|
|
# Also finds overlaps
|
|
|
|
sites = []
|
|
|
|
i = 0
|
2018-01-03 01:31:06 +08:00
|
|
|
while True:
|
|
|
|
i = text.find(substr, i)
|
|
|
|
if i == -1:
|
2008-07-18 11:47:27 +08:00
|
|
|
break
|
2018-01-03 01:31:06 +08:00
|
|
|
sites.append(i)
|
|
|
|
i += 1
|
2008-07-18 11:47:27 +08:00
|
|
|
return sites
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2008-07-18 11:47:27 +08:00
|
|
|
def strftime(dt, fmt):
|
2018-08-02 22:20:24 +08:00
|
|
|
if dt.year >= 1000:
|
2008-07-18 11:47:27 +08:00
|
|
|
return super(type(dt), dt).strftime(fmt)
|
|
|
|
illegal_formatting = _illegal_formatting.search(fmt)
|
|
|
|
if illegal_formatting:
|
2020-05-11 04:03:39 +08:00
|
|
|
raise TypeError('strftime of dates before 1000 does not handle ' + illegal_formatting[0])
|
2008-07-18 11:47:27 +08:00
|
|
|
|
|
|
|
year = dt.year
|
|
|
|
# For every non-leap year century, advance by
|
|
|
|
# 6 years to get into the 28-year repeat cycle
|
|
|
|
delta = 2000 - year
|
|
|
|
off = 6 * (delta // 100 + delta // 400)
|
|
|
|
year = year + off
|
|
|
|
|
|
|
|
# Move to around the year 2000
|
|
|
|
year = year + ((2000 - year) // 28) * 28
|
|
|
|
timetuple = dt.timetuple()
|
2021-05-11 21:03:55 +08:00
|
|
|
s1 = time.strftime(fmt, (year,) + timetuple[1:])
|
2008-07-18 11:47:27 +08:00
|
|
|
sites1 = _findall(s1, str(year))
|
|
|
|
|
2021-05-11 21:03:55 +08:00
|
|
|
s2 = time.strftime(fmt, (year + 28,) + timetuple[1:])
|
2013-11-04 02:08:55 +08:00
|
|
|
sites2 = _findall(s2, str(year + 28))
|
2008-07-18 11:47:27 +08:00
|
|
|
|
|
|
|
sites = []
|
|
|
|
for site in sites1:
|
|
|
|
if site in sites2:
|
|
|
|
sites.append(site)
|
|
|
|
|
|
|
|
s = s1
|
2020-04-25 21:53:30 +08:00
|
|
|
syear = "%04d" % dt.year
|
2008-07-18 11:47:27 +08:00
|
|
|
for site in sites:
|
2013-11-04 02:08:55 +08:00
|
|
|
s = s[:site] + syear + s[site + 4:]
|
2008-07-18 11:47:27 +08:00
|
|
|
return s
|