0
0
AirflowHow-ToBeginner ยท 3 min read

How to Set Timezone in Apache Airflow Correctly

To set the timezone in Airflow, update the default_timezone setting in the airflow.cfg file to your desired timezone (e.g., UTC or America/New_York). Alternatively, set the environment variable AIRFLOW__CORE__DEFAULT_TIMEZONE to override the config file.
๐Ÿ“

Syntax

Airflow uses the default_timezone configuration under the [core] section in airflow.cfg to set the timezone. You can specify timezones using IANA timezone names like UTC or America/New_York.

Alternatively, you can set the environment variable AIRFLOW__CORE__DEFAULT_TIMEZONE to the timezone string to override the config file.

ini
[core]
default_timezone = America/New_York
๐Ÿ’ป

Example

This example shows how to set the timezone to UTC in airflow.cfg and verify it in a DAG.

python
[core]
default_timezone = UTC

# Example DAG snippet to print current execution date in configured timezone
from airflow import DAG
from airflow.operators.python import PythonOperator
from datetime import datetime
import pendulum

def print_execution_date(**kwargs):
    print(f"Execution date: {kwargs['execution_date']}")

default_args = {
    'start_date': pendulum.datetime(2024, 1, 1, tz='UTC')
}

dag = DAG('timezone_example', default_args=default_args, schedule_interval='@daily')

print_task = PythonOperator(
    task_id='print_execution_date',
    python_callable=print_execution_date,
    dag=dag
)
Output
Execution date: 2024-01-01 00:00:00+00:00
โš ๏ธ

Common Pitfalls

  • Not restarting Airflow services after changing airflow.cfg so the new timezone is not applied.
  • Using invalid timezone strings that are not recognized by the pendulum library Airflow uses.
  • Forgetting that DAG start_date and other datetime objects should use timezone-aware datetime to avoid confusion.
  • Setting timezone only in DAG code but not in Airflow config, which can cause inconsistent scheduling.
ini
[core]
default_timezone = Invalid/Timezone  # Wrong

# Correct
[core]
default_timezone = UTC
๐Ÿ“Š

Quick Reference

Summary tips for setting timezone in Airflow:

  • Set default_timezone in airflow.cfg under [core].
  • Use valid IANA timezone names like UTC, Europe/London, or America/New_York.
  • Restart Airflow scheduler and webserver after changes.
  • Use timezone-aware datetime objects in DAGs.
  • Override config with environment variable AIRFLOW__CORE__DEFAULT_TIMEZONE if needed.
โœ…

Key Takeaways

Set the timezone in airflow.cfg under the [core] section with default_timezone.
Use valid IANA timezone names like UTC or America/New_York.
Restart Airflow services after changing timezone settings.
Use timezone-aware datetime objects in DAG definitions.
You can override config with AIRFLOW__CORE__DEFAULT_TIMEZONE environment variable.