Skip to content

DJANGO_CELERY_BEAT_TZ_AWARE = False Time zone usage issue #924

@NeverStopDreamingWang

Description

@NeverStopDreamingWang

I checked the solutions in celery/django-celery-beat/issues/798, specifically #798 (comment).

However, this still doesn't solve the problem.

Core Problem Statement

The root cause occurs when USE_TZDJANGO_CELERY_BEAT_TZ_AWARE

This mismatch creates timezone awareness incompatibility between

leading to TypeError: can't compare offset-naive and offset-aware datetimes during time comparisons.

Key Conflict Points

Django's USE_TZ Behavior

Setting Storage Behavior Read Behavior
USE_TZ=True Stores all times as UTC Converts to UTC timezone when reading
USE_TZ=False Stores timezone-naive times Reads timezone-naive times

Celery Beat's DJANGO_CELERY_BEAT_TZ_AWARE Behavior

Setting Time Generation Method
True datetime.now(self.app.timezone) (timezone-aware)
False datetime.utcnow() (timezone-naive UTC)
    def _default_now(self):
        if getattr(settings, 'DJANGO_CELERY_BEAT_TZ_AWARE', True):
            now = datetime.datetime.now(self.app.timezone)
        else:
            # this ends up getting passed to maybe_make_aware, which expects
            # all naive datetime objects to be in utc time.
            now = datetime.datetime.utcnow()
        return now

Conflict Scenarios Analysis

Scenario DJANGO_CELERY_BEAT_TZ_AWARE USE_TZ _default_now() Returns ORM Returns Result
1 True False Timezone-aware Timezone-naive Comparison error
2 False True Timezone-naive Timezone-aware Comparison error
3 True True Timezone-aware Timezone-aware Valid
4 False False Timezone-naive Timezone-naive Valid

Core Problems with last_run_at

When USE_TZDJANGO_CELERY_BEAT_TZ_AWARE:

Celery Version: 5.5.1
Celery-Beat Version: 2.8.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions