diff --git a/django_celery_results/backends/database.py b/django_celery_results/backends/database.py index a4e364a6..45eebc3a 100644 --- a/django_celery_results/backends/database.py +++ b/django_celery_results/backends/database.py @@ -8,13 +8,13 @@ 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 ..models import ChordCounter from ..models import GroupResult as GroupResultModel from ..models import TaskResult +from ..utils import now EXCEPTIONS_TO_CATCH = (InterfaceError,) @@ -146,7 +146,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 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"}