2011-11-18 21:01:06 +08:00
|
|
|
|
import datetime
|
2013-01-12 00:49:55 +08:00
|
|
|
|
import re
|
2011-11-20 15:40:19 +08:00
|
|
|
|
import sys
|
2023-01-18 16:46:01 +08:00
|
|
|
|
import zoneinfo
|
2015-06-05 19:49:32 +08:00
|
|
|
|
from contextlib import contextmanager
|
2015-05-03 15:06:06 +08:00
|
|
|
|
from unittest import SkipTest, skipIf
|
2012-10-26 05:14:17 +08:00
|
|
|
|
from xml.dom.minidom import parseString
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
2015-02-23 08:53:57 +08:00
|
|
|
|
from django.contrib.auth.models import User
|
2011-11-18 21:01:06 +08:00
|
|
|
|
from django.core import serializers
|
2019-07-20 21:38:43 +08:00
|
|
|
|
from django.db import connection
|
2016-11-30 01:23:44 +08:00
|
|
|
|
from django.db.models import F, Max, Min
|
2011-11-18 21:01:06 +08:00
|
|
|
|
from django.http import HttpRequest
|
2015-01-13 05:31:44 +08:00
|
|
|
|
from django.template import (
|
2015-01-28 20:35:27 +08:00
|
|
|
|
Context,
|
|
|
|
|
RequestContext,
|
|
|
|
|
Template,
|
|
|
|
|
TemplateSyntaxError,
|
|
|
|
|
context_processors,
|
|
|
|
|
)
|
2015-01-13 05:31:44 +08:00
|
|
|
|
from django.test import (
|
2021-09-09 13:42:05 +08:00
|
|
|
|
SimpleTestCase,
|
|
|
|
|
TestCase,
|
|
|
|
|
TransactionTestCase,
|
|
|
|
|
override_settings,
|
|
|
|
|
skipIfDBFeature,
|
|
|
|
|
skipUnlessDBFeature,
|
2015-01-28 20:35:27 +08:00
|
|
|
|
)
|
2013-12-23 23:01:13 +08:00
|
|
|
|
from django.test.utils import requires_tz_support
|
2015-12-30 23:51:16 +08:00
|
|
|
|
from django.urls import reverse
|
2023-01-06 21:46:33 +08:00
|
|
|
|
from django.utils import timezone, translation
|
2016-11-30 01:23:44 +08:00
|
|
|
|
from django.utils.timezone import timedelta
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
|
from .forms import (
|
|
|
|
|
EventForm,
|
|
|
|
|
EventLocalizedForm,
|
|
|
|
|
EventLocalizedModelForm,
|
|
|
|
|
EventModelForm,
|
|
|
|
|
EventSplitForm,
|
|
|
|
|
)
|
|
|
|
|
from .models import (
|
2021-07-01 21:19:09 +08:00
|
|
|
|
AllDayEvent,
|
|
|
|
|
DailyEvent,
|
|
|
|
|
Event,
|
|
|
|
|
MaybeEvent,
|
|
|
|
|
Session,
|
|
|
|
|
SessionEvent,
|
|
|
|
|
Timestamp,
|
2015-01-28 20:35:27 +08:00
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
2020-01-07 16:54:22 +08:00
|
|
|
|
try:
|
|
|
|
|
import yaml
|
2022-02-04 03:24:19 +08:00
|
|
|
|
|
2020-01-07 16:54:22 +08:00
|
|
|
|
HAS_YAML = True
|
|
|
|
|
except ImportError:
|
|
|
|
|
HAS_YAML = False
|
|
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
|
# These tests use the EAT (Eastern Africa Time) and ICT (Indochina Time)
|
2021-07-30 20:41:35 +08:00
|
|
|
|
# who don't have daylight saving time, so we can represent them easily
|
2018-07-10 04:33:36 +08:00
|
|
|
|
# with fixed offset timezones and use them directly as tzinfo in the
|
|
|
|
|
# constructors.
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
|
|
# settings.TIME_ZONE is forced to EAT. Most tests use a variant of
|
|
|
|
|
# datetime.datetime(2011, 9, 1, 13, 20, 30), which translates to
|
|
|
|
|
# 10:20:30 in UTC and 17:20:30 in ICT.
|
|
|
|
|
|
2022-03-23 19:15:36 +08:00
|
|
|
|
UTC = datetime.timezone.utc
|
2013-09-08 15:04:31 +08:00
|
|
|
|
EAT = timezone.get_fixed_timezone(180) # Africa/Nairobi
|
|
|
|
|
ICT = timezone.get_fixed_timezone(420) # Asia/Bangkok
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-30 05:07:29 +08:00
|
|
|
|
@contextmanager
|
|
|
|
|
def override_database_connection_timezone(timezone):
|
|
|
|
|
try:
|
|
|
|
|
orig_timezone = connection.settings_dict["TIME_ZONE"]
|
|
|
|
|
connection.settings_dict["TIME_ZONE"] = timezone
|
|
|
|
|
# Clear cached properties, after first accessing them to ensure they exist.
|
|
|
|
|
connection.timezone
|
|
|
|
|
del connection.timezone
|
|
|
|
|
connection.timezone_name
|
|
|
|
|
del connection.timezone_name
|
|
|
|
|
yield
|
|
|
|
|
finally:
|
|
|
|
|
connection.settings_dict["TIME_ZONE"] = orig_timezone
|
|
|
|
|
# Clear cached properties, after first accessing them to ensure they exist.
|
|
|
|
|
connection.timezone
|
|
|
|
|
del connection.timezone
|
|
|
|
|
connection.timezone_name
|
|
|
|
|
del connection.timezone_name
|
|
|
|
|
|
|
|
|
|
|
2012-04-29 22:03:46 +08:00
|
|
|
|
@override_settings(TIME_ZONE="Africa/Nairobi", USE_TZ=False)
|
|
|
|
|
class LegacyDatabaseTests(TestCase):
|
2011-11-18 21:01:06 +08:00
|
|
|
|
def test_naive_datetime(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 13, 20, 30)
|
|
|
|
|
Event.objects.create(dt=dt)
|
|
|
|
|
event = Event.objects.get()
|
|
|
|
|
self.assertEqual(event.dt, dt)
|
|
|
|
|
|
|
|
|
|
def test_naive_datetime_with_microsecond(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 13, 20, 30, 405060)
|
|
|
|
|
Event.objects.create(dt=dt)
|
|
|
|
|
event = Event.objects.get()
|
|
|
|
|
self.assertEqual(event.dt, dt)
|
|
|
|
|
|
|
|
|
|
@skipUnlessDBFeature("supports_timezones")
|
|
|
|
|
def test_aware_datetime_in_local_timezone(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT)
|
|
|
|
|
Event.objects.create(dt=dt)
|
|
|
|
|
event = Event.objects.get()
|
|
|
|
|
self.assertIsNone(event.dt.tzinfo)
|
|
|
|
|
# interpret the naive datetime in local time to get the correct value
|
|
|
|
|
self.assertEqual(event.dt.replace(tzinfo=EAT), dt)
|
|
|
|
|
|
|
|
|
|
@skipUnlessDBFeature("supports_timezones")
|
|
|
|
|
def test_aware_datetime_in_local_timezone_with_microsecond(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 13, 20, 30, 405060, tzinfo=EAT)
|
|
|
|
|
Event.objects.create(dt=dt)
|
|
|
|
|
event = Event.objects.get()
|
|
|
|
|
self.assertIsNone(event.dt.tzinfo)
|
|
|
|
|
# interpret the naive datetime in local time to get the correct value
|
|
|
|
|
self.assertEqual(event.dt.replace(tzinfo=EAT), dt)
|
|
|
|
|
|
|
|
|
|
@skipUnlessDBFeature("supports_timezones")
|
|
|
|
|
def test_aware_datetime_in_utc(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC)
|
|
|
|
|
Event.objects.create(dt=dt)
|
|
|
|
|
event = Event.objects.get()
|
|
|
|
|
self.assertIsNone(event.dt.tzinfo)
|
|
|
|
|
# interpret the naive datetime in local time to get the correct value
|
|
|
|
|
self.assertEqual(event.dt.replace(tzinfo=EAT), dt)
|
|
|
|
|
|
|
|
|
|
@skipUnlessDBFeature("supports_timezones")
|
|
|
|
|
def test_aware_datetime_in_other_timezone(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 17, 20, 30, tzinfo=ICT)
|
|
|
|
|
Event.objects.create(dt=dt)
|
|
|
|
|
event = Event.objects.get()
|
|
|
|
|
self.assertIsNone(event.dt.tzinfo)
|
|
|
|
|
# interpret the naive datetime in local time to get the correct value
|
|
|
|
|
self.assertEqual(event.dt.replace(tzinfo=EAT), dt)
|
|
|
|
|
|
|
|
|
|
@skipIfDBFeature("supports_timezones")
|
2015-12-03 07:55:50 +08:00
|
|
|
|
def test_aware_datetime_unsupported(self):
|
2011-11-18 21:01:06 +08:00
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT)
|
2017-05-29 03:37:21 +08:00
|
|
|
|
msg = "backend does not support timezone-aware datetimes when USE_TZ is False."
|
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
2011-11-18 21:01:06 +08:00
|
|
|
|
Event.objects.create(dt=dt)
|
|
|
|
|
|
|
|
|
|
def test_auto_now_and_auto_now_add(self):
|
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
|
past = now - datetime.timedelta(seconds=2)
|
|
|
|
|
future = now + datetime.timedelta(seconds=2)
|
|
|
|
|
Timestamp.objects.create()
|
|
|
|
|
ts = Timestamp.objects.get()
|
|
|
|
|
self.assertLess(past, ts.created)
|
|
|
|
|
self.assertLess(past, ts.updated)
|
|
|
|
|
self.assertGreater(future, ts.updated)
|
|
|
|
|
self.assertGreater(future, ts.updated)
|
|
|
|
|
|
|
|
|
|
def test_query_filter(self):
|
|
|
|
|
dt1 = datetime.datetime(2011, 9, 1, 12, 20, 30)
|
|
|
|
|
dt2 = datetime.datetime(2011, 9, 1, 14, 20, 30)
|
|
|
|
|
Event.objects.create(dt=dt1)
|
|
|
|
|
Event.objects.create(dt=dt2)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__gte=dt1).count(), 2)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__gt=dt1).count(), 1)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__gte=dt2).count(), 1)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__gt=dt2).count(), 0)
|
|
|
|
|
|
2013-02-10 23:15:49 +08:00
|
|
|
|
def test_query_datetime_lookups(self):
|
2011-11-18 21:01:06 +08:00
|
|
|
|
Event.objects.create(dt=datetime.datetime(2011, 1, 1, 1, 30, 0))
|
|
|
|
|
Event.objects.create(dt=datetime.datetime(2011, 1, 1, 4, 30, 0))
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__year=2011).count(), 2)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__month=1).count(), 2)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__day=1).count(), 2)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__week_day=7).count(), 2)
|
2019-10-01 06:12:19 +08:00
|
|
|
|
self.assertEqual(Event.objects.filter(dt__iso_week_day=6).count(), 2)
|
2013-02-10 23:15:49 +08:00
|
|
|
|
self.assertEqual(Event.objects.filter(dt__hour=1).count(), 1)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__minute=30).count(), 2)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__second=0).count(), 2)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
|
|
def test_query_aggregation(self):
|
|
|
|
|
# Only min and max make sense for datetimes.
|
|
|
|
|
Event.objects.create(dt=datetime.datetime(2011, 9, 1, 23, 20, 20))
|
|
|
|
|
Event.objects.create(dt=datetime.datetime(2011, 9, 1, 13, 20, 30))
|
|
|
|
|
Event.objects.create(dt=datetime.datetime(2011, 9, 1, 3, 20, 40))
|
2022-02-22 17:29:38 +08:00
|
|
|
|
result = Event.objects.aggregate(Min("dt"), Max("dt"))
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
result,
|
|
|
|
|
{
|
|
|
|
|
"dt__min": datetime.datetime(2011, 9, 1, 3, 20, 40),
|
|
|
|
|
"dt__max": datetime.datetime(2011, 9, 1, 23, 20, 20),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
2012-02-23 03:40:27 +08:00
|
|
|
|
def test_query_annotation(self):
|
|
|
|
|
# Only min and max make sense for datetimes.
|
|
|
|
|
morning = Session.objects.create(name="morning")
|
|
|
|
|
afternoon = Session.objects.create(name="afternoon")
|
|
|
|
|
SessionEvent.objects.create(
|
|
|
|
|
dt=datetime.datetime(2011, 9, 1, 23, 20, 20), session=afternoon
|
|
|
|
|
)
|
|
|
|
|
SessionEvent.objects.create(
|
|
|
|
|
dt=datetime.datetime(2011, 9, 1, 13, 20, 30), session=afternoon
|
|
|
|
|
)
|
|
|
|
|
SessionEvent.objects.create(
|
|
|
|
|
dt=datetime.datetime(2011, 9, 1, 3, 20, 40), session=morning
|
|
|
|
|
)
|
|
|
|
|
morning_min_dt = datetime.datetime(2011, 9, 1, 3, 20, 40)
|
|
|
|
|
afternoon_min_dt = datetime.datetime(2011, 9, 1, 13, 20, 30)
|
2022-09-24 18:29:58 +08:00
|
|
|
|
self.assertQuerySetEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Session.objects.annotate(dt=Min("events__dt")).order_by("dt"),
|
|
|
|
|
[morning_min_dt, afternoon_min_dt],
|
2018-03-16 17:54:34 +08:00
|
|
|
|
transform=lambda d: d.dt,
|
|
|
|
|
)
|
2022-09-24 18:29:58 +08:00
|
|
|
|
self.assertQuerySetEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Session.objects.annotate(dt=Min("events__dt")).filter(
|
|
|
|
|
dt__lt=afternoon_min_dt
|
|
|
|
|
),
|
|
|
|
|
[morning_min_dt],
|
2018-03-16 17:54:34 +08:00
|
|
|
|
transform=lambda d: d.dt,
|
|
|
|
|
)
|
2022-09-24 18:29:58 +08:00
|
|
|
|
self.assertQuerySetEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Session.objects.annotate(dt=Min("events__dt")).filter(
|
|
|
|
|
dt__gte=afternoon_min_dt
|
|
|
|
|
),
|
|
|
|
|
[afternoon_min_dt],
|
2018-03-16 17:54:34 +08:00
|
|
|
|
transform=lambda d: d.dt,
|
|
|
|
|
)
|
2012-02-23 03:40:27 +08:00
|
|
|
|
|
2013-02-10 23:15:49 +08:00
|
|
|
|
def test_query_datetimes(self):
|
2011-11-18 21:01:06 +08:00
|
|
|
|
Event.objects.create(dt=datetime.datetime(2011, 1, 1, 1, 30, 0))
|
|
|
|
|
Event.objects.create(dt=datetime.datetime(2011, 1, 1, 4, 30, 0))
|
2016-09-10 17:36:27 +08:00
|
|
|
|
self.assertSequenceEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Event.objects.datetimes("dt", "year"),
|
|
|
|
|
[datetime.datetime(2011, 1, 1, 0, 0, 0)],
|
2022-02-04 03:24:19 +08:00
|
|
|
|
)
|
2016-09-10 17:36:27 +08:00
|
|
|
|
self.assertSequenceEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Event.objects.datetimes("dt", "month"),
|
2016-09-10 17:36:27 +08:00
|
|
|
|
[datetime.datetime(2011, 1, 1, 0, 0, 0)],
|
|
|
|
|
)
|
|
|
|
|
self.assertSequenceEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Event.objects.datetimes("dt", "day"),
|
|
|
|
|
[datetime.datetime(2011, 1, 1, 0, 0, 0)],
|
2016-09-10 17:36:27 +08:00
|
|
|
|
)
|
|
|
|
|
self.assertSequenceEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Event.objects.datetimes("dt", "hour"),
|
2022-02-04 03:24:19 +08:00
|
|
|
|
[
|
2013-10-20 07:33:10 +08:00
|
|
|
|
datetime.datetime(2011, 1, 1, 1, 0, 0),
|
2016-09-10 17:36:27 +08:00
|
|
|
|
datetime.datetime(2011, 1, 1, 4, 0, 0),
|
2022-02-04 03:24:19 +08:00
|
|
|
|
],
|
|
|
|
|
)
|
2016-09-10 17:36:27 +08:00
|
|
|
|
self.assertSequenceEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Event.objects.datetimes("dt", "minute"),
|
2022-02-04 03:24:19 +08:00
|
|
|
|
[
|
2013-10-20 07:33:10 +08:00
|
|
|
|
datetime.datetime(2011, 1, 1, 1, 30, 0),
|
2016-09-10 17:36:27 +08:00
|
|
|
|
datetime.datetime(2011, 1, 1, 4, 30, 0),
|
2022-02-04 03:24:19 +08:00
|
|
|
|
],
|
|
|
|
|
)
|
2016-09-10 17:36:27 +08:00
|
|
|
|
self.assertSequenceEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Event.objects.datetimes("dt", "second"),
|
|
|
|
|
[
|
|
|
|
|
datetime.datetime(2011, 1, 1, 1, 30, 0),
|
2016-09-10 17:36:27 +08:00
|
|
|
|
datetime.datetime(2011, 1, 1, 4, 30, 0),
|
2022-02-04 03:24:19 +08:00
|
|
|
|
],
|
2016-09-10 17:36:27 +08:00
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
2012-02-28 05:15:25 +08:00
|
|
|
|
def test_raw_sql(self):
|
|
|
|
|
# Regression test for #17755
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 13, 20, 30)
|
|
|
|
|
event = Event.objects.create(dt=dt)
|
2017-03-17 19:51:48 +08:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
list(
|
|
|
|
|
Event.objects.raw("SELECT * FROM timezones_event WHERE dt = %s", [dt])
|
|
|
|
|
),
|
|
|
|
|
[event],
|
2022-02-04 03:24:19 +08:00
|
|
|
|
)
|
2012-02-28 05:15:25 +08:00
|
|
|
|
|
2015-05-02 21:54:17 +08:00
|
|
|
|
def test_cursor_execute_accepts_naive_datetime(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 13, 20, 30)
|
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
|
cursor.execute("INSERT INTO timezones_event (dt) VALUES (%s)", [dt])
|
|
|
|
|
event = Event.objects.get()
|
|
|
|
|
self.assertEqual(event.dt, dt)
|
|
|
|
|
|
2015-04-12 20:46:24 +08:00
|
|
|
|
def test_cursor_execute_returns_naive_datetime(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 13, 20, 30)
|
|
|
|
|
Event.objects.create(dt=dt)
|
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
|
cursor.execute("SELECT dt FROM timezones_event WHERE dt = %s", [dt])
|
|
|
|
|
self.assertEqual(cursor.fetchall()[0][0], dt)
|
|
|
|
|
|
2012-05-01 16:40:25 +08:00
|
|
|
|
def test_filter_date_field_with_aware_datetime(self):
|
|
|
|
|
# Regression test for #17742
|
|
|
|
|
day = datetime.date(2011, 9, 1)
|
2013-10-19 20:31:38 +08:00
|
|
|
|
AllDayEvent.objects.create(day=day)
|
2012-05-01 16:40:25 +08:00
|
|
|
|
# This is 2011-09-02T01:30:00+03:00 in EAT
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 22, 30, 0, tzinfo=UTC)
|
|
|
|
|
self.assertTrue(AllDayEvent.objects.filter(day__gte=dt).exists())
|
|
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
2012-04-29 22:03:46 +08:00
|
|
|
|
@override_settings(TIME_ZONE="Africa/Nairobi", USE_TZ=True)
|
|
|
|
|
class NewDatabaseTests(TestCase):
|
2018-04-28 05:18:15 +08:00
|
|
|
|
naive_warning = "DateTimeField Event.dt received a naive datetime"
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
2021-07-01 21:19:09 +08:00
|
|
|
|
@skipIfDBFeature("supports_timezones")
|
|
|
|
|
def test_aware_time_unsupported(self):
|
|
|
|
|
t = datetime.time(13, 20, 30, tzinfo=EAT)
|
|
|
|
|
msg = "backend does not support timezone-aware times."
|
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
|
DailyEvent.objects.create(time=t)
|
|
|
|
|
|
2011-12-27 17:28:50 +08:00
|
|
|
|
@requires_tz_support
|
2011-11-18 21:01:06 +08:00
|
|
|
|
def test_naive_datetime(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 13, 20, 30)
|
2018-04-28 05:18:15 +08:00
|
|
|
|
with self.assertWarnsMessage(RuntimeWarning, self.naive_warning):
|
2011-11-20 07:27:20 +08:00
|
|
|
|
Event.objects.create(dt=dt)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
event = Event.objects.get()
|
|
|
|
|
# naive datetimes are interpreted in local time
|
|
|
|
|
self.assertEqual(event.dt, dt.replace(tzinfo=EAT))
|
|
|
|
|
|
2012-01-24 18:00:39 +08:00
|
|
|
|
@requires_tz_support
|
|
|
|
|
def test_datetime_from_date(self):
|
|
|
|
|
dt = datetime.date(2011, 9, 1)
|
2018-04-28 05:18:15 +08:00
|
|
|
|
with self.assertWarnsMessage(RuntimeWarning, self.naive_warning):
|
2012-01-29 04:34:02 +08:00
|
|
|
|
Event.objects.create(dt=dt)
|
2012-01-24 18:00:39 +08:00
|
|
|
|
event = Event.objects.get()
|
|
|
|
|
self.assertEqual(event.dt, datetime.datetime(2011, 9, 1, tzinfo=EAT))
|
|
|
|
|
|
2011-12-27 17:28:50 +08:00
|
|
|
|
@requires_tz_support
|
2011-11-18 21:01:06 +08:00
|
|
|
|
def test_naive_datetime_with_microsecond(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 13, 20, 30, 405060)
|
2018-04-28 05:18:15 +08:00
|
|
|
|
with self.assertWarnsMessage(RuntimeWarning, self.naive_warning):
|
2011-11-20 07:27:20 +08:00
|
|
|
|
Event.objects.create(dt=dt)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
event = Event.objects.get()
|
|
|
|
|
# naive datetimes are interpreted in local time
|
|
|
|
|
self.assertEqual(event.dt, dt.replace(tzinfo=EAT))
|
|
|
|
|
|
|
|
|
|
def test_aware_datetime_in_local_timezone(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT)
|
|
|
|
|
Event.objects.create(dt=dt)
|
|
|
|
|
event = Event.objects.get()
|
|
|
|
|
self.assertEqual(event.dt, dt)
|
|
|
|
|
|
|
|
|
|
def test_aware_datetime_in_local_timezone_with_microsecond(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 13, 20, 30, 405060, tzinfo=EAT)
|
|
|
|
|
Event.objects.create(dt=dt)
|
|
|
|
|
event = Event.objects.get()
|
|
|
|
|
self.assertEqual(event.dt, dt)
|
|
|
|
|
|
|
|
|
|
def test_aware_datetime_in_utc(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC)
|
|
|
|
|
Event.objects.create(dt=dt)
|
|
|
|
|
event = Event.objects.get()
|
|
|
|
|
self.assertEqual(event.dt, dt)
|
|
|
|
|
|
|
|
|
|
def test_aware_datetime_in_other_timezone(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 17, 20, 30, tzinfo=ICT)
|
|
|
|
|
Event.objects.create(dt=dt)
|
|
|
|
|
event = Event.objects.get()
|
|
|
|
|
self.assertEqual(event.dt, dt)
|
|
|
|
|
|
|
|
|
|
def test_auto_now_and_auto_now_add(self):
|
2011-11-20 07:27:20 +08:00
|
|
|
|
now = timezone.now()
|
2011-11-18 21:01:06 +08:00
|
|
|
|
past = now - datetime.timedelta(seconds=2)
|
|
|
|
|
future = now + datetime.timedelta(seconds=2)
|
|
|
|
|
Timestamp.objects.create()
|
|
|
|
|
ts = Timestamp.objects.get()
|
|
|
|
|
self.assertLess(past, ts.created)
|
|
|
|
|
self.assertLess(past, ts.updated)
|
|
|
|
|
self.assertGreater(future, ts.updated)
|
|
|
|
|
self.assertGreater(future, ts.updated)
|
|
|
|
|
|
|
|
|
|
def test_query_filter(self):
|
|
|
|
|
dt1 = datetime.datetime(2011, 9, 1, 12, 20, 30, tzinfo=EAT)
|
|
|
|
|
dt2 = datetime.datetime(2011, 9, 1, 14, 20, 30, tzinfo=EAT)
|
|
|
|
|
Event.objects.create(dt=dt1)
|
|
|
|
|
Event.objects.create(dt=dt2)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__gte=dt1).count(), 2)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__gt=dt1).count(), 1)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__gte=dt2).count(), 1)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__gt=dt2).count(), 0)
|
|
|
|
|
|
2023-01-09 16:03:38 +08:00
|
|
|
|
def test_query_filter_with_timezones(self):
|
|
|
|
|
tz = zoneinfo.ZoneInfo("Europe/Paris")
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 12, 20, 30, tzinfo=tz)
|
|
|
|
|
Event.objects.create(dt=dt)
|
|
|
|
|
next = dt + datetime.timedelta(seconds=3)
|
|
|
|
|
prev = dt - datetime.timedelta(seconds=3)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__exact=dt).count(), 1)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__exact=next).count(), 0)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__in=(prev, next)).count(), 0)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__in=(prev, dt, next)).count(), 1)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__range=(prev, next)).count(), 1)
|
2021-09-09 21:15:44 +08:00
|
|
|
|
|
2019-03-30 05:07:29 +08:00
|
|
|
|
def test_query_convert_timezones(self):
|
|
|
|
|
# Connection timezone is equal to the current timezone, datetime
|
|
|
|
|
# shouldn't be converted.
|
|
|
|
|
with override_database_connection_timezone("Africa/Nairobi"):
|
|
|
|
|
event_datetime = datetime.datetime(2016, 1, 2, 23, 10, 11, 123, tzinfo=EAT)
|
|
|
|
|
event = Event.objects.create(dt=event_datetime)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
Event.objects.filter(dt__date=event_datetime.date()).first(), event
|
|
|
|
|
)
|
|
|
|
|
# Connection timezone is not equal to the current timezone, datetime
|
|
|
|
|
# should be converted (-4h).
|
|
|
|
|
with override_database_connection_timezone("Asia/Bangkok"):
|
|
|
|
|
event_datetime = datetime.datetime(2016, 1, 2, 3, 10, 11, tzinfo=ICT)
|
|
|
|
|
event = Event.objects.create(dt=event_datetime)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
Event.objects.filter(dt__date=datetime.date(2016, 1, 1)).first(), event
|
|
|
|
|
)
|
|
|
|
|
|
2011-12-27 17:28:50 +08:00
|
|
|
|
@requires_tz_support
|
2011-11-20 23:29:39 +08:00
|
|
|
|
def test_query_filter_with_naive_datetime(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 12, 20, 30, tzinfo=EAT)
|
|
|
|
|
Event.objects.create(dt=dt)
|
|
|
|
|
dt = dt.replace(tzinfo=None)
|
2018-04-28 05:18:15 +08:00
|
|
|
|
# naive datetimes are interpreted in local time
|
|
|
|
|
with self.assertWarnsMessage(RuntimeWarning, self.naive_warning):
|
2011-11-20 23:29:39 +08:00
|
|
|
|
self.assertEqual(Event.objects.filter(dt__exact=dt).count(), 1)
|
2018-04-28 05:18:15 +08:00
|
|
|
|
with self.assertWarnsMessage(RuntimeWarning, self.naive_warning):
|
2011-11-20 23:29:39 +08:00
|
|
|
|
self.assertEqual(Event.objects.filter(dt__lte=dt).count(), 1)
|
2018-04-28 05:18:15 +08:00
|
|
|
|
with self.assertWarnsMessage(RuntimeWarning, self.naive_warning):
|
2011-11-20 23:29:39 +08:00
|
|
|
|
self.assertEqual(Event.objects.filter(dt__gt=dt).count(), 0)
|
|
|
|
|
|
2013-02-10 23:15:49 +08:00
|
|
|
|
@skipUnlessDBFeature("has_zoneinfo_database")
|
|
|
|
|
def test_query_datetime_lookups(self):
|
2011-11-18 21:01:06 +08:00
|
|
|
|
Event.objects.create(dt=datetime.datetime(2011, 1, 1, 1, 30, 0, tzinfo=EAT))
|
|
|
|
|
Event.objects.create(dt=datetime.datetime(2011, 1, 1, 4, 30, 0, tzinfo=EAT))
|
2013-02-10 23:15:49 +08:00
|
|
|
|
self.assertEqual(Event.objects.filter(dt__year=2011).count(), 2)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__month=1).count(), 2)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__day=1).count(), 2)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__week_day=7).count(), 2)
|
2019-10-01 06:12:19 +08:00
|
|
|
|
self.assertEqual(Event.objects.filter(dt__iso_week_day=6).count(), 2)
|
2013-02-10 23:15:49 +08:00
|
|
|
|
self.assertEqual(Event.objects.filter(dt__hour=1).count(), 1)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__minute=30).count(), 2)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__second=0).count(), 2)
|
|
|
|
|
|
|
|
|
|
@skipUnlessDBFeature("has_zoneinfo_database")
|
|
|
|
|
def test_query_datetime_lookups_in_other_timezone(self):
|
|
|
|
|
Event.objects.create(dt=datetime.datetime(2011, 1, 1, 1, 30, 0, tzinfo=EAT))
|
|
|
|
|
Event.objects.create(dt=datetime.datetime(2011, 1, 1, 4, 30, 0, tzinfo=EAT))
|
|
|
|
|
with timezone.override(UTC):
|
|
|
|
|
# These two dates fall in the same day in EAT, but in different days,
|
|
|
|
|
# years and months in UTC.
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__year=2011).count(), 1)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__month=1).count(), 1)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__day=1).count(), 1)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__week_day=7).count(), 1)
|
2019-10-01 06:12:19 +08:00
|
|
|
|
self.assertEqual(Event.objects.filter(dt__iso_week_day=6).count(), 1)
|
2013-02-10 23:15:49 +08:00
|
|
|
|
self.assertEqual(Event.objects.filter(dt__hour=22).count(), 1)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__minute=30).count(), 2)
|
|
|
|
|
self.assertEqual(Event.objects.filter(dt__second=0).count(), 2)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
|
|
def test_query_aggregation(self):
|
|
|
|
|
# Only min and max make sense for datetimes.
|
|
|
|
|
Event.objects.create(dt=datetime.datetime(2011, 9, 1, 23, 20, 20, tzinfo=EAT))
|
|
|
|
|
Event.objects.create(dt=datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT))
|
|
|
|
|
Event.objects.create(dt=datetime.datetime(2011, 9, 1, 3, 20, 40, tzinfo=EAT))
|
2022-02-22 17:29:38 +08:00
|
|
|
|
result = Event.objects.aggregate(Min("dt"), Max("dt"))
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
result,
|
|
|
|
|
{
|
|
|
|
|
"dt__min": datetime.datetime(2011, 9, 1, 3, 20, 40, tzinfo=EAT),
|
|
|
|
|
"dt__max": datetime.datetime(2011, 9, 1, 23, 20, 20, tzinfo=EAT),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
2012-02-23 03:40:27 +08:00
|
|
|
|
def test_query_annotation(self):
|
|
|
|
|
# Only min and max make sense for datetimes.
|
|
|
|
|
morning = Session.objects.create(name="morning")
|
|
|
|
|
afternoon = Session.objects.create(name="afternoon")
|
|
|
|
|
SessionEvent.objects.create(
|
|
|
|
|
dt=datetime.datetime(2011, 9, 1, 23, 20, 20, tzinfo=EAT), session=afternoon
|
|
|
|
|
)
|
|
|
|
|
SessionEvent.objects.create(
|
|
|
|
|
dt=datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT), session=afternoon
|
|
|
|
|
)
|
|
|
|
|
SessionEvent.objects.create(
|
|
|
|
|
dt=datetime.datetime(2011, 9, 1, 3, 20, 40, tzinfo=EAT), session=morning
|
|
|
|
|
)
|
|
|
|
|
morning_min_dt = datetime.datetime(2011, 9, 1, 3, 20, 40, tzinfo=EAT)
|
|
|
|
|
afternoon_min_dt = datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT)
|
2022-09-24 18:29:58 +08:00
|
|
|
|
self.assertQuerySetEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Session.objects.annotate(dt=Min("events__dt")).order_by("dt"),
|
|
|
|
|
[morning_min_dt, afternoon_min_dt],
|
2018-03-16 17:54:34 +08:00
|
|
|
|
transform=lambda d: d.dt,
|
|
|
|
|
)
|
2022-09-24 18:29:58 +08:00
|
|
|
|
self.assertQuerySetEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Session.objects.annotate(dt=Min("events__dt")).filter(
|
|
|
|
|
dt__lt=afternoon_min_dt
|
|
|
|
|
),
|
|
|
|
|
[morning_min_dt],
|
2018-03-16 17:54:34 +08:00
|
|
|
|
transform=lambda d: d.dt,
|
|
|
|
|
)
|
2022-09-24 18:29:58 +08:00
|
|
|
|
self.assertQuerySetEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Session.objects.annotate(dt=Min("events__dt")).filter(
|
|
|
|
|
dt__gte=afternoon_min_dt
|
|
|
|
|
),
|
|
|
|
|
[afternoon_min_dt],
|
2018-03-16 17:54:34 +08:00
|
|
|
|
transform=lambda d: d.dt,
|
|
|
|
|
)
|
2012-02-23 03:40:27 +08:00
|
|
|
|
|
2013-02-10 23:15:49 +08:00
|
|
|
|
@skipUnlessDBFeature("has_zoneinfo_database")
|
|
|
|
|
def test_query_datetimes(self):
|
2011-11-18 21:01:06 +08:00
|
|
|
|
Event.objects.create(dt=datetime.datetime(2011, 1, 1, 1, 30, 0, tzinfo=EAT))
|
|
|
|
|
Event.objects.create(dt=datetime.datetime(2011, 1, 1, 4, 30, 0, tzinfo=EAT))
|
2016-09-10 17:36:27 +08:00
|
|
|
|
self.assertSequenceEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Event.objects.datetimes("dt", "year"),
|
2016-09-10 17:36:27 +08:00
|
|
|
|
[datetime.datetime(2011, 1, 1, 0, 0, 0, tzinfo=EAT)],
|
|
|
|
|
)
|
|
|
|
|
self.assertSequenceEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Event.objects.datetimes("dt", "month"),
|
2016-09-10 17:36:27 +08:00
|
|
|
|
[datetime.datetime(2011, 1, 1, 0, 0, 0, tzinfo=EAT)],
|
|
|
|
|
)
|
|
|
|
|
self.assertSequenceEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Event.objects.datetimes("dt", "day"),
|
2016-09-10 17:36:27 +08:00
|
|
|
|
[datetime.datetime(2011, 1, 1, 0, 0, 0, tzinfo=EAT)],
|
|
|
|
|
)
|
|
|
|
|
self.assertSequenceEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Event.objects.datetimes("dt", "hour"),
|
|
|
|
|
[
|
|
|
|
|
datetime.datetime(2011, 1, 1, 1, 0, 0, tzinfo=EAT),
|
2016-09-10 17:36:27 +08:00
|
|
|
|
datetime.datetime(2011, 1, 1, 4, 0, 0, tzinfo=EAT),
|
2022-02-04 03:24:19 +08:00
|
|
|
|
],
|
2016-09-10 17:36:27 +08:00
|
|
|
|
)
|
|
|
|
|
self.assertSequenceEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Event.objects.datetimes("dt", "minute"),
|
|
|
|
|
[
|
|
|
|
|
datetime.datetime(2011, 1, 1, 1, 30, 0, tzinfo=EAT),
|
2016-09-10 17:36:27 +08:00
|
|
|
|
datetime.datetime(2011, 1, 1, 4, 30, 0, tzinfo=EAT),
|
2022-02-04 03:24:19 +08:00
|
|
|
|
],
|
2016-09-10 17:36:27 +08:00
|
|
|
|
)
|
|
|
|
|
self.assertSequenceEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Event.objects.datetimes("dt", "second"),
|
|
|
|
|
[
|
|
|
|
|
datetime.datetime(2011, 1, 1, 1, 30, 0, tzinfo=EAT),
|
2016-09-10 17:36:27 +08:00
|
|
|
|
datetime.datetime(2011, 1, 1, 4, 30, 0, tzinfo=EAT),
|
2022-02-04 03:24:19 +08:00
|
|
|
|
],
|
2016-09-10 17:36:27 +08:00
|
|
|
|
)
|
2013-02-10 23:15:49 +08:00
|
|
|
|
|
|
|
|
|
@skipUnlessDBFeature("has_zoneinfo_database")
|
|
|
|
|
def test_query_datetimes_in_other_timezone(self):
|
|
|
|
|
Event.objects.create(dt=datetime.datetime(2011, 1, 1, 1, 30, 0, tzinfo=EAT))
|
|
|
|
|
Event.objects.create(dt=datetime.datetime(2011, 1, 1, 4, 30, 0, tzinfo=EAT))
|
|
|
|
|
with timezone.override(UTC):
|
2016-09-10 17:36:27 +08:00
|
|
|
|
self.assertSequenceEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Event.objects.datetimes("dt", "year"),
|
|
|
|
|
[
|
|
|
|
|
datetime.datetime(2010, 1, 1, 0, 0, 0, tzinfo=UTC),
|
2016-09-10 17:36:27 +08:00
|
|
|
|
datetime.datetime(2011, 1, 1, 0, 0, 0, tzinfo=UTC),
|
2022-02-04 03:24:19 +08:00
|
|
|
|
],
|
2016-09-10 17:36:27 +08:00
|
|
|
|
)
|
|
|
|
|
self.assertSequenceEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Event.objects.datetimes("dt", "month"),
|
|
|
|
|
[
|
|
|
|
|
datetime.datetime(2010, 12, 1, 0, 0, 0, tzinfo=UTC),
|
2016-09-10 17:36:27 +08:00
|
|
|
|
datetime.datetime(2011, 1, 1, 0, 0, 0, tzinfo=UTC),
|
2022-02-04 03:24:19 +08:00
|
|
|
|
],
|
2016-09-10 17:36:27 +08:00
|
|
|
|
)
|
|
|
|
|
self.assertSequenceEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Event.objects.datetimes("dt", "day"),
|
|
|
|
|
[
|
|
|
|
|
datetime.datetime(2010, 12, 31, 0, 0, 0, tzinfo=UTC),
|
2016-09-10 17:36:27 +08:00
|
|
|
|
datetime.datetime(2011, 1, 1, 0, 0, 0, tzinfo=UTC),
|
2022-02-04 03:24:19 +08:00
|
|
|
|
],
|
2016-09-10 17:36:27 +08:00
|
|
|
|
)
|
|
|
|
|
self.assertSequenceEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Event.objects.datetimes("dt", "hour"),
|
|
|
|
|
[
|
|
|
|
|
datetime.datetime(2010, 12, 31, 22, 0, 0, tzinfo=UTC),
|
2016-09-10 17:36:27 +08:00
|
|
|
|
datetime.datetime(2011, 1, 1, 1, 0, 0, tzinfo=UTC),
|
2022-02-04 03:24:19 +08:00
|
|
|
|
],
|
2016-09-10 17:36:27 +08:00
|
|
|
|
)
|
|
|
|
|
self.assertSequenceEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Event.objects.datetimes("dt", "minute"),
|
|
|
|
|
[
|
|
|
|
|
datetime.datetime(2010, 12, 31, 22, 30, 0, tzinfo=UTC),
|
2016-09-10 17:36:27 +08:00
|
|
|
|
datetime.datetime(2011, 1, 1, 1, 30, 0, tzinfo=UTC),
|
2022-02-04 03:24:19 +08:00
|
|
|
|
],
|
2016-09-10 17:36:27 +08:00
|
|
|
|
)
|
|
|
|
|
self.assertSequenceEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
|
Event.objects.datetimes("dt", "second"),
|
|
|
|
|
[
|
|
|
|
|
datetime.datetime(2010, 12, 31, 22, 30, 0, tzinfo=UTC),
|
2016-09-10 17:36:27 +08:00
|
|
|
|
datetime.datetime(2011, 1, 1, 1, 30, 0, tzinfo=UTC),
|
2022-02-04 03:24:19 +08:00
|
|
|
|
],
|
2016-09-10 17:36:27 +08:00
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
2012-02-28 05:15:25 +08:00
|
|
|
|
def test_raw_sql(self):
|
|
|
|
|
# Regression test for #17755
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT)
|
|
|
|
|
event = Event.objects.create(dt=dt)
|
2016-09-10 17:36:27 +08:00
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
|
list(
|
|
|
|
|
Event.objects.raw("SELECT * FROM timezones_event WHERE dt = %s", [dt])
|
|
|
|
|
),
|
|
|
|
|
[event],
|
2022-02-04 03:24:19 +08:00
|
|
|
|
)
|
2012-02-28 05:15:25 +08:00
|
|
|
|
|
2015-05-02 21:54:17 +08:00
|
|
|
|
@skipUnlessDBFeature("supports_timezones")
|
|
|
|
|
def test_cursor_execute_accepts_aware_datetime(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT)
|
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
|
cursor.execute("INSERT INTO timezones_event (dt) VALUES (%s)", [dt])
|
|
|
|
|
event = Event.objects.get()
|
|
|
|
|
self.assertEqual(event.dt, dt)
|
|
|
|
|
|
|
|
|
|
@skipIfDBFeature("supports_timezones")
|
|
|
|
|
def test_cursor_execute_accepts_naive_datetime(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT)
|
2022-03-23 19:15:36 +08:00
|
|
|
|
utc_naive_dt = timezone.make_naive(dt, datetime.timezone.utc)
|
2015-05-02 21:54:17 +08:00
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
|
cursor.execute(
|
|
|
|
|
"INSERT INTO timezones_event (dt) VALUES (%s)", [utc_naive_dt]
|
|
|
|
|
)
|
|
|
|
|
event = Event.objects.get()
|
|
|
|
|
self.assertEqual(event.dt, dt)
|
|
|
|
|
|
2015-04-12 20:46:24 +08:00
|
|
|
|
@skipUnlessDBFeature("supports_timezones")
|
|
|
|
|
def test_cursor_execute_returns_aware_datetime(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT)
|
|
|
|
|
Event.objects.create(dt=dt)
|
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
|
cursor.execute("SELECT dt FROM timezones_event WHERE dt = %s", [dt])
|
|
|
|
|
self.assertEqual(cursor.fetchall()[0][0], dt)
|
|
|
|
|
|
|
|
|
|
@skipIfDBFeature("supports_timezones")
|
|
|
|
|
def test_cursor_execute_returns_naive_datetime(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT)
|
2022-03-23 19:15:36 +08:00
|
|
|
|
utc_naive_dt = timezone.make_naive(dt, datetime.timezone.utc)
|
2015-04-12 20:46:24 +08:00
|
|
|
|
Event.objects.create(dt=dt)
|
|
|
|
|
with connection.cursor() as cursor:
|
2015-05-02 21:54:17 +08:00
|
|
|
|
cursor.execute(
|
|
|
|
|
"SELECT dt FROM timezones_event WHERE dt = %s", [utc_naive_dt]
|
|
|
|
|
)
|
2015-04-12 20:46:24 +08:00
|
|
|
|
self.assertEqual(cursor.fetchall()[0][0], utc_naive_dt)
|
|
|
|
|
|
2019-07-20 21:38:43 +08:00
|
|
|
|
@skipUnlessDBFeature("supports_timezones")
|
|
|
|
|
def test_cursor_explicit_time_zone(self):
|
|
|
|
|
with override_database_connection_timezone("Europe/Paris"):
|
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
|
cursor.execute("SELECT CURRENT_TIMESTAMP")
|
|
|
|
|
now = cursor.fetchone()[0]
|
2021-01-19 18:16:01 +08:00
|
|
|
|
self.assertEqual(str(now.tzinfo), "Europe/Paris")
|
2019-07-20 21:38:43 +08:00
|
|
|
|
|
2012-08-12 05:32:24 +08:00
|
|
|
|
@requires_tz_support
|
2012-05-01 16:40:25 +08:00
|
|
|
|
def test_filter_date_field_with_aware_datetime(self):
|
|
|
|
|
# Regression test for #17742
|
|
|
|
|
day = datetime.date(2011, 9, 1)
|
2013-10-19 20:31:38 +08:00
|
|
|
|
AllDayEvent.objects.create(day=day)
|
2012-05-01 16:40:25 +08:00
|
|
|
|
# This is 2011-09-02T01:30:00+03:00 in EAT
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 22, 30, 0, tzinfo=UTC)
|
|
|
|
|
self.assertFalse(AllDayEvent.objects.filter(day__gte=dt).exists())
|
|
|
|
|
|
2011-11-25 17:25:43 +08:00
|
|
|
|
def test_null_datetime(self):
|
2012-05-01 16:40:25 +08:00
|
|
|
|
# Regression test for #17294
|
2011-11-25 17:25:43 +08:00
|
|
|
|
e = MaybeEvent.objects.create()
|
2016-06-17 02:19:18 +08:00
|
|
|
|
self.assertIsNone(e.dt)
|
2011-11-25 17:25:43 +08:00
|
|
|
|
|
2016-11-30 01:23:44 +08:00
|
|
|
|
def test_update_with_timedelta(self):
|
|
|
|
|
initial_dt = timezone.now().replace(microsecond=0)
|
|
|
|
|
event = Event.objects.create(dt=initial_dt)
|
|
|
|
|
Event.objects.update(dt=F("dt") + timedelta(hours=2))
|
|
|
|
|
event.refresh_from_db()
|
|
|
|
|
self.assertEqual(event.dt, initial_dt + timedelta(hours=2))
|
|
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
2015-05-03 03:56:53 +08:00
|
|
|
|
@override_settings(TIME_ZONE="Africa/Nairobi", USE_TZ=True)
|
|
|
|
|
class ForcedTimeZoneDatabaseTests(TransactionTestCase):
|
|
|
|
|
"""
|
|
|
|
|
Test the TIME_ZONE database configuration parameter.
|
|
|
|
|
|
|
|
|
|
Since this involves reading and writing to the same database through two
|
|
|
|
|
connections, this is a TransactionTestCase.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
available_apps = ["timezones"]
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2015-05-03 15:06:06 +08:00
|
|
|
|
# @skipIfDBFeature and @skipUnlessDBFeature cannot be chained. The
|
|
|
|
|
# outermost takes precedence. Handle skipping manually instead.
|
|
|
|
|
if connection.features.supports_timezones:
|
|
|
|
|
raise SkipTest("Database has feature(s) supports_timezones")
|
|
|
|
|
if not connection.features.test_db_allows_multiple_connections:
|
|
|
|
|
raise SkipTest(
|
|
|
|
|
"Database doesn't support feature(s): "
|
|
|
|
|
"test_db_allows_multiple_connections"
|
|
|
|
|
)
|
|
|
|
|
|
2017-01-21 21:13:44 +08:00
|
|
|
|
super().setUpClass()
|
2015-05-03 03:56:53 +08:00
|
|
|
|
|
|
|
|
|
def test_read_datetime(self):
|
|
|
|
|
fake_dt = datetime.datetime(2011, 9, 1, 17, 20, 30, tzinfo=UTC)
|
|
|
|
|
Event.objects.create(dt=fake_dt)
|
|
|
|
|
|
2019-03-30 05:07:29 +08:00
|
|
|
|
with override_database_connection_timezone("Asia/Bangkok"):
|
2015-06-05 19:49:32 +08:00
|
|
|
|
event = Event.objects.get()
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC)
|
2015-05-03 03:56:53 +08:00
|
|
|
|
self.assertEqual(event.dt, dt)
|
|
|
|
|
|
|
|
|
|
def test_write_datetime(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC)
|
2019-03-30 05:07:29 +08:00
|
|
|
|
with override_database_connection_timezone("Asia/Bangkok"):
|
2015-06-05 19:49:32 +08:00
|
|
|
|
Event.objects.create(dt=dt)
|
2015-05-03 03:56:53 +08:00
|
|
|
|
|
|
|
|
|
event = Event.objects.get()
|
|
|
|
|
fake_dt = datetime.datetime(2011, 9, 1, 17, 20, 30, tzinfo=UTC)
|
|
|
|
|
self.assertEqual(event.dt, fake_dt)
|
|
|
|
|
|
|
|
|
|
|
2012-04-29 22:03:46 +08:00
|
|
|
|
@override_settings(TIME_ZONE="Africa/Nairobi")
|
2015-04-18 05:38:20 +08:00
|
|
|
|
class SerializationTests(SimpleTestCase):
|
2011-11-18 21:01:06 +08:00
|
|
|
|
# Backend-specific notes:
|
|
|
|
|
# - JSON supports only milliseconds, microseconds will be truncated.
|
2020-01-07 16:54:22 +08:00
|
|
|
|
# - PyYAML dumps the UTC offset correctly for timezone-aware datetimes.
|
|
|
|
|
# When PyYAML < 5.3 loads this representation, it subtracts the offset
|
|
|
|
|
# and returns a naive datetime object in UTC. PyYAML 5.3+ loads timezones
|
|
|
|
|
# correctly.
|
2011-11-18 21:01:06 +08:00
|
|
|
|
# Tests are adapted to take these quirks into account.
|
|
|
|
|
|
2012-10-26 05:14:17 +08:00
|
|
|
|
def assert_python_contains_datetime(self, objects, dt):
|
|
|
|
|
self.assertEqual(objects[0]["fields"]["dt"], dt)
|
|
|
|
|
|
|
|
|
|
def assert_json_contains_datetime(self, json, dt):
|
|
|
|
|
self.assertIn('"fields": {"dt": "%s"}' % dt, json)
|
|
|
|
|
|
|
|
|
|
def assert_xml_contains_datetime(self, xml, dt):
|
|
|
|
|
field = parseString(xml).getElementsByTagName("field")[0]
|
|
|
|
|
self.assertXMLEqual(field.childNodes[0].wholeText, dt)
|
|
|
|
|
|
|
|
|
|
def assert_yaml_contains_datetime(self, yaml, dt):
|
2013-01-12 00:49:55 +08:00
|
|
|
|
# Depending on the yaml dumper, '!timestamp' might be absent
|
2016-12-08 06:42:31 +08:00
|
|
|
|
self.assertRegex(yaml, r"\n fields: {dt: !(!timestamp)? '%s'}" % re.escape(dt))
|
2012-10-26 05:14:17 +08:00
|
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
|
def test_naive_datetime(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 13, 20, 30)
|
|
|
|
|
|
|
|
|
|
data = serializers.serialize("python", [Event(dt=dt)])
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_python_contains_datetime(data, dt)
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("python", data)).object
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(obj.dt, dt)
|
|
|
|
|
|
|
|
|
|
data = serializers.serialize("json", [Event(dt=dt)])
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_json_contains_datetime(data, "2011-09-01T13:20:30")
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("json", data)).object
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(obj.dt, dt)
|
|
|
|
|
|
|
|
|
|
data = serializers.serialize("xml", [Event(dt=dt)])
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_xml_contains_datetime(data, "2011-09-01T13:20:30")
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("xml", data)).object
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(obj.dt, dt)
|
|
|
|
|
|
2013-09-07 06:22:04 +08:00
|
|
|
|
if not isinstance(
|
|
|
|
|
serializers.get_serializer("yaml"), serializers.BadSerializer
|
|
|
|
|
):
|
2019-03-14 03:06:47 +08:00
|
|
|
|
data = serializers.serialize(
|
|
|
|
|
"yaml", [Event(dt=dt)], default_flow_style=None
|
|
|
|
|
)
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_yaml_contains_datetime(data, "2011-09-01 13:20:30")
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("yaml", data)).object
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(obj.dt, dt)
|
|
|
|
|
|
|
|
|
|
def test_naive_datetime_with_microsecond(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 13, 20, 30, 405060)
|
|
|
|
|
|
|
|
|
|
data = serializers.serialize("python", [Event(dt=dt)])
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_python_contains_datetime(data, dt)
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("python", data)).object
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(obj.dt, dt)
|
|
|
|
|
|
|
|
|
|
data = serializers.serialize("json", [Event(dt=dt)])
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_json_contains_datetime(data, "2011-09-01T13:20:30.405")
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("json", data)).object
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(obj.dt, dt.replace(microsecond=405000))
|
|
|
|
|
|
|
|
|
|
data = serializers.serialize("xml", [Event(dt=dt)])
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_xml_contains_datetime(data, "2011-09-01T13:20:30.405060")
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("xml", data)).object
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(obj.dt, dt)
|
|
|
|
|
|
2013-09-07 06:22:04 +08:00
|
|
|
|
if not isinstance(
|
|
|
|
|
serializers.get_serializer("yaml"), serializers.BadSerializer
|
|
|
|
|
):
|
2019-03-14 03:06:47 +08:00
|
|
|
|
data = serializers.serialize(
|
|
|
|
|
"yaml", [Event(dt=dt)], default_flow_style=None
|
|
|
|
|
)
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_yaml_contains_datetime(data, "2011-09-01 13:20:30.405060")
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("yaml", data)).object
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(obj.dt, dt)
|
|
|
|
|
|
|
|
|
|
def test_aware_datetime_with_microsecond(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 17, 20, 30, 405060, tzinfo=ICT)
|
|
|
|
|
|
|
|
|
|
data = serializers.serialize("python", [Event(dt=dt)])
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_python_contains_datetime(data, dt)
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("python", data)).object
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(obj.dt, dt)
|
|
|
|
|
|
|
|
|
|
data = serializers.serialize("json", [Event(dt=dt)])
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_json_contains_datetime(data, "2011-09-01T17:20:30.405+07:00")
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("json", data)).object
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(obj.dt, dt.replace(microsecond=405000))
|
|
|
|
|
|
|
|
|
|
data = serializers.serialize("xml", [Event(dt=dt)])
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_xml_contains_datetime(data, "2011-09-01T17:20:30.405060+07:00")
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("xml", data)).object
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(obj.dt, dt)
|
|
|
|
|
|
2013-09-07 06:22:04 +08:00
|
|
|
|
if not isinstance(
|
|
|
|
|
serializers.get_serializer("yaml"), serializers.BadSerializer
|
|
|
|
|
):
|
2019-03-14 03:06:47 +08:00
|
|
|
|
data = serializers.serialize(
|
|
|
|
|
"yaml", [Event(dt=dt)], default_flow_style=None
|
|
|
|
|
)
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_yaml_contains_datetime(data, "2011-09-01 17:20:30.405060+07:00")
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("yaml", data)).object
|
2020-01-07 16:54:22 +08:00
|
|
|
|
if HAS_YAML and yaml.__version__ < "5.3":
|
|
|
|
|
self.assertEqual(obj.dt.replace(tzinfo=UTC), dt)
|
|
|
|
|
else:
|
|
|
|
|
self.assertEqual(obj.dt, dt)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
|
|
def test_aware_datetime_in_utc(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC)
|
|
|
|
|
|
|
|
|
|
data = serializers.serialize("python", [Event(dt=dt)])
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_python_contains_datetime(data, dt)
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("python", data)).object
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(obj.dt, dt)
|
|
|
|
|
|
|
|
|
|
data = serializers.serialize("json", [Event(dt=dt)])
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_json_contains_datetime(data, "2011-09-01T10:20:30Z")
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("json", data)).object
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(obj.dt, dt)
|
|
|
|
|
|
|
|
|
|
data = serializers.serialize("xml", [Event(dt=dt)])
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_xml_contains_datetime(data, "2011-09-01T10:20:30+00:00")
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("xml", data)).object
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(obj.dt, dt)
|
|
|
|
|
|
2013-09-07 06:22:04 +08:00
|
|
|
|
if not isinstance(
|
|
|
|
|
serializers.get_serializer("yaml"), serializers.BadSerializer
|
|
|
|
|
):
|
2019-03-14 03:06:47 +08:00
|
|
|
|
data = serializers.serialize(
|
|
|
|
|
"yaml", [Event(dt=dt)], default_flow_style=None
|
|
|
|
|
)
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_yaml_contains_datetime(data, "2011-09-01 10:20:30+00:00")
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("yaml", data)).object
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(obj.dt.replace(tzinfo=UTC), dt)
|
|
|
|
|
|
|
|
|
|
def test_aware_datetime_in_local_timezone(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT)
|
|
|
|
|
|
|
|
|
|
data = serializers.serialize("python", [Event(dt=dt)])
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_python_contains_datetime(data, dt)
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("python", data)).object
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(obj.dt, dt)
|
|
|
|
|
|
|
|
|
|
data = serializers.serialize("json", [Event(dt=dt)])
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_json_contains_datetime(data, "2011-09-01T13:20:30+03:00")
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("json", data)).object
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(obj.dt, dt)
|
|
|
|
|
|
|
|
|
|
data = serializers.serialize("xml", [Event(dt=dt)])
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_xml_contains_datetime(data, "2011-09-01T13:20:30+03:00")
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("xml", data)).object
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(obj.dt, dt)
|
|
|
|
|
|
2013-09-07 06:22:04 +08:00
|
|
|
|
if not isinstance(
|
|
|
|
|
serializers.get_serializer("yaml"), serializers.BadSerializer
|
|
|
|
|
):
|
2019-03-14 03:06:47 +08:00
|
|
|
|
data = serializers.serialize(
|
|
|
|
|
"yaml", [Event(dt=dt)], default_flow_style=None
|
|
|
|
|
)
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_yaml_contains_datetime(data, "2011-09-01 13:20:30+03:00")
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("yaml", data)).object
|
2020-01-07 16:54:22 +08:00
|
|
|
|
if HAS_YAML and yaml.__version__ < "5.3":
|
|
|
|
|
self.assertEqual(obj.dt.replace(tzinfo=UTC), dt)
|
|
|
|
|
else:
|
|
|
|
|
self.assertEqual(obj.dt, dt)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
|
|
def test_aware_datetime_in_other_timezone(self):
|
|
|
|
|
dt = datetime.datetime(2011, 9, 1, 17, 20, 30, tzinfo=ICT)
|
|
|
|
|
|
|
|
|
|
data = serializers.serialize("python", [Event(dt=dt)])
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_python_contains_datetime(data, dt)
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("python", data)).object
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(obj.dt, dt)
|
|
|
|
|
|
|
|
|
|
data = serializers.serialize("json", [Event(dt=dt)])
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_json_contains_datetime(data, "2011-09-01T17:20:30+07:00")
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("json", data)).object
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(obj.dt, dt)
|
|
|
|
|
|
|
|
|
|
data = serializers.serialize("xml", [Event(dt=dt)])
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_xml_contains_datetime(data, "2011-09-01T17:20:30+07:00")
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("xml", data)).object
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(obj.dt, dt)
|
|
|
|
|
|
2013-09-07 06:22:04 +08:00
|
|
|
|
if not isinstance(
|
|
|
|
|
serializers.get_serializer("yaml"), serializers.BadSerializer
|
|
|
|
|
):
|
2019-03-14 03:06:47 +08:00
|
|
|
|
data = serializers.serialize(
|
|
|
|
|
"yaml", [Event(dt=dt)], default_flow_style=None
|
|
|
|
|
)
|
2012-10-26 05:14:17 +08:00
|
|
|
|
self.assert_yaml_contains_datetime(data, "2011-09-01 17:20:30+07:00")
|
2012-05-11 02:14:04 +08:00
|
|
|
|
obj = next(serializers.deserialize("yaml", data)).object
|
2020-01-07 16:54:22 +08:00
|
|
|
|
if HAS_YAML and yaml.__version__ < "5.3":
|
|
|
|
|
self.assertEqual(obj.dt.replace(tzinfo=UTC), dt)
|
|
|
|
|
else:
|
|
|
|
|
self.assertEqual(obj.dt, dt)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
2012-04-29 22:03:46 +08:00
|
|
|
|
|
2023-01-06 21:46:33 +08:00
|
|
|
|
@translation.override(None)
|
|
|
|
|
@override_settings(DATETIME_FORMAT="c", TIME_ZONE="Africa/Nairobi", USE_TZ=True)
|
2016-06-06 11:47:17 +08:00
|
|
|
|
class TemplateTests(SimpleTestCase):
|
2011-12-27 17:28:50 +08:00
|
|
|
|
@requires_tz_support
|
2011-11-18 21:01:06 +08:00
|
|
|
|
def test_localtime_templatetag_and_filters(self):
|
|
|
|
|
"""
|
|
|
|
|
Test the {% localtime %} templatetag and related filters.
|
|
|
|
|
"""
|
|
|
|
|
datetimes = {
|
|
|
|
|
"utc": datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC),
|
|
|
|
|
"eat": datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT),
|
|
|
|
|
"ict": datetime.datetime(2011, 9, 1, 17, 20, 30, tzinfo=ICT),
|
|
|
|
|
"naive": datetime.datetime(2011, 9, 1, 13, 20, 30),
|
|
|
|
|
}
|
|
|
|
|
templates = {
|
2011-11-18 23:00:08 +08:00
|
|
|
|
"notag": Template(
|
|
|
|
|
"{% load tz %}"
|
|
|
|
|
"{{ dt }}|{{ dt|localtime }}|{{ dt|utc }}|{{ dt|timezone:ICT }}"
|
|
|
|
|
),
|
2015-09-12 07:33:12 +08:00
|
|
|
|
"noarg": Template(
|
|
|
|
|
"{% load tz %}{% localtime %}{{ dt }}|{{ dt|localtime }}|"
|
|
|
|
|
"{{ dt|utc }}|{{ dt|timezone:ICT }}{% endlocaltime %}"
|
|
|
|
|
),
|
|
|
|
|
"on": Template(
|
|
|
|
|
"{% load tz %}{% localtime on %}{{ dt }}|{{ dt|localtime }}|"
|
|
|
|
|
"{{ dt|utc }}|{{ dt|timezone:ICT }}{% endlocaltime %}"
|
|
|
|
|
),
|
|
|
|
|
"off": Template(
|
|
|
|
|
"{% load tz %}{% localtime off %}{{ dt }}|{{ dt|localtime }}|"
|
|
|
|
|
"{{ dt|utc }}|{{ dt|timezone:ICT }}{% endlocaltime %}"
|
|
|
|
|
),
|
2011-11-18 21:01:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Transform a list of keys in 'datetimes' to the expected template
|
|
|
|
|
# output. This makes the definition of 'results' more readable.
|
|
|
|
|
def t(*result):
|
|
|
|
|
return "|".join(datetimes[key].isoformat() for key in result)
|
|
|
|
|
|
|
|
|
|
# Results for USE_TZ = True
|
|
|
|
|
|
|
|
|
|
results = {
|
|
|
|
|
"utc": {
|
|
|
|
|
"notag": t("eat", "eat", "utc", "ict"),
|
|
|
|
|
"noarg": t("eat", "eat", "utc", "ict"),
|
2013-11-03 03:37:48 +08:00
|
|
|
|
"on": t("eat", "eat", "utc", "ict"),
|
|
|
|
|
"off": t("utc", "eat", "utc", "ict"),
|
2011-11-18 21:01:06 +08:00
|
|
|
|
},
|
|
|
|
|
"eat": {
|
|
|
|
|
"notag": t("eat", "eat", "utc", "ict"),
|
|
|
|
|
"noarg": t("eat", "eat", "utc", "ict"),
|
2013-11-03 03:37:48 +08:00
|
|
|
|
"on": t("eat", "eat", "utc", "ict"),
|
|
|
|
|
"off": t("eat", "eat", "utc", "ict"),
|
2011-11-18 21:01:06 +08:00
|
|
|
|
},
|
|
|
|
|
"ict": {
|
|
|
|
|
"notag": t("eat", "eat", "utc", "ict"),
|
|
|
|
|
"noarg": t("eat", "eat", "utc", "ict"),
|
2013-11-03 03:37:48 +08:00
|
|
|
|
"on": t("eat", "eat", "utc", "ict"),
|
|
|
|
|
"off": t("ict", "eat", "utc", "ict"),
|
2022-02-04 03:24:19 +08:00
|
|
|
|
},
|
|
|
|
|
"naive": {
|
2011-11-18 21:01:06 +08:00
|
|
|
|
"notag": t("naive", "eat", "utc", "ict"),
|
|
|
|
|
"noarg": t("naive", "eat", "utc", "ict"),
|
2013-11-03 03:37:48 +08:00
|
|
|
|
"on": t("naive", "eat", "utc", "ict"),
|
|
|
|
|
"off": t("naive", "eat", "utc", "ict"),
|
2011-11-18 21:01:06 +08:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-07 19:11:46 +08:00
|
|
|
|
for k1, dt in datetimes.items():
|
|
|
|
|
for k2, tpl in templates.items():
|
2011-11-18 21:01:06 +08:00
|
|
|
|
ctx = Context({"dt": dt, "ICT": ICT})
|
|
|
|
|
actual = tpl.render(ctx)
|
|
|
|
|
expected = results[k1][k2]
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
actual, expected, "%s / %s: %r != %r" % (k1, k2, actual, expected)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
|
|
# Changes for USE_TZ = False
|
|
|
|
|
|
|
|
|
|
results["utc"]["notag"] = t("utc", "eat", "utc", "ict")
|
|
|
|
|
results["ict"]["notag"] = t("ict", "eat", "utc", "ict")
|
|
|
|
|
|
|
|
|
|
with self.settings(USE_TZ=False):
|
2017-01-07 19:11:46 +08:00
|
|
|
|
for k1, dt in datetimes.items():
|
|
|
|
|
for k2, tpl in templates.items():
|
2011-11-18 21:01:06 +08:00
|
|
|
|
ctx = Context({"dt": dt, "ICT": ICT})
|
|
|
|
|
actual = tpl.render(ctx)
|
|
|
|
|
expected = results[k1][k2]
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
actual,
|
|
|
|
|
expected,
|
|
|
|
|
"%s / %s: %r != %r" % (k1, k2, actual, expected),
|
|
|
|
|
)
|
|
|
|
|
|
2021-01-19 18:16:01 +08:00
|
|
|
|
def test_localtime_filters_with_iana(self):
|
2011-11-18 21:01:06 +08:00
|
|
|
|
"""
|
2021-01-19 18:16:01 +08:00
|
|
|
|
Test the |localtime, |utc, and |timezone filters with iana zones.
|
2011-11-18 21:01:06 +08:00
|
|
|
|
"""
|
2021-01-19 18:16:01 +08:00
|
|
|
|
# Use an IANA timezone as local time
|
2011-11-18 23:00:08 +08:00
|
|
|
|
tpl = Template("{% load tz %}{{ dt|localtime }}|{{ dt|utc }}")
|
2011-11-18 21:01:06 +08:00
|
|
|
|
ctx = Context({"dt": datetime.datetime(2011, 9, 1, 12, 20, 30)})
|
|
|
|
|
|
|
|
|
|
with self.settings(TIME_ZONE="Europe/Paris"):
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
tpl.render(ctx), "2011-09-01T12:20:30+02:00|2011-09-01T10:20:30+00:00"
|
|
|
|
|
)
|
|
|
|
|
|
2021-01-19 18:16:01 +08:00
|
|
|
|
# Use an IANA timezone as argument
|
2023-01-09 16:03:38 +08:00
|
|
|
|
tz = zoneinfo.ZoneInfo("Europe/Paris")
|
|
|
|
|
tpl = Template("{% load tz %}{{ dt|timezone:tz }}")
|
|
|
|
|
ctx = Context(
|
|
|
|
|
{
|
|
|
|
|
"dt": datetime.datetime(2011, 9, 1, 13, 20, 30),
|
|
|
|
|
"tz": tz,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(tpl.render(ctx), "2011-09-01T12:20:30+02:00")
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
|
|
def test_localtime_templatetag_invalid_argument(self):
|
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
|
|
|
|
Template("{% load tz %}{% localtime foo %}{% endlocaltime %}").render()
|
|
|
|
|
|
|
|
|
|
def test_localtime_filters_do_not_raise_exceptions(self):
|
|
|
|
|
"""
|
2011-11-18 23:00:08 +08:00
|
|
|
|
Test the |localtime, |utc, and |timezone filters on bad inputs.
|
2011-11-18 21:01:06 +08:00
|
|
|
|
"""
|
2011-11-18 23:00:08 +08:00
|
|
|
|
tpl = Template(
|
|
|
|
|
"{% load tz %}{{ dt }}|{{ dt|localtime }}|{{ dt|utc }}|{{ dt|timezone:tz }}"
|
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
with self.settings(USE_TZ=True):
|
|
|
|
|
# bad datetime value
|
|
|
|
|
ctx = Context({"dt": None, "tz": ICT})
|
|
|
|
|
self.assertEqual(tpl.render(ctx), "None|||")
|
|
|
|
|
ctx = Context({"dt": "not a date", "tz": ICT})
|
|
|
|
|
self.assertEqual(tpl.render(ctx), "not a date|||")
|
|
|
|
|
# bad timezone value
|
2011-11-18 23:00:08 +08:00
|
|
|
|
tpl = Template("{% load tz %}{{ dt|timezone:tz }}")
|
2011-11-18 21:01:06 +08:00
|
|
|
|
ctx = Context({"dt": datetime.datetime(2011, 9, 1, 13, 20, 30), "tz": None})
|
|
|
|
|
self.assertEqual(tpl.render(ctx), "")
|
|
|
|
|
ctx = Context(
|
|
|
|
|
{"dt": datetime.datetime(2011, 9, 1, 13, 20, 30), "tz": "not a tz"}
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(tpl.render(ctx), "")
|
|
|
|
|
|
2011-12-27 17:28:50 +08:00
|
|
|
|
@requires_tz_support
|
2011-11-18 21:01:06 +08:00
|
|
|
|
def test_timezone_templatetag(self):
|
|
|
|
|
"""
|
|
|
|
|
Test the {% timezone %} templatetag.
|
|
|
|
|
"""
|
2013-12-13 04:23:24 +08:00
|
|
|
|
tpl = Template(
|
|
|
|
|
"{% load tz %}"
|
|
|
|
|
"{{ dt }}|"
|
|
|
|
|
"{% timezone tz1 %}"
|
|
|
|
|
"{{ dt }}|"
|
|
|
|
|
"{% timezone tz2 %}"
|
|
|
|
|
"{{ dt }}"
|
|
|
|
|
"{% endtimezone %}"
|
|
|
|
|
"{% endtimezone %}"
|
|
|
|
|
)
|
2019-01-03 07:18:19 +08:00
|
|
|
|
ctx = Context(
|
|
|
|
|
{
|
|
|
|
|
"dt": datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC),
|
|
|
|
|
"tz1": ICT,
|
|
|
|
|
"tz2": None,
|
|
|
|
|
}
|
|
|
|
|
)
|
2015-09-12 07:33:12 +08:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
tpl.render(ctx),
|
|
|
|
|
"2011-09-01T13:20:30+03:00|2011-09-01T17:20:30+07:00|"
|
|
|
|
|
"2011-09-01T13:20:30+03:00",
|
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
2021-01-19 18:16:01 +08:00
|
|
|
|
def test_timezone_templatetag_with_iana(self):
|
2011-11-18 21:01:06 +08:00
|
|
|
|
"""
|
2021-01-19 18:16:01 +08:00
|
|
|
|
Test the {% timezone %} templatetag with IANA time zone providers.
|
2011-11-18 21:01:06 +08:00
|
|
|
|
"""
|
|
|
|
|
tpl = Template("{% load tz %}{% timezone tz %}{{ dt }}{% endtimezone %}")
|
|
|
|
|
|
2021-01-19 18:16:01 +08:00
|
|
|
|
# Use a IANA timezone as argument
|
2023-01-09 16:03:38 +08:00
|
|
|
|
tz = zoneinfo.ZoneInfo("Europe/Paris")
|
|
|
|
|
ctx = Context(
|
|
|
|
|
{
|
|
|
|
|
"dt": datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT),
|
|
|
|
|
"tz": tz,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(tpl.render(ctx), "2011-09-01T12:20:30+02:00")
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
2021-01-19 18:16:01 +08:00
|
|
|
|
# Use a IANA timezone name as argument
|
2019-01-03 07:18:19 +08:00
|
|
|
|
ctx = Context(
|
|
|
|
|
{
|
|
|
|
|
"dt": datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT),
|
|
|
|
|
"tz": "Europe/Paris",
|
|
|
|
|
}
|
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(tpl.render(ctx), "2011-09-01T12:20:30+02:00")
|
|
|
|
|
|
2019-11-06 22:14:30 +08:00
|
|
|
|
@skipIf(sys.platform == "win32", "Windows uses non-standard time zone names")
|
2011-11-18 21:01:06 +08:00
|
|
|
|
def test_get_current_timezone_templatetag(self):
|
|
|
|
|
"""
|
|
|
|
|
Test the {% get_current_timezone %} templatetag.
|
|
|
|
|
"""
|
|
|
|
|
tpl = Template(
|
|
|
|
|
"{% load tz %}{% get_current_timezone as time_zone %}{{ time_zone }}"
|
|
|
|
|
)
|
|
|
|
|
|
2016-10-08 09:06:49 +08:00
|
|
|
|
self.assertEqual(tpl.render(Context()), "Africa/Nairobi")
|
2011-11-18 21:01:06 +08:00
|
|
|
|
with timezone.override(UTC):
|
|
|
|
|
self.assertEqual(tpl.render(Context()), "UTC")
|
|
|
|
|
|
2015-09-12 07:33:12 +08:00
|
|
|
|
tpl = Template(
|
|
|
|
|
"{% load tz %}{% timezone tz %}{% get_current_timezone as time_zone %}"
|
|
|
|
|
"{% endtimezone %}{{ time_zone }}"
|
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
|
|
self.assertEqual(tpl.render(Context({"tz": ICT})), "+0700")
|
|
|
|
|
with timezone.override(UTC):
|
|
|
|
|
self.assertEqual(tpl.render(Context({"tz": ICT})), "+0700")
|
|
|
|
|
|
2021-01-19 18:16:01 +08:00
|
|
|
|
def test_get_current_timezone_templatetag_with_iana(self):
|
2011-11-18 21:01:06 +08:00
|
|
|
|
tpl = Template(
|
|
|
|
|
"{% load tz %}{% get_current_timezone as time_zone %}{{ time_zone }}"
|
|
|
|
|
)
|
2023-01-09 16:03:38 +08:00
|
|
|
|
tz = zoneinfo.ZoneInfo("Europe/Paris")
|
|
|
|
|
with timezone.override(tz):
|
|
|
|
|
self.assertEqual(tpl.render(Context()), "Europe/Paris")
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
2015-09-12 07:33:12 +08:00
|
|
|
|
tpl = Template(
|
|
|
|
|
"{% load tz %}{% timezone 'Europe/Paris' %}"
|
|
|
|
|
"{% get_current_timezone as time_zone %}{% endtimezone %}"
|
|
|
|
|
"{{ time_zone }}"
|
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertEqual(tpl.render(Context()), "Europe/Paris")
|
|
|
|
|
|
|
|
|
|
def test_get_current_timezone_templatetag_invalid_argument(self):
|
2017-05-29 03:37:21 +08:00
|
|
|
|
msg = (
|
|
|
|
|
"'get_current_timezone' requires 'as variable' (got "
|
|
|
|
|
"['get_current_timezone'])"
|
2022-02-04 15:08:27 +08:00
|
|
|
|
)
|
2017-05-29 03:37:21 +08:00
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
2011-11-18 21:01:06 +08:00
|
|
|
|
Template("{% load tz %}{% get_current_timezone %}").render()
|
|
|
|
|
|
2019-11-06 22:14:30 +08:00
|
|
|
|
@skipIf(sys.platform == "win32", "Windows uses non-standard time zone names")
|
2011-11-18 21:01:06 +08:00
|
|
|
|
def test_tz_template_context_processor(self):
|
|
|
|
|
"""
|
2014-12-03 06:23:51 +08:00
|
|
|
|
Test the django.template.context_processors.tz template context processor.
|
2011-11-18 21:01:06 +08:00
|
|
|
|
"""
|
|
|
|
|
tpl = Template("{{ TIME_ZONE }}")
|
2015-01-13 05:31:44 +08:00
|
|
|
|
context = Context()
|
|
|
|
|
self.assertEqual(tpl.render(context), "")
|
|
|
|
|
request_context = RequestContext(
|
|
|
|
|
HttpRequest(), processors=[context_processors.tz]
|
|
|
|
|
)
|
2016-10-08 09:06:49 +08:00
|
|
|
|
self.assertEqual(tpl.render(request_context), "Africa/Nairobi")
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
2011-12-27 17:28:50 +08:00
|
|
|
|
@requires_tz_support
|
2011-11-18 21:01:06 +08:00
|
|
|
|
def test_date_and_time_template_filters(self):
|
|
|
|
|
tpl = Template("{{ dt|date:'Y-m-d' }} at {{ dt|time:'H:i:s' }}")
|
|
|
|
|
ctx = Context({"dt": datetime.datetime(2011, 9, 1, 20, 20, 20, tzinfo=UTC)})
|
|
|
|
|
self.assertEqual(tpl.render(ctx), "2011-09-01 at 23:20:20")
|
|
|
|
|
with timezone.override(ICT):
|
|
|
|
|
self.assertEqual(tpl.render(ctx), "2011-09-02 at 03:20:20")
|
|
|
|
|
|
|
|
|
|
def test_date_and_time_template_filters_honor_localtime(self):
|
2015-09-12 07:33:12 +08:00
|
|
|
|
tpl = Template(
|
|
|
|
|
"{% load tz %}{% localtime off %}{{ dt|date:'Y-m-d' }} at "
|
|
|
|
|
"{{ dt|time:'H:i:s' }}{% endlocaltime %}"
|
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
ctx = Context({"dt": datetime.datetime(2011, 9, 1, 20, 20, 20, tzinfo=UTC)})
|
|
|
|
|
self.assertEqual(tpl.render(ctx), "2011-09-01 at 20:20:20")
|
|
|
|
|
with timezone.override(ICT):
|
|
|
|
|
self.assertEqual(tpl.render(ctx), "2011-09-01 at 20:20:20")
|
|
|
|
|
|
2011-12-27 17:28:50 +08:00
|
|
|
|
@requires_tz_support
|
2011-12-05 06:11:12 +08:00
|
|
|
|
def test_now_template_tag_uses_current_time_zone(self):
|
|
|
|
|
# Regression for #17343
|
|
|
|
|
tpl = Template('{% now "O" %}')
|
|
|
|
|
self.assertEqual(tpl.render(Context({})), "+0300")
|
|
|
|
|
with timezone.override(ICT):
|
|
|
|
|
self.assertEqual(tpl.render(Context({})), "+0700")
|
|
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
2023-01-06 21:46:33 +08:00
|
|
|
|
@override_settings(DATETIME_FORMAT="c", TIME_ZONE="Africa/Nairobi", USE_TZ=False)
|
2012-04-29 22:03:46 +08:00
|
|
|
|
class LegacyFormsTests(TestCase):
|
2011-11-18 21:01:06 +08:00
|
|
|
|
def test_form(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
|
form = EventForm({"dt": "2011-09-01 13:20:30"})
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertTrue(form.is_valid())
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
form.cleaned_data["dt"], datetime.datetime(2011, 9, 1, 13, 20, 30)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
|
|
def test_form_with_non_existent_time(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
|
form = EventForm({"dt": "2011-03-27 02:30:00"})
|
2023-01-09 16:03:38 +08:00
|
|
|
|
tz = zoneinfo.ZoneInfo("Europe/Paris")
|
|
|
|
|
with timezone.override(tz):
|
|
|
|
|
# This is a bug.
|
|
|
|
|
self.assertTrue(form.is_valid())
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
form.cleaned_data["dt"],
|
|
|
|
|
datetime.datetime(2011, 3, 27, 2, 30, 0),
|
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
|
|
def test_form_with_ambiguous_time(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
|
form = EventForm({"dt": "2011-10-30 02:30:00"})
|
2023-01-09 16:03:38 +08:00
|
|
|
|
tz = zoneinfo.ZoneInfo("Europe/Paris")
|
|
|
|
|
with timezone.override(tz):
|
|
|
|
|
# This is a bug.
|
|
|
|
|
self.assertTrue(form.is_valid())
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
form.cleaned_data["dt"],
|
|
|
|
|
datetime.datetime(2011, 10, 30, 2, 30, 0),
|
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
|
|
def test_split_form(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
|
form = EventSplitForm({"dt_0": "2011-09-01", "dt_1": "13:20:30"})
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertTrue(form.is_valid())
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
form.cleaned_data["dt"], datetime.datetime(2011, 9, 1, 13, 20, 30)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
|
|
def test_model_form(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
|
EventModelForm({"dt": "2011-09-01 13:20:30"}).save()
|
2011-11-18 21:01:06 +08:00
|
|
|
|
e = Event.objects.get()
|
|
|
|
|
self.assertEqual(e.dt, datetime.datetime(2011, 9, 1, 13, 20, 30))
|
|
|
|
|
|
|
|
|
|
|
2023-01-06 21:46:33 +08:00
|
|
|
|
@override_settings(DATETIME_FORMAT="c", TIME_ZONE="Africa/Nairobi", USE_TZ=True)
|
2012-04-29 22:03:46 +08:00
|
|
|
|
class NewFormsTests(TestCase):
|
2011-12-27 17:28:50 +08:00
|
|
|
|
@requires_tz_support
|
2011-11-18 21:01:06 +08:00
|
|
|
|
def test_form(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
|
form = EventForm({"dt": "2011-09-01 13:20:30"})
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertTrue(form.is_valid())
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
form.cleaned_data["dt"],
|
|
|
|
|
datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_form_with_other_timezone(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
|
form = EventForm({"dt": "2011-09-01 17:20:30"})
|
2011-11-18 21:01:06 +08:00
|
|
|
|
with timezone.override(ICT):
|
|
|
|
|
self.assertTrue(form.is_valid())
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
form.cleaned_data["dt"],
|
|
|
|
|
datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_form_with_non_existent_time(self):
|
2023-01-09 16:03:38 +08:00
|
|
|
|
tz = zoneinfo.ZoneInfo("Europe/Paris")
|
|
|
|
|
with timezone.override(tz):
|
|
|
|
|
form = EventForm({"dt": "2011-03-27 02:30:00"})
|
|
|
|
|
self.assertFalse(form.is_valid())
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
form.errors["dt"],
|
|
|
|
|
[
|
|
|
|
|
"2011-03-27 02:30:00 couldn’t be interpreted in time zone "
|
|
|
|
|
"Europe/Paris; it may be ambiguous or it may not exist."
|
|
|
|
|
],
|
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
|
|
def test_form_with_ambiguous_time(self):
|
2023-01-09 16:03:38 +08:00
|
|
|
|
tz = zoneinfo.ZoneInfo("Europe/Paris")
|
|
|
|
|
with timezone.override(tz):
|
|
|
|
|
form = EventForm({"dt": "2011-10-30 02:30:00"})
|
|
|
|
|
self.assertFalse(form.is_valid())
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
form.errors["dt"],
|
|
|
|
|
[
|
|
|
|
|
"2011-10-30 02:30:00 couldn’t be interpreted in time zone "
|
|
|
|
|
"Europe/Paris; it may be ambiguous or it may not exist."
|
|
|
|
|
],
|
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
2011-12-27 17:28:50 +08:00
|
|
|
|
@requires_tz_support
|
2011-11-18 21:01:06 +08:00
|
|
|
|
def test_split_form(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
|
form = EventSplitForm({"dt_0": "2011-09-01", "dt_1": "13:20:30"})
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertTrue(form.is_valid())
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
form.cleaned_data["dt"],
|
|
|
|
|
datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC),
|
|
|
|
|
)
|
|
|
|
|
|
2013-09-22 04:50:33 +08:00
|
|
|
|
@requires_tz_support
|
|
|
|
|
def test_localized_form(self):
|
|
|
|
|
form = EventLocalizedForm(
|
|
|
|
|
initial={"dt": datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT)}
|
|
|
|
|
)
|
|
|
|
|
with timezone.override(ICT):
|
|
|
|
|
self.assertIn("2011-09-01 17:20:30", str(form))
|
|
|
|
|
|
2011-12-27 17:28:50 +08:00
|
|
|
|
@requires_tz_support
|
2011-11-18 21:01:06 +08:00
|
|
|
|
def test_model_form(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
|
EventModelForm({"dt": "2011-09-01 13:20:30"}).save()
|
2011-11-18 21:01:06 +08:00
|
|
|
|
e = Event.objects.get()
|
|
|
|
|
self.assertEqual(e.dt, datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC))
|
|
|
|
|
|
2013-09-22 04:50:33 +08:00
|
|
|
|
@requires_tz_support
|
|
|
|
|
def test_localized_model_form(self):
|
|
|
|
|
form = EventLocalizedModelForm(
|
|
|
|
|
instance=Event(dt=datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT))
|
2022-02-04 03:24:19 +08:00
|
|
|
|
)
|
2013-09-22 04:50:33 +08:00
|
|
|
|
with timezone.override(ICT):
|
|
|
|
|
self.assertIn("2011-09-01 17:20:30", str(form))
|
|
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
2023-01-06 21:46:33 +08:00
|
|
|
|
@translation.override(None)
|
2016-02-06 04:56:52 +08:00
|
|
|
|
@override_settings(
|
|
|
|
|
DATETIME_FORMAT="c",
|
|
|
|
|
TIME_ZONE="Africa/Nairobi",
|
|
|
|
|
USE_TZ=True,
|
|
|
|
|
ROOT_URLCONF="timezones.urls",
|
|
|
|
|
)
|
2012-04-29 22:03:46 +08:00
|
|
|
|
class AdminTests(TestCase):
|
2015-02-23 08:53:57 +08:00
|
|
|
|
@classmethod
|
|
|
|
|
def setUpTestData(cls):
|
2016-02-06 04:56:52 +08:00
|
|
|
|
cls.u1 = User.objects.create_user(
|
|
|
|
|
password="secret",
|
2015-05-18 09:15:23 +08:00
|
|
|
|
last_login=datetime.datetime(2007, 5, 30, 13, 20, 10, tzinfo=UTC),
|
|
|
|
|
is_superuser=True,
|
|
|
|
|
username="super",
|
|
|
|
|
first_name="Super",
|
|
|
|
|
last_name="User",
|
|
|
|
|
email="super@example.com",
|
|
|
|
|
is_staff=True,
|
|
|
|
|
is_active=True,
|
|
|
|
|
date_joined=datetime.datetime(2007, 5, 30, 13, 20, 10, tzinfo=UTC),
|
2015-02-23 08:53:57 +08:00
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
|
|
def setUp(self):
|
2016-02-06 04:56:52 +08:00
|
|
|
|
self.client.force_login(self.u1)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
2011-12-27 17:28:50 +08:00
|
|
|
|
@requires_tz_support
|
2011-11-18 21:01:06 +08:00
|
|
|
|
def test_changelist(self):
|
|
|
|
|
e = Event.objects.create(
|
|
|
|
|
dt=datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
)
|
2015-03-31 00:33:26 +08:00
|
|
|
|
response = self.client.get(reverse("admin_tz:timezones_event_changelist"))
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertContains(response, e.dt.astimezone(EAT).isoformat())
|
|
|
|
|
|
|
|
|
|
def test_changelist_in_other_timezone(self):
|
|
|
|
|
e = Event.objects.create(
|
|
|
|
|
dt=datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
with timezone.override(ICT):
|
2015-03-31 00:33:26 +08:00
|
|
|
|
response = self.client.get(reverse("admin_tz:timezones_event_changelist"))
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertContains(response, e.dt.astimezone(ICT).isoformat())
|
|
|
|
|
|
2011-12-27 17:28:50 +08:00
|
|
|
|
@requires_tz_support
|
2011-11-18 21:01:06 +08:00
|
|
|
|
def test_change_editable(self):
|
|
|
|
|
e = Event.objects.create(
|
|
|
|
|
dt=datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
)
|
2015-03-31 00:33:26 +08:00
|
|
|
|
response = self.client.get(
|
|
|
|
|
reverse("admin_tz:timezones_event_change", args=(e.pk,))
|
2022-02-04 03:24:19 +08:00
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertContains(response, e.dt.astimezone(EAT).date().isoformat())
|
|
|
|
|
self.assertContains(response, e.dt.astimezone(EAT).time().isoformat())
|
|
|
|
|
|
|
|
|
|
def test_change_editable_in_other_timezone(self):
|
|
|
|
|
e = Event.objects.create(
|
|
|
|
|
dt=datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
with timezone.override(ICT):
|
2015-03-31 00:33:26 +08:00
|
|
|
|
response = self.client.get(
|
|
|
|
|
reverse("admin_tz:timezones_event_change", args=(e.pk,))
|
2022-02-04 03:24:19 +08:00
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertContains(response, e.dt.astimezone(ICT).date().isoformat())
|
|
|
|
|
self.assertContains(response, e.dt.astimezone(ICT).time().isoformat())
|
|
|
|
|
|
2011-12-27 17:28:50 +08:00
|
|
|
|
@requires_tz_support
|
2011-11-18 21:01:06 +08:00
|
|
|
|
def test_change_readonly(self):
|
2022-09-26 19:36:47 +08:00
|
|
|
|
t = Timestamp.objects.create()
|
2015-03-31 00:33:26 +08:00
|
|
|
|
response = self.client.get(
|
|
|
|
|
reverse("admin_tz:timezones_timestamp_change", args=(t.pk,))
|
2022-02-04 03:24:19 +08:00
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertContains(response, t.created.astimezone(EAT).isoformat())
|
|
|
|
|
|
|
|
|
|
def test_change_readonly_in_other_timezone(self):
|
2022-09-26 19:36:47 +08:00
|
|
|
|
t = Timestamp.objects.create()
|
2011-11-18 21:01:06 +08:00
|
|
|
|
with timezone.override(ICT):
|
2015-03-31 00:33:26 +08:00
|
|
|
|
response = self.client.get(
|
|
|
|
|
reverse("admin_tz:timezones_timestamp_change", args=(t.pk,))
|
2022-02-04 03:24:19 +08:00
|
|
|
|
)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
self.assertContains(response, t.created.astimezone(ICT).isoformat())
|