@@ -2,23 +2,23 @@ Time zones
2
2
==========
3
3
4
4
Support for time zones is enabled by default. Airflow stores datetime information in UTC internally and in the database.
5
- It allows you to run your DAGs with time zone dependent schedules. At the moment Airflow does not convert them to the
6
- end user’s time zone in the user interface. There it will always be displayed in UTC. Also templates used in Operators
5
+ It allows you to run your DAGs with time zone dependent schedules. At the moment Airflow does not convert them to the
6
+ end user’s time zone in the user interface. There it will always be displayed in UTC. Also templates used in Operators
7
7
are not converted. Time zone information is exposed and it is up to the writer of DAG what do with it.
8
8
9
- This is handy if your users live in more than one time zone and you want to display datetime information according to
9
+ This is handy if your users live in more than one time zone and you want to display datetime information according to
10
10
each user’s wall clock.
11
11
12
- Even if you are running Airflow in only one time zone it is still good practice to store data in UTC in your database
13
- (also before Airflow became time zone aware this was also to recommended or even required setup). The main reason is
14
- Daylight Saving Time (DST). Many countries have a system of DST, where clocks are moved forward in spring and backward
15
- in autumn. If you’re working in local time, you’re likely to encounter errors twice a year, when the transitions
16
- happen. (The pendulum and pytz documentation discusses these issues in greater detail.) This probably doesn’t matter
17
- for a simple DAG, but it’s a problem if you are in, for example, financial services where you have end of day
18
- deadlines to meet.
12
+ Even if you are running Airflow in only one time zone it is still good practice to store data in UTC in your database
13
+ (also before Airflow became time zone aware this was also to recommended or even required setup). The main reason is
14
+ Daylight Saving Time (DST). Many countries have a system of DST, where clocks are moved forward in spring and backward
15
+ in autumn. If you’re working in local time, you’re likely to encounter errors twice a year, when the transitions
16
+ happen. (The pendulum and pytz documentation discusses these issues in greater detail.) This probably doesn’t matter
17
+ for a simple DAG, but it’s a problem if you are in, for example, financial services where you have end of day
18
+ deadlines to meet.
19
19
20
- The time zone is set in `airflow.cfg `. By default it is set to utc, but you change it to use the system’s settings or
21
- an arbitrary IANA time zone, e.g. `Europe/Amsterdam `. It is dependent on `pendulum `, which is more accurate than `pytz `.
20
+ The time zone is set in `airflow.cfg `. By default it is set to utc, but you change it to use the system’s settings or
21
+ an arbitrary IANA time zone, e.g. `Europe/Amsterdam `. It is dependent on `pendulum `, which is more accurate than `pytz `.
22
22
Pendulum is installed when you install Airflow.
23
23
24
24
Please note that the Web UI currently only runs in UTC.
@@ -28,8 +28,8 @@ Concepts
28
28
Naïve and aware datetime objects
29
29
''''''''''''''''''''''''''''''''
30
30
31
- Python’s datetime.datetime objects have a tzinfo attribute that can be used to store time zone information,
32
- represented as an instance of a subclass of datetime.tzinfo. When this attribute is set and describes an offset,
31
+ Python’s datetime.datetime objects have a tzinfo attribute that can be used to store time zone information,
32
+ represented as an instance of a subclass of datetime.tzinfo. When this attribute is set and describes an offset,
33
33
a datetime object is aware. Otherwise, it’s naive.
34
34
35
35
You can use timezone.is_aware() and timezone.is_naive() to determine whether datetimes are aware or naive.
@@ -39,7 +39,7 @@ Because Airflow uses time-zone-aware datetime objects. If your code creates date
39
39
.. code :: python
40
40
41
41
from airflow.utils import timezone
42
-
42
+
43
43
now = timezone.utcnow()
44
44
a_date = timezone.datetime(2017 ,1 ,1 )
45
45
@@ -49,9 +49,9 @@ Interpretation of naive datetime objects
49
49
50
50
Although Airflow operates fully time zone aware, it still accepts naive date time objects for `start_dates `
51
51
and `end_dates ` in your DAG definitions. This is mostly in order to preserve backwards compatibility. In
52
- case a naive `start_date ` or `end_date ` is encountered the default time zone is applied. It is applied
52
+ case a naive `start_date ` or `end_date ` is encountered the default time zone is applied. It is applied
53
53
in such a way that it is assumed that the naive date time is already in the default time zone. In other
54
- words if you have a default time zone setting of `Europe/Amsterdam ` and create a naive datetime `start_date ` of
54
+ words if you have a default time zone setting of `Europe/Amsterdam ` and create a naive datetime `start_date ` of
55
55
`datetime(2017,1,1) ` it is assumed to be a `start_date ` of Jan 1, 2017 Amsterdam time.
56
56
57
57
.. code :: python
@@ -65,16 +65,16 @@ words if you have a default time zone setting of `Europe/Amsterdam` and create a
65
65
op = DummyOperator(task_id = ' dummy' , dag = dag)
66
66
print (op.owner) # Airflow
67
67
68
- Unfortunately, during DST transitions, some datetimes don’t exist or are ambiguous.
69
- In such situations, pendulum raises an exception. That’s why you should always create aware
68
+ Unfortunately, during DST transitions, some datetimes don’t exist or are ambiguous.
69
+ In such situations, pendulum raises an exception. That’s why you should always create aware
70
70
datetime objects when time zone support is enabled.
71
71
72
- In practice, this is rarely an issue. Airflow gives you aware datetime objects in the models and DAGs, and most often,
73
- new datetime objects are created from existing ones through timedelta arithmetic. The only datetime that’s often
72
+ In practice, this is rarely an issue. Airflow gives you aware datetime objects in the models and DAGs, and most often,
73
+ new datetime objects are created from existing ones through timedelta arithmetic. The only datetime that’s often
74
74
created in application code is the current time, and timezone.utcnow() automatically does the right thing.
75
75
76
76
77
- Default time zone
77
+ Default time zone
78
78
'''''''''''''''''
79
79
80
80
The default time zone is the time zone defined by the `default_timezone ` setting under `[core] `. If
@@ -92,15 +92,15 @@ it is therefore important to make sure this setting is equal on all Airflow node
92
92
Time zone aware DAGs
93
93
--------------------
94
94
95
- Creating a time zone aware DAG is quite simple. Just make sure to supply a time zone aware `start_date `. It is
95
+ Creating a time zone aware DAG is quite simple. Just make sure to supply a time zone aware `start_date `. It is
96
96
recommended to use `pendulum ` for this, but `pytz ` (to be installed manually) can also be used for this.
97
97
98
98
.. code :: python
99
99
100
100
import pendulum
101
-
101
+
102
102
local_tz = pendulum.timezone(" Europe/Amsterdam" )
103
-
103
+
104
104
default_args= dict (
105
105
start_date = datetime(2016 , 1 , 1 , tzinfo = local_tz),
106
106
owner = ' Airflow'
@@ -110,29 +110,32 @@ recommended to use `pendulum` for this, but `pytz` (to be installed manually) ca
110
110
op = DummyOperator(task_id = ' dummy' , dag = dag)
111
111
print (dag.timezone) # <Timezone [Europe/Amsterdam]>
112
112
113
-
113
+ Please note that while it is possible to set a `start_date ` and `end_date ` for Tasks always the DAG timezone
114
+ or global timezone (in that order) will be used to calculate the next execution date. Upon first encounter
115
+ the start date or end date will be converted to UTC using the timezone associated with start_date or end_date,
116
+ then for calculations this timezone information will be disregarded.
114
117
115
118
Templates
116
119
'''''''''
117
120
118
- Airflow returns time zone aware datetimes in templates, but does not convert them to local time so they remain in UTC.
121
+ Airflow returns time zone aware datetimes in templates, but does not convert them to local time so they remain in UTC.
119
122
It is left up to the DAG to handle this.
120
123
121
124
.. code :: python
122
125
123
126
import pendulum
124
-
127
+
125
128
local_tz = pendulum.timezone(" Europe/Amsterdam" )
126
129
local_tz.convert(execution_date)
127
130
128
131
129
132
Cron schedules
130
133
''''''''''''''
131
134
132
- In case you set a cron schedule, Airflow assumes you will always want to run at the exact same time. It will
133
- then ignore day light savings time. Thus, if you have a schedule that says
134
- run at end of interval every day at 08:00 GMT+1 it will always run end of interval 08:00 GMT+1,
135
- regardless if day light savings time is in place.
135
+ In case you set a cron schedule, Airflow assumes you will always want to run at the exact same time. It will
136
+ then ignore day light savings time. Thus, if you have a schedule that says
137
+ run at end of interval every day at 08:00 GMT+1 it will always run end of interval 08:00 GMT+1,
138
+ regardless if day light savings time is in place.
136
139
137
140
138
141
Time deltas
0 commit comments