Last active
October 22, 2019 08:14
-
-
Save chenhan1218/d714b0ef3010bcbe2c005985bc216d11 to your computer and use it in GitHub Desktop.
airflow dag with timezone aware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime, timedelta | |
import pendulum | |
from airflow import DAG | |
from airflow.operators.python_operator import PythonOperator | |
default_args = {"owner": "airflow"} | |
def f1(tzinfo: datetime.tzinfo = None, **context): | |
if tzinfo is not None: | |
print(("local time:", tzinfo.convert(context["execution_date"]))) | |
for key in context: | |
print((key, context[key])) | |
list_tz = [ | |
"Europe/Amsterdam", | |
"America/New_York", | |
"America/Los_Angeles", | |
"Asia/Jakarta", | |
"Asia/Kolkata", | |
"Asia/Taipei", | |
] | |
for tz in list_tz: | |
format_tz = tz.replace("/", "_") | |
dag_id = "timezone_%s" % format_tz | |
with DAG( | |
dag_id, | |
default_args=default_args, | |
catchup=True, | |
start_date=datetime(2018, 11, 1, tzinfo=pendulum.timezone(tz)), | |
end_date=datetime(2019, 11, 1, tzinfo=pendulum.timezone(tz)), | |
schedule_interval="@daily", | |
) as dag: | |
# As of airflow 1.10.5, the marcos value is based on UTC. | |
# For timezone aware task, I suggest to only use execution_date as a reference. | |
# Don't use ds or prev_ds | |
# See https://airflow.apache.org/timezone.html#templates | |
task = PythonOperator( | |
task_id="t1", | |
provide_context=True, | |
python_callable=f1, | |
dag=dag, | |
op_kwargs={"tzinfo": pendulum.timezone(tz)}, | |
) | |
globals()[dag_id] = dag |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment