| |
| |
| <!-- |
| Javascript to render AIRFLOW-XXX and PR references in text |
| as HTML links. |
| |
| Overrides extrahead block from sphinx_rtd_theme |
| https://www.sphinx-doc.org/en/master/templating.html |
| --> |
| |
| |
| <!DOCTYPE html> |
| <!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]--> |
| <!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]--> |
| <head> |
| <meta charset="utf-8"> |
| |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| |
| <title>Time zones — Airflow Documentation</title> |
| |
| |
| |
| |
| <link rel="shortcut icon" href="_static/pin_32.png"/> |
| |
| |
| |
| |
| |
| <script type="text/javascript" src="_static/js/modernizr.min.js"></script> |
| |
| |
| <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script> |
| <script type="text/javascript" src="_static/jquery.js"></script> |
| <script type="text/javascript" src="_static/underscore.js"></script> |
| <script type="text/javascript" src="_static/doctools.js"></script> |
| <script type="text/javascript" src="_static/language_data.js"></script> |
| <script type="text/javascript" src="_static/jira-links.js"></script> |
| |
| <script type="text/javascript" src="_static/js/theme.js"></script> |
| |
| |
| |
| |
| <link rel="stylesheet" href="_static/css/theme.css" type="text/css" /> |
| <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> |
| <link rel="stylesheet" href="_static/graphviz.css" type="text/css" /> |
| <link rel="stylesheet" href="_static/exampleinclude.css" type="text/css" /> |
| <link rel="index" title="Index" href="genindex.html" /> |
| <link rel="search" title="Search" href="search.html" /> |
| <link rel="next" title="REST API Reference" href="api.html" /> |
| <link rel="prev" title="Security" href="security.html" /> |
| |
| <script> |
| </script> |
| <style> |
| |
| </style> |
| |
| </head> |
| |
| <body class="wy-body-for-nav"> |
| |
| |
| <div class="wy-grid-for-nav"> |
| |
| <nav data-toggle="wy-nav-shift" class="wy-nav-side"> |
| <div class="wy-side-scroll"> |
| <div class="wy-side-nav-search" > |
| |
| |
| |
| <a href="index.html" class="icon icon-home"> Airflow |
| |
| |
| |
| </a> |
| |
| |
| |
| |
| <div class="version"> |
| 1.10.8 |
| </div> |
| |
| |
| |
| |
| <div role="search"> |
| <form id="rtd-search-form" class="wy-form" action="search.html" method="get"> |
| <input type="text" name="q" placeholder="Search docs" /> |
| <input type="hidden" name="check_keywords" value="yes" /> |
| <input type="hidden" name="area" value="default" /> |
| </form> |
| </div> |
| |
| |
| </div> |
| |
| <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation"> |
| |
| |
| |
| |
| |
| |
| <ul class="current"> |
| <li class="toctree-l1"><a class="reference internal" href="project.html">Project</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="license.html">License</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="start.html">Quick Start</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="installation.html">Installation</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="tutorial.html">Tutorial</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="howto/index.html">How-to Guides</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="ui.html">UI / Screenshots</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="concepts.html">Concepts</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="profiling.html">Data Profiling</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="cli.html">Command Line Interface Reference</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="scheduler.html">Scheduling & Triggers</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="executor/index.html">Executor</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="plugins.html">Plugins</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="security.html">Security</a></li> |
| <li class="toctree-l1 current"><a class="current reference internal" href="#">Time zones</a><ul> |
| <li class="toctree-l2"><a class="reference internal" href="#concepts">Concepts</a><ul> |
| <li class="toctree-l3"><a class="reference internal" href="#naive-and-aware-datetime-objects">Naïve and aware datetime objects</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="#interpretation-of-naive-datetime-objects">Interpretation of naive datetime objects</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="#default-time-zone">Default time zone</a></li> |
| </ul> |
| </li> |
| <li class="toctree-l2"><a class="reference internal" href="#time-zone-aware-dags">Time zone aware DAGs</a><ul> |
| <li class="toctree-l3"><a class="reference internal" href="#templates">Templates</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="#cron-schedules">Cron schedules</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="#time-deltas">Time deltas</a></li> |
| </ul> |
| </li> |
| </ul> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="api.html">REST API Reference</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="integration.html">Integration</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="metrics.html">Metrics</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="errors.html">Error Tracking</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="kubernetes.html">Kubernetes</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="lineage.html">Lineage</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="dag-serialization.html">DAG Serialization</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="changelog.html">Changelog</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="best-practices.html">Best Practices</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="faq.html">FAQ</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="macros.html">Macros reference</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="privacy_notice.html">Privacy Notice</a></li> |
| </ul> |
| <p class="caption"><span class="caption-text">References</span></p> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="_api/index.html">Python API</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="configurations-ref.html">Configurations</a></li> |
| </ul> |
| |
| |
| |
| </div> |
| </div> |
| </nav> |
| |
| <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"> |
| |
| |
| <nav class="wy-nav-top" aria-label="top navigation"> |
| |
| <i data-toggle="wy-nav-top" class="fa fa-bars"></i> |
| <a href="index.html">Airflow</a> |
| |
| </nav> |
| |
| |
| <div class="wy-nav-content"> |
| |
| <div class="rst-content"> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <div role="navigation" aria-label="breadcrumbs navigation"> |
| |
| <ul class="wy-breadcrumbs"> |
| |
| <li><a href="index.html">Docs</a> »</li> |
| |
| <li>Time zones</li> |
| |
| |
| <li class="wy-breadcrumbs-aside"> |
| |
| |
| <a href="_sources/timezone.rst.txt" rel="nofollow"> View page source</a> |
| |
| |
| </li> |
| |
| </ul> |
| |
| |
| <hr/> |
| </div> |
| <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article"> |
| <div itemprop="articleBody"> |
| |
| <blockquote> |
| <div></div></blockquote> |
| <div class="section" id="time-zones"> |
| <h1>Time zones<a class="headerlink" href="#time-zones" title="Permalink to this headline">¶</a></h1> |
| <p>Support for time zones is enabled by default. Airflow stores datetime information in UTC internally and in the database. |
| It allows you to run your DAGs with time zone dependent schedules. At the moment Airflow does not convert them to the |
| end user’s time zone in the user interface. There it will always be displayed in UTC. Also templates used in Operators |
| are not converted. Time zone information is exposed and it is up to the writer of DAG what do with it.</p> |
| <p>This is handy if your users live in more than one time zone and you want to display datetime information according to |
| each user’s wall clock.</p> |
| <p>Even if you are running Airflow in only one time zone it is still good practice to store data in UTC in your database |
| (also before Airflow became time zone aware this was also to recommended or even required setup). The main reason is |
| Daylight Saving Time (DST). Many countries have a system of DST, where clocks are moved forward in spring and backward |
| in autumn. If you’re working in local time, you’re likely to encounter errors twice a year, when the transitions |
| happen. (The pendulum and pytz documentation discusses these issues in greater detail.) This probably doesn’t matter |
| for a simple DAG, but it’s a problem if you are in, for example, financial services where you have end of day |
| deadlines to meet.</p> |
| <p>The time zone is set in <code class="docutils literal notranslate"><span class="pre">airflow.cfg</span></code>. By default it is set to utc, but you change it to use the system’s settings or |
| an arbitrary IANA time zone, e.g. <code class="docutils literal notranslate"><span class="pre">Europe/Amsterdam</span></code>. It is dependent on <code class="docutils literal notranslate"><span class="pre">pendulum</span></code>, which is more accurate than <code class="docutils literal notranslate"><span class="pre">pytz</span></code>. |
| Pendulum is installed when you install Airflow.</p> |
| <p>Please note that the Web UI currently only runs in UTC.</p> |
| <div class="section" id="concepts"> |
| <h2>Concepts<a class="headerlink" href="#concepts" title="Permalink to this headline">¶</a></h2> |
| <div class="section" id="naive-and-aware-datetime-objects"> |
| <h3>Naïve and aware datetime objects<a class="headerlink" href="#naive-and-aware-datetime-objects" title="Permalink to this headline">¶</a></h3> |
| <p>Python’s datetime.datetime objects have a tzinfo attribute that can be used to store time zone information, |
| represented as an instance of a subclass of datetime.tzinfo. When this attribute is set and describes an offset, |
| a datetime object is aware. Otherwise, it’s naive.</p> |
| <p>You can use <code class="docutils literal notranslate"><span class="pre">timezone.is_localized()</span></code> and <code class="docutils literal notranslate"><span class="pre">timezone.is_naive()</span></code> to determine whether datetimes are aware or naive.</p> |
| <p>Because Airflow uses time-zone-aware datetime objects. If your code creates datetime objects they need to be aware too.</p> |
| <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">airflow.utils</span> <span class="kn">import</span> <span class="n">timezone</span> |
| |
| <span class="n">now</span> <span class="o">=</span> <span class="n">timezone</span><span class="o">.</span><span class="n">utcnow</span><span class="p">()</span> |
| <span class="n">a_date</span> <span class="o">=</span> <span class="n">timezone</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2017</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span> |
| </pre></div> |
| </div> |
| </div> |
| <div class="section" id="interpretation-of-naive-datetime-objects"> |
| <h3>Interpretation of naive datetime objects<a class="headerlink" href="#interpretation-of-naive-datetime-objects" title="Permalink to this headline">¶</a></h3> |
| <p>Although Airflow operates fully time zone aware, it still accepts naive date time objects for <code class="docutils literal notranslate"><span class="pre">start_dates</span></code> |
| and <code class="docutils literal notranslate"><span class="pre">end_dates</span></code> in your DAG definitions. This is mostly in order to preserve backwards compatibility. In |
| case a naive <code class="docutils literal notranslate"><span class="pre">start_date</span></code> or <code class="docutils literal notranslate"><span class="pre">end_date</span></code> is encountered the default time zone is applied. It is applied |
| in such a way that it is assumed that the naive date time is already in the default time zone. In other |
| words if you have a default time zone setting of <code class="docutils literal notranslate"><span class="pre">Europe/Amsterdam</span></code> and create a naive datetime <code class="docutils literal notranslate"><span class="pre">start_date</span></code> of |
| <code class="docutils literal notranslate"><span class="pre">datetime(2017,1,1)</span></code> it is assumed to be a <code class="docutils literal notranslate"><span class="pre">start_date</span></code> of Jan 1, 2017 Amsterdam time.</p> |
| <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">default_args</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span> |
| <span class="n">start_date</span><span class="o">=</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2016</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> |
| <span class="n">owner</span><span class="o">=</span><span class="s1">'Airflow'</span> |
| <span class="p">)</span> |
| |
| <span class="n">dag</span> <span class="o">=</span> <span class="n">DAG</span><span class="p">(</span><span class="s1">'my_dag'</span><span class="p">,</span> <span class="n">default_args</span><span class="o">=</span><span class="n">default_args</span><span class="p">)</span> |
| <span class="n">op</span> <span class="o">=</span> <span class="n">DummyOperator</span><span class="p">(</span><span class="n">task_id</span><span class="o">=</span><span class="s1">'dummy'</span><span class="p">,</span> <span class="n">dag</span><span class="o">=</span><span class="n">dag</span><span class="p">)</span> |
| <span class="nb">print</span><span class="p">(</span><span class="n">op</span><span class="o">.</span><span class="n">owner</span><span class="p">)</span> <span class="c1"># Airflow</span> |
| </pre></div> |
| </div> |
| <p>Unfortunately, during DST transitions, some datetimes don’t exist or are ambiguous. |
| In such situations, pendulum raises an exception. That’s why you should always create aware |
| datetime objects when time zone support is enabled.</p> |
| <p>In practice, this is rarely an issue. Airflow gives you aware datetime objects in the models and DAGs, and most often, |
| new datetime objects are created from existing ones through timedelta arithmetic. The only datetime that’s often |
| created in application code is the current time, and <code class="docutils literal notranslate"><span class="pre">timezone.utcnow()</span></code> automatically does the right thing.</p> |
| </div> |
| <div class="section" id="default-time-zone"> |
| <h3>Default time zone<a class="headerlink" href="#default-time-zone" title="Permalink to this headline">¶</a></h3> |
| <p>The default time zone is the time zone defined by the <code class="docutils literal notranslate"><span class="pre">default_timezone</span></code> setting under <code class="docutils literal notranslate"><span class="pre">[core]</span></code>. If |
| you just installed Airflow it will be set to <code class="docutils literal notranslate"><span class="pre">utc</span></code>, which is recommended. You can also set it to |
| <code class="docutils literal notranslate"><span class="pre">system</span></code> or an IANA time zone (e.g.``Europe/Amsterdam``). DAGs are also evaluated on Airflow workers, |
| it is therefore important to make sure this setting is equal on all Airflow nodes.</p> |
| <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">core</span><span class="p">]</span> |
| <span class="n">default_timezone</span> <span class="o">=</span> <span class="n">utc</span> |
| </pre></div> |
| </div> |
| </div> |
| </div> |
| <div class="section" id="time-zone-aware-dags"> |
| <h2>Time zone aware DAGs<a class="headerlink" href="#time-zone-aware-dags" title="Permalink to this headline">¶</a></h2> |
| <p>Creating a time zone aware DAG is quite simple. Just make sure to supply a time zone aware <code class="docutils literal notranslate"><span class="pre">start_date</span></code> |
| using <code class="docutils literal notranslate"><span class="pre">pendulum</span></code>.</p> |
| <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">pendulum</span> |
| |
| <span class="n">local_tz</span> <span class="o">=</span> <span class="n">pendulum</span><span class="o">.</span><span class="n">timezone</span><span class="p">(</span><span class="s2">"Europe/Amsterdam"</span><span class="p">)</span> |
| |
| <span class="n">default_args</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span> |
| <span class="n">start_date</span><span class="o">=</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2016</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">local_tz</span><span class="p">),</span> |
| <span class="n">owner</span><span class="o">=</span><span class="s1">'Airflow'</span> |
| <span class="p">)</span> |
| |
| <span class="n">dag</span> <span class="o">=</span> <span class="n">DAG</span><span class="p">(</span><span class="s1">'my_tz_dag'</span><span class="p">,</span> <span class="n">default_args</span><span class="o">=</span><span class="n">default_args</span><span class="p">)</span> |
| <span class="n">op</span> <span class="o">=</span> <span class="n">DummyOperator</span><span class="p">(</span><span class="n">task_id</span><span class="o">=</span><span class="s1">'dummy'</span><span class="p">,</span> <span class="n">dag</span><span class="o">=</span><span class="n">dag</span><span class="p">)</span> |
| <span class="nb">print</span><span class="p">(</span><span class="n">dag</span><span class="o">.</span><span class="n">timezone</span><span class="p">)</span> <span class="c1"># <Timezone [Europe/Amsterdam]></span> |
| </pre></div> |
| </div> |
| <p>Please note that while it is possible to set a <code class="docutils literal notranslate"><span class="pre">start_date</span></code> and <code class="docutils literal notranslate"><span class="pre">end_date</span></code> for Tasks always the DAG timezone |
| or global timezone (in that order) will be used to calculate the next execution date. Upon first encounter |
| the start date or end date will be converted to UTC using the timezone associated with start_date or end_date, |
| then for calculations this timezone information will be disregarded.</p> |
| <div class="section" id="templates"> |
| <h3>Templates<a class="headerlink" href="#templates" title="Permalink to this headline">¶</a></h3> |
| <p>Airflow returns time zone aware datetimes in templates, but does not convert them to local time so they remain in UTC. |
| It is left up to the DAG to handle this.</p> |
| <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">pendulum</span> |
| |
| <span class="n">local_tz</span> <span class="o">=</span> <span class="n">pendulum</span><span class="o">.</span><span class="n">timezone</span><span class="p">(</span><span class="s2">"Europe/Amsterdam"</span><span class="p">)</span> |
| <span class="n">local_tz</span><span class="o">.</span><span class="n">convert</span><span class="p">(</span><span class="n">execution_date</span><span class="p">)</span> |
| </pre></div> |
| </div> |
| </div> |
| <div class="section" id="cron-schedules"> |
| <h3>Cron schedules<a class="headerlink" href="#cron-schedules" title="Permalink to this headline">¶</a></h3> |
| <p>Time zone aware DAGs that use cron schedules respect daylight savings |
| time. For example, a DAG with a start date in the <code class="docutils literal notranslate"><span class="pre">US/Eastern</span></code> time zone |
| with a schedule of <code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre">0</span> <span class="pre">*</span> <span class="pre">*</span> <span class="pre">*</span></code> will run daily at 04:00 UTC during |
| daylight savings time and at 05:00 otherwise.</p> |
| </div> |
| <div class="section" id="time-deltas"> |
| <h3>Time deltas<a class="headerlink" href="#time-deltas" title="Permalink to this headline">¶</a></h3> |
| <p>Time zone aware DAGs that use <code class="docutils literal notranslate"><span class="pre">timedelta</span></code> or <code class="docutils literal notranslate"><span class="pre">relativedelta</span></code> schedules |
| respect daylight savings time for the start date but do not adjust for |
| daylight savings time when scheduling subsequent runs. For example, a |
| DAG with a start date of <code class="docutils literal notranslate"><span class="pre">pendulum.create(2020,</span> <span class="pre">1,</span> <span class="pre">1,</span> <span class="pre">tz="US/Eastern")</span></code> |
| and a schedule interval of <code class="docutils literal notranslate"><span class="pre">timedelta(days=1)</span></code> will run daily at 05:00 |
| UTC regardless of daylight savings time.</p> |
| </div> |
| </div> |
| </div> |
| |
| |
| </div> |
| |
| </div> |
| |
| |
| <footer> |
| |
| <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation"> |
| |
| <a href="api.html" class="btn btn-neutral float-right" title="REST API Reference" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a> |
| |
| |
| <a href="security.html" class="btn btn-neutral float-left" title="Security" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a> |
| |
| </div> |
| |
| |
| <hr/> |
| |
| <div role="contentinfo"> |
| <p> |
| |
| </p> |
| </div> |
| Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. |
| <div class="footer">This page uses <a href="https://analytics.google.com/"> |
| Google Analytics</a> to collect statistics. You can disable it by blocking |
| the JavaScript coming from www.google-analytics.com. Check our |
| <a href="privacy_notice.html">Privacy Policy</a> |
| for more details. |
| </div> |
| |
| |
| </footer> |
| |
| </div> |
| </div> |
| |
| </section> |
| |
| </div> |
| |
| |
| |
| <script type="text/javascript"> |
| jQuery(function () { |
| SphinxRtdTheme.Navigation.enable(true); |
| }); |
| </script> |
| |
| |
| |
| |
| <!-- Theme Analytics --> |
| <script> |
| (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ |
| (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), |
| m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) |
| })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); |
| |
| ga('create', 'UA-140539454-1', 'auto'); |
| ga('send', 'pageview'); |
| </script> |
| |
| |
| |
| |
| </body> |
| </html> |