From 71413b76a31226f612aa31e765df26f3de4c81cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Federico=20Tabb=C3=B2?= Date: Wed, 10 Dec 2025 13:34:31 +0100 Subject: [PATCH 1/3] save date_created as timezone-aware --- django_celery_results/backends/database.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/django_celery_results/backends/database.py b/django_celery_results/backends/database.py index a4e364a6..0446760e 100644 --- a/django_celery_results/backends/database.py +++ b/django_celery_results/backends/database.py @@ -15,6 +15,7 @@ from ..models import ChordCounter from ..models import GroupResult as GroupResultModel from ..models import TaskResult +from ..utils import now EXCEPTIONS_TO_CATCH = (InterfaceError,) @@ -146,7 +147,7 @@ def _store_result( ) if status == states.STARTED: - task_props['date_started'] = Now() + task_props['date_started'] = now() self.TaskModel._default_manager.store_result(**task_props) return result From e00316659daa39fec6c6f9d1230a55eed76f18b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Federico=20Tabb=C3=B2?= Date: Wed, 10 Dec 2025 13:37:31 +0100 Subject: [PATCH 2/3] Remove unused import --- django_celery_results/backends/database.py | 1 - 1 file changed, 1 deletion(-) diff --git a/django_celery_results/backends/database.py b/django_celery_results/backends/database.py index 0446760e..45eebc3a 100644 --- a/django_celery_results/backends/database.py +++ b/django_celery_results/backends/database.py @@ -8,7 +8,6 @@ from celery.utils.log import get_logger from celery.utils.serialization import b64decode, b64encode from django.db import connection, router, transaction -from django.db.models.functions import Now from django.db.utils import InterfaceError from kombu.exceptions import DecodeError From 01078ebe185e5dc3a2be3f88c2a4c724f1deeccf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Federico=20Tabb=C3=B2?= Date: Wed, 10 Dec 2025 19:54:55 +0100 Subject: [PATCH 3/3] Add tests --- t/unit/backends/test_database.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/t/unit/backends/test_database.py b/t/unit/backends/test_database.py index 8baa6cc0..2eb08a2b 100644 --- a/t/unit/backends/test_database.py +++ b/t/unit/backends/test_database.py @@ -968,6 +968,26 @@ def test_custom_state(self): assert self.b.get_status(tid) == states.SUCCESS assert self.b.get_result(tid) == 42 + def test_date_started_is_timezone_aware(self): + """Test that date_started is timezone-aware when task is started""" + tid = uuid() + + # Create a task in PENDING state + self.b.store_result(tid, None, states.PENDING) + tr = TaskResult.objects.get(task_id=tid) + assert tr.date_started is None + + # Mark as started + self.b.mark_as_started(tid) + + # Verify date_started is set and timezone-aware + tr = TaskResult.objects.get(task_id=tid) + assert tr.date_started is not None + assert isinstance(tr.date_started, datetime.datetime) + # Check if datetime is timezone-aware (has tzinfo set) + assert tr.date_started.tzinfo is not None + assert tr.date_started.tzinfo.utcoffset(tr.date_started) is not None + class DjangoCeleryResultRouter: route_app_labels = {"django_celery_results"}