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
1109ebd7
Kaydet (Commit)
1109ebd7
authored
May 16, 2014
tarafından
Aymeric Augustin
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Optimized make_aware/naive by removing redundant checks. Refs #22625.
Also added tests with pytz and removed misplaced tests.
üst
fa89acf1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
36 deletions
+42
-36
timezone.py
django/utils/timezone.py
+12
-8
tests.py
tests/timezones/tests.py
+0
-28
test_timezone.py
tests/utils_tests/test_timezone.py
+30
-0
No files found.
django/utils/timezone.py
Dosyayı görüntüle @
1109ebd7
...
@@ -306,9 +306,11 @@ def localtime(value, timezone=None):
...
@@ -306,9 +306,11 @@ def localtime(value, timezone=None):
"""
"""
if
timezone
is
None
:
if
timezone
is
None
:
timezone
=
get_current_timezone
()
timezone
=
get_current_timezone
()
# If `value` is naive, astimezone() will raise a ValueError,
# so we don't need to perform a redundant check.
value
=
value
.
astimezone
(
timezone
)
value
=
value
.
astimezone
(
timezone
)
if
hasattr
(
timezone
,
'normalize'
):
if
hasattr
(
timezone
,
'normalize'
):
#
available for pytz time zones
#
This method is available for pytz time zones.
value
=
timezone
.
normalize
(
value
)
value
=
timezone
.
normalize
(
value
)
return
value
return
value
...
@@ -351,13 +353,15 @@ def make_aware(value, timezone):
...
@@ -351,13 +353,15 @@ def make_aware(value, timezone):
"""
"""
Makes a naive datetime.datetime in a given time zone aware.
Makes a naive datetime.datetime in a given time zone aware.
"""
"""
if
is_aware
(
value
):
raise
ValueError
(
"make_aware expects a naive value, got
%
s"
%
value
)
if
hasattr
(
timezone
,
'localize'
):
if
hasattr
(
timezone
,
'localize'
):
#
available for pytz time zones
#
This method is available for pytz time zones.
return
timezone
.
localize
(
value
,
is_dst
=
None
)
return
timezone
.
localize
(
value
,
is_dst
=
None
)
else
:
else
:
# may be wrong around DST changes
# Check that we won't overwrite the timezone of an aware datetime.
if
is_aware
(
value
):
raise
ValueError
(
"make_aware expects a naive datetime, got
%
s"
%
value
)
# This may be wrong around DST changes!
return
value
.
replace
(
tzinfo
=
timezone
)
return
value
.
replace
(
tzinfo
=
timezone
)
...
@@ -365,10 +369,10 @@ def make_naive(value, timezone):
...
@@ -365,10 +369,10 @@ def make_naive(value, timezone):
"""
"""
Makes an aware datetime.datetime naive in a given time zone.
Makes an aware datetime.datetime naive in a given time zone.
"""
"""
if
is_naive
(
value
):
# If `value` is naive, astimezone() will raise a ValueError,
raise
ValueError
(
"make_naive expects an aware value, got
%
s"
%
value
)
# so we don't need to perform a redundant check.
value
=
value
.
astimezone
(
timezone
)
value
=
value
.
astimezone
(
timezone
)
if
hasattr
(
timezone
,
'normalize'
):
if
hasattr
(
timezone
,
'normalize'
):
#
available for pytz time zones
#
This method is available for pytz time zones.
value
=
timezone
.
normalize
(
value
)
value
=
timezone
.
normalize
(
value
)
return
value
.
replace
(
tzinfo
=
None
)
return
value
.
replace
(
tzinfo
=
None
)
tests/timezones/tests.py
Dosyayı görüntüle @
1109ebd7
...
@@ -1120,31 +1120,3 @@ class AdminTests(TestCase):
...
@@ -1120,31 +1120,3 @@ class AdminTests(TestCase):
with
timezone
.
override
(
ICT
):
with
timezone
.
override
(
ICT
):
response
=
self
.
client
.
get
(
reverse
(
'admin:timezones_timestamp_change'
,
args
=
(
t
.
pk
,)))
response
=
self
.
client
.
get
(
reverse
(
'admin:timezones_timestamp_change'
,
args
=
(
t
.
pk
,)))
self
.
assertContains
(
response
,
t
.
created
.
astimezone
(
ICT
)
.
isoformat
())
self
.
assertContains
(
response
,
t
.
created
.
astimezone
(
ICT
)
.
isoformat
())
@override_settings
(
TIME_ZONE
=
'Africa/Nairobi'
)
class
UtilitiesTests
(
TestCase
):
def
test_make_aware
(
self
):
self
.
assertEqual
(
timezone
.
make_aware
(
datetime
.
datetime
(
2011
,
9
,
1
,
13
,
20
,
30
),
EAT
),
datetime
.
datetime
(
2011
,
9
,
1
,
13
,
20
,
30
,
tzinfo
=
EAT
)
)
self
.
assertEqual
(
timezone
.
make_aware
(
datetime
.
datetime
(
2011
,
9
,
1
,
10
,
20
,
30
),
UTC
),
datetime
.
datetime
(
2011
,
9
,
1
,
10
,
20
,
30
,
tzinfo
=
UTC
)
)
def
test_make_naive
(
self
):
self
.
assertEqual
(
timezone
.
make_naive
(
datetime
.
datetime
(
2011
,
9
,
1
,
13
,
20
,
30
,
tzinfo
=
EAT
),
EAT
),
datetime
.
datetime
(
2011
,
9
,
1
,
13
,
20
,
30
)
)
self
.
assertEqual
(
timezone
.
make_naive
(
datetime
.
datetime
(
2011
,
9
,
1
,
13
,
20
,
30
,
tzinfo
=
EAT
),
UTC
),
datetime
.
datetime
(
2011
,
9
,
1
,
10
,
20
,
30
)
)
self
.
assertEqual
(
timezone
.
make_naive
(
datetime
.
datetime
(
2011
,
9
,
1
,
10
,
20
,
30
,
tzinfo
=
UTC
),
UTC
),
datetime
.
datetime
(
2011
,
9
,
1
,
10
,
20
,
30
)
)
tests/utils_tests/test_timezone.py
Dosyayı görüntüle @
1109ebd7
...
@@ -3,10 +3,17 @@ import datetime
...
@@ -3,10 +3,17 @@ import datetime
import
pickle
import
pickle
import
unittest
import
unittest
try
:
import
pytz
except
ImportError
:
pytz
=
None
from
django.test
import
override_settings
from
django.test
import
override_settings
from
django.utils
import
timezone
from
django.utils
import
timezone
if
pytz
is
not
None
:
CET
=
pytz
.
timezone
(
"Europe/Paris"
)
EAT
=
timezone
.
get_fixed_timezone
(
180
)
# Africa/Nairobi
EAT
=
timezone
.
get_fixed_timezone
(
180
)
# Africa/Nairobi
ICT
=
timezone
.
get_fixed_timezone
(
420
)
# Asia/Bangkok
ICT
=
timezone
.
get_fixed_timezone
(
420
)
# Asia/Bangkok
...
@@ -19,6 +26,10 @@ class TimezoneTests(unittest.TestCase):
...
@@ -19,6 +26,10 @@ class TimezoneTests(unittest.TestCase):
local_now
=
timezone
.
localtime
(
now
,
local_tz
)
local_now
=
timezone
.
localtime
(
now
,
local_tz
)
self
.
assertEqual
(
local_now
.
tzinfo
,
local_tz
)
self
.
assertEqual
(
local_now
.
tzinfo
,
local_tz
)
def
test_localtime_naive
(
self
):
with
self
.
assertRaises
(
ValueError
):
timezone
.
localtime
(
datetime
.
datetime
.
now
())
def
test_localtime_out_of_range
(
self
):
def
test_localtime_out_of_range
(
self
):
local_tz
=
timezone
.
LocalTimezone
()
local_tz
=
timezone
.
LocalTimezone
()
long_ago
=
datetime
.
datetime
(
1900
,
1
,
1
,
tzinfo
=
timezone
.
utc
)
long_ago
=
datetime
.
datetime
(
1900
,
1
,
1
,
tzinfo
=
timezone
.
utc
)
...
@@ -96,3 +107,22 @@ class TimezoneTests(unittest.TestCase):
...
@@ -96,3 +107,22 @@ class TimezoneTests(unittest.TestCase):
datetime
.
datetime
(
2011
,
9
,
1
,
13
,
20
,
30
))
datetime
.
datetime
(
2011
,
9
,
1
,
13
,
20
,
30
))
with
self
.
assertRaises
(
ValueError
):
with
self
.
assertRaises
(
ValueError
):
timezone
.
make_naive
(
datetime
.
datetime
(
2011
,
9
,
1
,
13
,
20
,
30
),
EAT
)
timezone
.
make_naive
(
datetime
.
datetime
(
2011
,
9
,
1
,
13
,
20
,
30
),
EAT
)
@unittest.skipIf
(
pytz
is
None
,
"this test requires pytz"
)
def
test_make_aware
(
self
):
self
.
assertEqual
(
timezone
.
make_aware
(
datetime
.
datetime
(
2011
,
9
,
1
,
12
,
20
,
30
),
CET
),
CET
.
localize
(
datetime
.
datetime
(
2011
,
9
,
1
,
12
,
20
,
30
)))
with
self
.
assertRaises
(
ValueError
):
timezone
.
make_aware
(
CET
.
localize
(
datetime
.
datetime
(
2011
,
9
,
1
,
12
,
20
,
30
)),
CET
)
@unittest.skipIf
(
pytz
is
None
,
"this test requires pytz"
)
def
test_make_aware_pytz
(
self
):
self
.
assertEqual
(
timezone
.
make_naive
(
CET
.
localize
(
datetime
.
datetime
(
2011
,
9
,
1
,
12
,
20
,
30
)),
CET
),
datetime
.
datetime
(
2011
,
9
,
1
,
12
,
20
,
30
))
self
.
assertEqual
(
timezone
.
make_naive
(
pytz
.
timezone
(
"Asia/Bangkok"
)
.
localize
(
datetime
.
datetime
(
2011
,
9
,
1
,
17
,
20
,
30
)),
CET
),
datetime
.
datetime
(
2011
,
9
,
1
,
12
,
20
,
30
))
with
self
.
assertRaises
(
ValueError
):
timezone
.
make_naive
(
datetime
.
datetime
(
2011
,
9
,
1
,
12
,
20
,
30
),
CET
)
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