| |
| |
| <!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="stylesheet" href="_static/css/theme.css" type="text/css" /> |
| <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> |
| <link rel="index" title="Index" href="genindex.html" /> |
| <link rel="search" title="Search" href="search.html" /> |
| <link rel="next" title="Experimental Rest API" href="api.html" /> |
| <link rel="prev" title="Security" href="security.html" /> |
| |
| |
| <script src="_static/js/modernizr.min.js"></script> |
| <!-- Matomo --> |
| <script> |
| var _paq = window._paq = window._paq || []; |
| _paq.push(['disableCookies']); |
| _paq.push(['trackPageView']); |
| _paq.push(['enableLinkTracking']); |
| (function() { |
| var u="https://analytics.apache.org/"; |
| _paq.push(['setTrackerUrl', u+'matomo.php']); |
| _paq.push(['setSiteId', '13']); |
| var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; |
| g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); |
| })(); |
| </script> |
| <!-- End Matomo --> |
| </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 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</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="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">Experimental Rest API</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="lineage.html">Lineage</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="code.html">API Reference</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"> |
| |
| <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 <cite>airflow.cfg</cite>. 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. <cite>Europe/Amsterdam</cite>. It is dependent on <cite>pendulum</cite>, which is more accurate than <cite>pytz</cite>. |
| 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 timezone.is_aware() and timezone.is_naive() 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="code python highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">airflow.utils</span> <span class="k">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 <cite>start_dates</cite> |
| and <cite>end_dates</cite> in your DAG definitions. This is mostly in order to preserve backwards compatibility. In |
| case a naive <cite>start_date</cite> or <cite>end_date</cite> 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 <cite>Europe/Amsterdam</cite> and create a naive datetime <cite>start_date</cite> of |
| <cite>datetime(2017,1,1)</cite> it is assumed to be a <cite>start_date</cite> of Jan 1, 2017 Amsterdam time.</p> |
| <div class="code python highlight-default 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 timezone.utcnow() 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 <cite>default_timezone</cite> setting under <cite>[core]</cite>. If |
| you just installed Airflow it will be set to <cite>utc</cite>, which is recommended. You can also set it to |
| <cite>system</cite> 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="code python highlight-default 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 <cite>start_date</cite>. It is |
| recommended to use <cite>pendulum</cite> for this, but <cite>pytz</cite> (to be installed manually) can also be used for this.</p> |
| <div class="code python highlight-default 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 <cite>start_date</cite> and <cite>end_date</cite> 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="code python highlight-default 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>In case you set a cron schedule, Airflow assumes you will always want to run at the exact same time. It will |
| then ignore day light savings time. Thus, if you have a schedule that says |
| run at end of interval every day at 08:00 GMT+1 it will always run end of interval 08:00 GMT+1, |
| regardless if day light savings time is in place.</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>For schedules with time deltas Airflow assumes you always will want to run with the specified interval. So if you |
| specify a timedelta(hours=2) you will always want to run to hours later. In this case day light savings time will |
| be taken into account.</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="Experimental Rest API" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a> |
| |
| |
| <a href="security.html" class="btn btn-neutral" 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>. |
| |
| </footer> |
| |
| </div> |
| </div> |
| |
| </section> |
| |
| </div> |
| |
| |
| |
| |
| |
| |
| |
| <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/js/theme.js"></script> |
| |
| <script type="text/javascript"> |
| jQuery(function () { |
| SphinxRtdTheme.Navigation.enable(true); |
| }); |
| </script> |
| |
| </body> |
| </html> |