Skip to content

Cron Task type is not aware about local Timezone in the cron-string #288

@dmitriev-e

Description

@dmitriev-e

Describe the bug
If you have Timezone configuration in the host system
(in my case I have timezone EEST

Wed Jul  2 13:00:44 EEST 2025

plus
if you have Timezone configuration in Django

Python 3.12.0 (v3.12.0:0fb18b02c8, Oct  2 2023, 09:45:56) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.utils.timezone import get_current_timezone
>>> print(get_current_timezone())
Europe/Nicosia
>>> from django.utils.timezone import localtime 
>>> print(localtime())
2025-07-02 13:08:58.644141+03:00
>>>

but
In Database you store datetime in UTC format

+--------------------+---------------------+--------------------+
| @@global.time_zone | @@session.time_zone | @@system_time_zone |
+--------------------+---------------------+--------------------+
| SYSTEM             | SYSTEM              | UTC                |
+--------------------+---------------------+--------------------+
1 row in set (0.001 sec)

When you try to create a Cron-type task via build-in web-interface
with next cron-string
55 14 * * *

in DB we have next record (I converted it to JSON-format)

"scheduler_task": [
	{
		"id" : 9,
		"created_at" : "2025-07-02T06:53:11.558Z",
		"updated_at" : "2025-07-02T08:55:47.818Z",
		"name" : "sample_everyday_cron",
		"task_type" : "CronTaskType",
		"callable" : "scheduler.tests.jobs.test_args_kwargs",
		"enabled" : 1,
		"queue" : "default",
		"at_front" : 0,
		"timeout" : null,
		"result_ttl" : null,
		"failed_runs" : 0,
		"successful_runs" : 0,
		"last_successful_run" : null,
		"last_failed_run" : null,
		"interval" : null,
		"interval_unit" : "hours",
		"repeat" : null,
		"scheduled_time" : "2025-07-02T11:55:00.000Z",
		"cron_string" : "55 14 * * *",
		"job_name" : "default:9:20250702115016673618"
	}
]}

Ok - We have "scheduled_time" "2025-07-02T11:55:00.000Z" - is correct time in UTC format
but what about worker job ?
Let's check Redis

127.0.0.1:6379> ZRANGE :registry:default:scheduled_jobs 0 -1 WITHSCORES
1) "default:9:20250702115016673618"
2) "1751468100"

convert Timestamp 1751468100 to Date - 07/02/2025, 14:55:00 <<-- This is UTC datetime !!

So, In the end, we have next run at 07/02/2025, 14:55:00 in UTC time
It means - the real running will be at 17:55 in my case (UTC+3)

To Reproduce
Steps to reproduce the behavior:

  1. Be sure, that you have another timezone on you PC (I have UTC+3)
  2. Be sure that Worker is running
  3. Create task-type "Cron Task"
  4. Fill the cron-string , for example now+few hours
  5. Go to /scheduler/queue/
  6. Click to Scheduled Jobs count link in you queue (in my case - default queue)
  7. Find new ID for the recently created task
  8. Check column "Scheduled"
  9. Check real time of real execution

Expected behavior
When I fill cron-string, I'm expecting executiong at specific time. Cron Task must to be aware about timezone.

Screenshots
Image

Desktop (please complete the following information):

  • OS: MacOS Sonoma
  • python version - 3.12
  • django version - 5.1.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions