Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
2092206b
Unverified
Kaydet (Commit)
2092206b
authored
Agu 02, 2018
tarafından
Tim Graham
Kaydeden (comit)
GitHub
Agu 02, 2018
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Refs #29600 -- Updated django.utils.datetime_safe now that Python 2 is unsupported.
üst
fa54ebc7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
17 deletions
+20
-17
datetime_safe.py
django/utils/datetime_safe.py
+7
-7
test_datetime_safe.py
tests/utils_tests/test_datetime_safe.py
+13
-10
No files found.
django/utils/datetime_safe.py
Dosyayı görüntüle @
2092206b
#
Python's datetime strftime doesn't handle dates before 1900.
#
These classes override date and datetime to support the formatting of a date
#
through its full "proleptic Gregorian" date range.
#
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
#
# Based on code submitted to comp.lang.python by Andrew Dalke
#
# >>> datetime_safe.date(1
85
0, 8, 2).strftime("%Y/%m/%d was a %A")
# '
1850/08/02 was a Fri
day'
# >>> datetime_safe.date(10, 8, 2).strftime("%Y/%m/%d was a %A")
# '
0010/08/02 was a Mon
day'
import
re
import
time
as
ttime
...
...
@@ -71,11 +71,11 @@ def _findall(text, substr):
def
strftime
(
dt
,
fmt
):
if
dt
.
year
>=
1
9
00
:
if
dt
.
year
>=
1
0
00
:
return
super
(
type
(
dt
),
dt
)
.
strftime
(
fmt
)
illegal_formatting
=
_illegal_formatting
.
search
(
fmt
)
if
illegal_formatting
:
raise
TypeError
(
"strftime of dates before 1
900 does not handle
"
+
illegal_formatting
.
group
(
0
))
raise
TypeError
(
"strftime of dates before 1
000 does not handle
"
+
illegal_formatting
.
group
(
0
))
year
=
dt
.
year
# For every non-leap year century, advance by
...
...
tests/utils_tests/test_datetime_safe.py
Dosyayı görüntüle @
2092206b
import
unittest
from
datetime
import
(
date
as
original_date
,
datetime
as
original_datetime
,
time
as
original_time
,
)
from
django.test
import
SimpleTestCase
from
django.utils.datetime_safe
import
date
,
datetime
,
time
class
DatetimeTests
(
unittest
.
TestCase
):
class
DatetimeTests
(
Simple
TestCase
):
def
setUp
(
self
):
self
.
just_safe
=
(
1900
,
1
,
1
)
self
.
just_unsafe
=
(
1899
,
12
,
31
,
23
,
59
,
59
)
self
.
percent_y_safe
=
(
1900
,
1
,
1
)
# >= 1900 required on Windows.
self
.
just_safe
=
(
1000
,
1
,
1
)
self
.
just_unsafe
=
(
999
,
12
,
31
,
23
,
59
,
59
)
self
.
just_time
=
(
11
,
30
,
59
)
self
.
really_old
=
(
20
,
1
,
1
)
self
.
more_recent
=
(
2006
,
1
,
1
)
...
...
@@ -34,21 +35,23 @@ class DatetimeTests(unittest.TestCase):
)
def
test_safe_strftime
(
self
):
self
.
assertEqual
(
date
(
*
self
.
just_unsafe
[:
3
])
.
strftime
(
'
%
Y-
%
m-
%
d (weekday
%
w)'
),
'
1899-12-31 (weekday 0
)'
)
self
.
assertEqual
(
date
(
*
self
.
just_safe
)
.
strftime
(
'
%
Y-
%
m-
%
d (weekday
%
w)'
),
'1
900-01-01 (weekday 1
)'
)
self
.
assertEqual
(
date
(
*
self
.
just_unsafe
[:
3
])
.
strftime
(
'
%
Y-
%
m-
%
d (weekday
%
w)'
),
'
0999-12-31 (weekday 2
)'
)
self
.
assertEqual
(
date
(
*
self
.
just_safe
)
.
strftime
(
'
%
Y-
%
m-
%
d (weekday
%
w)'
),
'1
000-01-01 (weekday 3
)'
)
self
.
assertEqual
(
datetime
(
*
self
.
just_unsafe
)
.
strftime
(
'
%
Y-
%
m-
%
d
%
H:
%
M:
%
S (weekday
%
w)'
),
'
1899-12-31 23:59:59 (weekday 0
)'
datetime
(
*
self
.
just_unsafe
)
.
strftime
(
'
%
Y-
%
m-
%
d
%
H:
%
M:
%
S (weekday
%
w)'
),
'
0999-12-31 23:59:59 (weekday 2
)'
)
self
.
assertEqual
(
datetime
(
*
self
.
just_safe
)
.
strftime
(
'
%
Y-
%
m-
%
d
%
H:
%
M:
%
S (weekday
%
w)'
),
'1
900-01-01 00:00:00 (weekday 1
)'
datetime
(
*
self
.
just_safe
)
.
strftime
(
'
%
Y-
%
m-
%
d
%
H:
%
M:
%
S (weekday
%
w)'
),
'1
000-01-01 00:00:00 (weekday 3
)'
)
self
.
assertEqual
(
time
(
*
self
.
just_time
)
.
strftime
(
'
%
H:
%
M:
%
S AM'
),
'11:30:59 AM'
)
# %y will error before this date
self
.
assertEqual
(
date
(
*
self
.
just_safe
)
.
strftime
(
'
%
y'
),
'00'
)
self
.
assertEqual
(
datetime
(
*
self
.
just_safe
)
.
strftime
(
'
%
y'
),
'00'
)
self
.
assertEqual
(
date
(
*
self
.
percent_y_safe
)
.
strftime
(
'
%
y'
),
'00'
)
self
.
assertEqual
(
datetime
(
*
self
.
percent_y_safe
)
.
strftime
(
'
%
y'
),
'00'
)
with
self
.
assertRaisesMessage
(
TypeError
,
'strftime of dates before 1000 does not handle
%
y'
):
datetime
(
*
self
.
just_unsafe
)
.
strftime
(
'
%
y'
)
self
.
assertEqual
(
date
(
1850
,
8
,
2
)
.
strftime
(
"
%
Y/
%
m/
%
d was a
%
A"
),
'1850/08/02 was a Friday'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment