Kerala Cyber
Warriors
KCW Uploader V1.1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>8.1. datetime — Basic date and time types — Python 2.7.18 documentation</title>
<link rel="stylesheet" href="../_static/classic.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<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/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Search within Python 2.7.18 documentation"
href="../_static/opensearch.xml"/>
<link rel="author" title="About these documents" href="../about.html" />
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="copyright" title="Copyright" href="../copyright.html" />
<link rel="next" title="8.2. calendar — General calendar-related functions" href="calendar.html" />
<link rel="prev" title="8. Data Types" href="datatypes.html" />
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
<link rel="canonical" href="file:///usr/share/doc/python2.7/html/library/datetime.html" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
</head><body>
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
This document is for an old version of Python that is <a href="https://devguide.python.org/devcycle/#end-of-life-branches">no longer supported</a>.
You should install the python3 and python3-doc packages and read the
<a href="file:///usr/share/doc/python3-doc/html/library/datetime.html"> Python documentation for the Python3 version packaged in this release</a>.
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="calendar.html" title="8.2. calendar — General calendar-related functions"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="datatypes.html" title="8. Data Types"
accesskey="P">previous</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<a href="../index.html">Python 2.7.18 documentation</a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
<li class="nav-item nav-item-2"><a href="datatypes.html" accesskey="U">8. Data Types</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="module-datetime">
<span id="datetime-basic-date-and-time-types"></span><h1>8.1. <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> — Basic date and time types<a class="headerlink" href="#module-datetime" title="Permalink to this headline">¶</a></h1>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.3.</span></p>
</div>
<p>The <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> module supplies classes for manipulating dates and times in
both simple and complex ways. While date and time arithmetic is supported, the
focus of the implementation is on efficient attribute extraction for output
formatting and manipulation. For related functionality, see also the
<a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</span></code></a> and <a class="reference internal" href="calendar.html#module-calendar" title="calendar: Functions for working with calendars, including some emulation of the Unix cal program."><code class="xref py py-mod docutils literal notranslate"><span class="pre">calendar</span></code></a> modules.</p>
<p>There are two kinds of date and time objects: “naive” and “aware”.</p>
<p>An aware object has sufficient knowledge of applicable algorithmic and
political time adjustments, such as time zone and daylight saving time
information, to locate itself relative to other aware objects. An aware object
is used to represent a specific moment in time that is not open to
interpretation <a class="footnote-reference" href="#id2" id="id1">[1]</a>.</p>
<p>A naive object does not contain enough information to unambiguously locate
itself relative to other date/time objects. Whether a naive object represents
Coordinated Universal Time (UTC), local time, or time in some other timezone is
purely up to the program, just like it’s up to the program whether a particular
number represents metres, miles, or mass. Naive objects are easy to understand
and to work with, at the cost of ignoring some aspects of reality.</p>
<p>For applications requiring aware objects, <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> and <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a>
objects have an optional time zone information attribute, <code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code>, that
can be set to an instance of a subclass of the abstract <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> class.
These <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> objects capture information about the offset from UTC
time, the time zone name, and whether Daylight Saving Time is in effect. Note
that no concrete <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> classes are supplied by the <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a>
module. Supporting timezones at whatever level of detail is required is up to
the application. The rules for time adjustment across the world are more
political than rational, and there is no standard suitable for every
application.</p>
<p>The <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> module exports the following constants:</p>
<dl class="data">
<dt id="datetime.MINYEAR">
<code class="descclassname">datetime.</code><code class="descname">MINYEAR</code><a class="headerlink" href="#datetime.MINYEAR" title="Permalink to this definition">¶</a></dt>
<dd><p>The smallest year number allowed in a <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> or <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object.
<a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MINYEAR</span></code></a> is <code class="docutils literal notranslate"><span class="pre">1</span></code>.</p>
</dd></dl>
<dl class="data">
<dt id="datetime.MAXYEAR">
<code class="descclassname">datetime.</code><code class="descname">MAXYEAR</code><a class="headerlink" href="#datetime.MAXYEAR" title="Permalink to this definition">¶</a></dt>
<dd><p>The largest year number allowed in a <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> or <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object.
<a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MAXYEAR</span></code></a> is <code class="docutils literal notranslate"><span class="pre">9999</span></code>.</p>
</dd></dl>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt>Module <a class="reference internal" href="calendar.html#module-calendar" title="calendar: Functions for working with calendars, including some emulation of the Unix cal program."><code class="xref py py-mod docutils literal notranslate"><span class="pre">calendar</span></code></a></dt>
<dd>General calendar related functions.</dd>
<dt>Module <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</span></code></a></dt>
<dd>Time access and conversions.</dd>
</dl>
</div>
<div class="section" id="available-types">
<h2>8.1.1. Available Types<a class="headerlink" href="#available-types" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt>
<em class="property">class </em><code class="descclassname">datetime.</code><code class="descname">date</code></dt>
<dd><p>An idealized naive date, assuming the current Gregorian calendar always was, and
always will be, in effect. Attributes: <a class="reference internal" href="#datetime.date.year" title="datetime.date.year"><code class="xref py py-attr docutils literal notranslate"><span class="pre">year</span></code></a>, <a class="reference internal" href="#datetime.date.month" title="datetime.date.month"><code class="xref py py-attr docutils literal notranslate"><span class="pre">month</span></code></a>, and
<a class="reference internal" href="#datetime.date.day" title="datetime.date.day"><code class="xref py py-attr docutils literal notranslate"><span class="pre">day</span></code></a>.</p>
</dd></dl>
<dl class="class">
<dt>
<em class="property">class </em><code class="descclassname">datetime.</code><code class="descname">time</code></dt>
<dd><p>An idealized time, independent of any particular day, assuming that every day
has exactly 24*60*60 seconds (there is no notion of “leap seconds” here).
Attributes: <a class="reference internal" href="#datetime.time.hour" title="datetime.time.hour"><code class="xref py py-attr docutils literal notranslate"><span class="pre">hour</span></code></a>, <a class="reference internal" href="#datetime.time.minute" title="datetime.time.minute"><code class="xref py py-attr docutils literal notranslate"><span class="pre">minute</span></code></a>, <a class="reference internal" href="#datetime.time.second" title="datetime.time.second"><code class="xref py py-attr docutils literal notranslate"><span class="pre">second</span></code></a>, <a class="reference internal" href="#datetime.time.microsecond" title="datetime.time.microsecond"><code class="xref py py-attr docutils literal notranslate"><span class="pre">microsecond</span></code></a>,
and <a class="reference internal" href="#datetime.time.tzinfo" title="datetime.time.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a>.</p>
</dd></dl>
<dl class="class">
<dt>
<em class="property">class </em><code class="descclassname">datetime.</code><code class="descname">datetime</code></dt>
<dd><p>A combination of a date and a time. Attributes: <a class="reference internal" href="#datetime.datetime.year" title="datetime.datetime.year"><code class="xref py py-attr docutils literal notranslate"><span class="pre">year</span></code></a>, <a class="reference internal" href="#datetime.datetime.month" title="datetime.datetime.month"><code class="xref py py-attr docutils literal notranslate"><span class="pre">month</span></code></a>,
<a class="reference internal" href="#datetime.datetime.day" title="datetime.datetime.day"><code class="xref py py-attr docutils literal notranslate"><span class="pre">day</span></code></a>, <a class="reference internal" href="#datetime.datetime.hour" title="datetime.datetime.hour"><code class="xref py py-attr docutils literal notranslate"><span class="pre">hour</span></code></a>, <a class="reference internal" href="#datetime.datetime.minute" title="datetime.datetime.minute"><code class="xref py py-attr docutils literal notranslate"><span class="pre">minute</span></code></a>, <a class="reference internal" href="#datetime.datetime.second" title="datetime.datetime.second"><code class="xref py py-attr docutils literal notranslate"><span class="pre">second</span></code></a>, <a class="reference internal" href="#datetime.datetime.microsecond" title="datetime.datetime.microsecond"><code class="xref py py-attr docutils literal notranslate"><span class="pre">microsecond</span></code></a>,
and <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a>.</p>
</dd></dl>
<dl class="class">
<dt>
<em class="property">class </em><code class="descclassname">datetime.</code><code class="descname">timedelta</code></dt>
<dd><p>A duration expressing the difference between two <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a>, <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a>,
or <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> instances to microsecond resolution.</p>
</dd></dl>
<dl class="class">
<dt>
<em class="property">class </em><code class="descclassname">datetime.</code><code class="descname">tzinfo</code></dt>
<dd><p>An abstract base class for time zone information objects. These are used by the
<a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> and <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> classes to provide a customizable notion of
time adjustment (for example, to account for time zone and/or daylight saving
time).</p>
</dd></dl>
<p>Objects of these types are immutable.</p>
<p>Objects of the <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> type are always naive.</p>
<p>An object of type <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> or <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> may be naive or aware.
A <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object <em>d</em> is aware if <code class="docutils literal notranslate"><span class="pre">d.tzinfo</span></code> is not <code class="docutils literal notranslate"><span class="pre">None</span></code> and
<code class="docutils literal notranslate"><span class="pre">d.tzinfo.utcoffset(d)</span></code> does not return <code class="docutils literal notranslate"><span class="pre">None</span></code>. If <code class="docutils literal notranslate"><span class="pre">d.tzinfo</span></code> is
<code class="docutils literal notranslate"><span class="pre">None</span></code>, or if <code class="docutils literal notranslate"><span class="pre">d.tzinfo</span></code> is not <code class="docutils literal notranslate"><span class="pre">None</span></code> but <code class="docutils literal notranslate"><span class="pre">d.tzinfo.utcoffset(d)</span></code>
returns <code class="docutils literal notranslate"><span class="pre">None</span></code>, <em>d</em> is naive. A <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> object <em>t</em> is aware
if <code class="docutils literal notranslate"><span class="pre">t.tzinfo</span></code> is not <code class="docutils literal notranslate"><span class="pre">None</span></code> and <code class="docutils literal notranslate"><span class="pre">t.tzinfo.utcoffset(None)</span></code> does not return
<code class="docutils literal notranslate"><span class="pre">None</span></code>. Otherwise, <em>t</em> is naive.</p>
<p>The distinction between naive and aware doesn’t apply to <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a>
objects.</p>
<p>Subclass relationships:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">object</span>
<span class="n">timedelta</span>
<span class="n">tzinfo</span>
<span class="n">time</span>
<span class="n">date</span>
<span class="n">datetime</span>
</pre></div>
</div>
</div>
<div class="section" id="timedelta-objects">
<span id="datetime-timedelta"></span><h2>8.1.2. <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> Objects<a class="headerlink" href="#timedelta-objects" title="Permalink to this headline">¶</a></h2>
<p>A <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object represents a duration, the difference between two
dates or times.</p>
<dl class="class">
<dt id="datetime.timedelta">
<em class="property">class </em><code class="descclassname">datetime.</code><code class="descname">timedelta</code><span class="sig-paren">(</span><span class="optional">[</span><em>days</em><span class="optional">[</span>, <em>seconds</em><span class="optional">[</span>, <em>microseconds</em><span class="optional">[</span>, <em>milliseconds</em><span class="optional">[</span>, <em>minutes</em><span class="optional">[</span>, <em>hours</em><span class="optional">[</span>, <em>weeks</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.timedelta" title="Permalink to this definition">¶</a></dt>
<dd><p>All arguments are optional and default to <code class="docutils literal notranslate"><span class="pre">0</span></code>. Arguments may be ints, longs,
or floats, and may be positive or negative.</p>
<p>Only <em>days</em>, <em>seconds</em> and <em>microseconds</em> are stored internally. Arguments are
converted to those units:</p>
<ul class="simple">
<li>A millisecond is converted to 1000 microseconds.</li>
<li>A minute is converted to 60 seconds.</li>
<li>An hour is converted to 3600 seconds.</li>
<li>A week is converted to 7 days.</li>
</ul>
<p>and days, seconds and microseconds are then normalized so that the
representation is unique, with</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">microseconds</span> <span class="pre"><</span> <span class="pre">1000000</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">seconds</span> <span class="pre"><</span> <span class="pre">3600*24</span></code> (the number of seconds in one day)</li>
<li><code class="docutils literal notranslate"><span class="pre">-999999999</span> <span class="pre"><=</span> <span class="pre">days</span> <span class="pre"><=</span> <span class="pre">999999999</span></code></li>
</ul>
<p>If any argument is a float and there are fractional microseconds, the fractional
microseconds left over from all arguments are combined and their sum is rounded
to the nearest microsecond. If no argument is a float, the conversion and
normalization processes are exact (no information is lost).</p>
<p>If the normalized value of days lies outside the indicated range,
<a class="reference internal" href="exceptions.html#exceptions.OverflowError" title="exceptions.OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> is raised.</p>
<p>Note that normalization of negative values may be surprising at first. For
example,</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">timedelta</span>
<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">microseconds</span><span class="o">=-</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">>>> </span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">days</span><span class="p">,</span> <span class="n">d</span><span class="o">.</span><span class="n">seconds</span><span class="p">,</span> <span class="n">d</span><span class="o">.</span><span class="n">microseconds</span><span class="p">)</span>
<span class="go">(-1, 86399, 999999)</span>
</pre></div>
</div>
</dd></dl>
<p>Class attributes are:</p>
<dl class="attribute">
<dt id="datetime.timedelta.min">
<code class="descclassname">timedelta.</code><code class="descname">min</code><a class="headerlink" href="#datetime.timedelta.min" title="Permalink to this definition">¶</a></dt>
<dd><p>The most negative <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object, <code class="docutils literal notranslate"><span class="pre">timedelta(-999999999)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.timedelta.max">
<code class="descclassname">timedelta.</code><code class="descname">max</code><a class="headerlink" href="#datetime.timedelta.max" title="Permalink to this definition">¶</a></dt>
<dd><p>The most positive <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object, <code class="docutils literal notranslate"><span class="pre">timedelta(days=999999999,</span>
<span class="pre">hours=23,</span> <span class="pre">minutes=59,</span> <span class="pre">seconds=59,</span> <span class="pre">microseconds=999999)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.timedelta.resolution">
<code class="descclassname">timedelta.</code><code class="descname">resolution</code><a class="headerlink" href="#datetime.timedelta.resolution" title="Permalink to this definition">¶</a></dt>
<dd><p>The smallest possible difference between non-equal <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> objects,
<code class="docutils literal notranslate"><span class="pre">timedelta(microseconds=1)</span></code>.</p>
</dd></dl>
<p>Note that, because of normalization, <code class="docutils literal notranslate"><span class="pre">timedelta.max</span></code> > <code class="docutils literal notranslate"><span class="pre">-timedelta.min</span></code>.
<code class="docutils literal notranslate"><span class="pre">-timedelta.max</span></code> is not representable as a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object.</p>
<p>Instance attributes (read-only):</p>
<table border="1" class="docutils">
<colgroup>
<col width="29%" />
<col width="71%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Attribute</th>
<th class="head">Value</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">days</span></code></td>
<td>Between -999999999 and 999999999 inclusive</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">seconds</span></code></td>
<td>Between 0 and 86399 inclusive</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">microseconds</span></code></td>
<td>Between 0 and 999999 inclusive</td>
</tr>
</tbody>
</table>
<p>Supported operations:</p>
<table border="1" class="docutils">
<colgroup>
<col width="41%" />
<col width="59%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Operation</th>
<th class="head">Result</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">+</span> <span class="pre">t3</span></code></td>
<td>Sum of <em>t2</em> and <em>t3</em>. Afterwards <em>t1</em>-<em>t2</em> ==
<em>t3</em> and <em>t1</em>-<em>t3</em> == <em>t2</em> are true. (1)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">-</span> <span class="pre">t3</span></code></td>
<td>Difference of <em>t2</em> and <em>t3</em>. Afterwards <em>t1</em>
== <em>t2</em> - <em>t3</em> and <em>t2</em> == <em>t1</em> + <em>t3</em> are
true. (1)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">*</span> <span class="pre">i</span> <span class="pre">or</span> <span class="pre">t1</span> <span class="pre">=</span> <span class="pre">i</span> <span class="pre">*</span> <span class="pre">t2</span></code></td>
<td>Delta multiplied by an integer or long.
Afterwards <em>t1</em> // i == <em>t2</em> is true,
provided <code class="docutils literal notranslate"><span class="pre">i</span> <span class="pre">!=</span> <span class="pre">0</span></code>.</td>
</tr>
<tr class="row-odd"><td> </td>
<td>In general, <em>t1</em> * i == <em>t1</em> * (i-1) + <em>t1</em>
is true. (1)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">//</span> <span class="pre">i</span></code></td>
<td>The floor is computed and the remainder (if
any) is thrown away. (3)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">+t1</span></code></td>
<td>Returns a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object with the
same value. (2)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">-t1</span></code></td>
<td>equivalent to <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a>(-<em>t1.days</em>, -<em>t1.seconds</em>,
-<em>t1.microseconds</em>), and to <em>t1</em>* -1. (1)(4)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">abs(t)</span></code></td>
<td>equivalent to +<em>t</em> when <code class="docutils literal notranslate"><span class="pre">t.days</span> <span class="pre">>=</span> <span class="pre">0</span></code>, and
to -<em>t</em> when <code class="docutils literal notranslate"><span class="pre">t.days</span> <span class="pre"><</span> <span class="pre">0</span></code>. (2)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">str(t)</span></code></td>
<td>Returns a string in the form
<code class="docutils literal notranslate"><span class="pre">[D</span> <span class="pre">day[s],</span> <span class="pre">][H]H:MM:SS[.UUUUUU]</span></code>, where D
is negative for negative <code class="docutils literal notranslate"><span class="pre">t</span></code>. (5)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">repr(t)</span></code></td>
<td>Returns a string in the form
<code class="docutils literal notranslate"><span class="pre">datetime.timedelta(D[,</span> <span class="pre">S[,</span> <span class="pre">U]])</span></code>, where D
is negative for negative <code class="docutils literal notranslate"><span class="pre">t</span></code>. (5)</td>
</tr>
</tbody>
</table>
<p>Notes:</p>
<ol class="arabic">
<li><p class="first">This is exact, but may overflow.</p>
</li>
<li><p class="first">This is exact, and cannot overflow.</p>
</li>
<li><p class="first">Division by 0 raises <a class="reference internal" href="exceptions.html#exceptions.ZeroDivisionError" title="exceptions.ZeroDivisionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZeroDivisionError</span></code></a>.</p>
</li>
<li><p class="first">-<em>timedelta.max</em> is not representable as a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object.</p>
</li>
<li><p class="first">String representations of <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> objects are normalized
similarly to their internal representation. This leads to somewhat
unusual results for negative timedeltas. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=-</span><span class="mi">5</span><span class="p">)</span>
<span class="go">datetime.timedelta(-1, 68400)</span>
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">_</span><span class="p">)</span>
<span class="go">-1 day, 19:00:00</span>
</pre></div>
</div>
</li>
</ol>
<p>In addition to the operations listed above <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> objects support
certain additions and subtractions with <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> and <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>
objects (see below).</p>
<p>Comparisons of <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> objects are supported with the
<a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object representing the smaller duration considered to be the
smaller timedelta. In order to stop mixed-type comparisons from falling back to
the default comparison by object address, when a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object is
compared to an object of a different type, <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised unless the
comparison is <code class="docutils literal notranslate"><span class="pre">==</span></code> or <code class="docutils literal notranslate"><span class="pre">!=</span></code>. The latter cases return <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a> or
<a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>, respectively.</p>
<p><a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> objects are <a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a> (usable as dictionary keys), support
efficient pickling, and in Boolean contexts, a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object is
considered to be true if and only if it isn’t equal to <code class="docutils literal notranslate"><span class="pre">timedelta(0)</span></code>.</p>
<p>Instance methods:</p>
<dl class="method">
<dt id="datetime.timedelta.total_seconds">
<code class="descclassname">timedelta.</code><code class="descname">total_seconds</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.timedelta.total_seconds" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the total number of seconds contained in the duration.
Equivalent to <code class="docutils literal notranslate"><span class="pre">(td.microseconds</span> <span class="pre">+</span> <span class="pre">(td.seconds</span> <span class="pre">+</span> <span class="pre">td.days</span> <span class="pre">*</span> <span class="pre">24</span> <span class="pre">*</span>
<span class="pre">3600)</span> <span class="pre">*</span> <span class="pre">10**6)</span> <span class="pre">/</span> <span class="pre">10**6</span></code> computed with true division enabled.</p>
<p>Note that for very large time intervals (greater than 270 years on
most platforms) this method will lose microsecond accuracy.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.7.</span></p>
</div>
</dd></dl>
<p>Example usage:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">timedelta</span>
<span class="gp">>>> </span><span class="n">year</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">365</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">another_year</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">weeks</span><span class="o">=</span><span class="mi">40</span><span class="p">,</span> <span class="n">days</span><span class="o">=</span><span class="mi">84</span><span class="p">,</span> <span class="n">hours</span><span class="o">=</span><span class="mi">23</span><span class="p">,</span>
<span class="gp">... </span> <span class="n">minutes</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">seconds</span><span class="o">=</span><span class="mi">600</span><span class="p">)</span> <span class="c1"># adds up to 365 days</span>
<span class="gp">>>> </span><span class="n">year</span><span class="o">.</span><span class="n">total_seconds</span><span class="p">()</span>
<span class="go">31536000.0</span>
<span class="gp">>>> </span><span class="n">year</span> <span class="o">==</span> <span class="n">another_year</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">ten_years</span> <span class="o">=</span> <span class="mi">10</span> <span class="o">*</span> <span class="n">year</span>
<span class="gp">>>> </span><span class="n">ten_years</span><span class="p">,</span> <span class="n">ten_years</span><span class="o">.</span><span class="n">days</span> <span class="o">//</span> <span class="mi">365</span>
<span class="go">(datetime.timedelta(3650), 10)</span>
<span class="gp">>>> </span><span class="n">nine_years</span> <span class="o">=</span> <span class="n">ten_years</span> <span class="o">-</span> <span class="n">year</span>
<span class="gp">>>> </span><span class="n">nine_years</span><span class="p">,</span> <span class="n">nine_years</span><span class="o">.</span><span class="n">days</span> <span class="o">//</span> <span class="mi">365</span>
<span class="go">(datetime.timedelta(3285), 9)</span>
<span class="gp">>>> </span><span class="n">three_years</span> <span class="o">=</span> <span class="n">nine_years</span> <span class="o">//</span> <span class="mi">3</span><span class="p">;</span>
<span class="gp">>>> </span><span class="n">three_years</span><span class="p">,</span> <span class="n">three_years</span><span class="o">.</span><span class="n">days</span> <span class="o">//</span> <span class="mi">365</span>
<span class="go">(datetime.timedelta(1095), 3)</span>
<span class="gp">>>> </span><span class="nb">abs</span><span class="p">(</span><span class="n">three_years</span> <span class="o">-</span> <span class="n">ten_years</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">three_years</span> <span class="o">+</span> <span class="n">year</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="date-objects">
<span id="datetime-date"></span><h2>8.1.3. <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> Objects<a class="headerlink" href="#date-objects" title="Permalink to this headline">¶</a></h2>
<p>A <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> object represents a date (year, month and day) in an idealized
calendar, the current Gregorian calendar indefinitely extended in both
directions. January 1 of year 1 is called day number 1, January 2 of year 1 is
called day number 2, and so on. This matches the definition of the “proleptic
Gregorian” calendar in Dershowitz and Reingold’s book Calendrical Calculations,
where it’s the base calendar for all computations. See the book for algorithms
for converting between proleptic Gregorian ordinals and many other calendar
systems.</p>
<dl class="class">
<dt id="datetime.date">
<em class="property">class </em><code class="descclassname">datetime.</code><code class="descname">date</code><span class="sig-paren">(</span><em>year</em>, <em>month</em>, <em>day</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date" title="Permalink to this definition">¶</a></dt>
<dd><p>All arguments are required. Arguments may be ints or longs, in the following
ranges:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">MINYEAR</span> <span class="pre"><=</span> <span class="pre">year</span> <span class="pre"><=</span> <span class="pre">MAXYEAR</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre"><=</span> <span class="pre">month</span> <span class="pre"><=</span> <span class="pre">12</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre"><=</span> <span class="pre">day</span> <span class="pre"><=</span> <span class="pre">number</span> <span class="pre">of</span> <span class="pre">days</span> <span class="pre">in</span> <span class="pre">the</span> <span class="pre">given</span> <span class="pre">month</span> <span class="pre">and</span> <span class="pre">year</span></code></li>
</ul>
<p>If an argument outside those ranges is given, <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.</p>
</dd></dl>
<p>Other constructors, all class methods:</p>
<dl class="classmethod">
<dt id="datetime.date.today">
<em class="property">classmethod </em><code class="descclassname">date.</code><code class="descname">today</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.today" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the current local date. This is equivalent to
<code class="docutils literal notranslate"><span class="pre">date.fromtimestamp(time.time())</span></code>.</p>
</dd></dl>
<dl class="classmethod">
<dt id="datetime.date.fromtimestamp">
<em class="property">classmethod </em><code class="descclassname">date.</code><code class="descname">fromtimestamp</code><span class="sig-paren">(</span><em>timestamp</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.fromtimestamp" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the local date corresponding to the POSIX timestamp, such as is returned
by <a class="reference internal" href="time.html#time.time" title="time.time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.time()</span></code></a>. This may raise <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>, if the timestamp is out
of the range of values supported by the platform C <code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> function.
It’s common for this to be restricted to years from 1970 through 2038. Note
that on non-POSIX systems that include leap seconds in their notion of a
timestamp, leap seconds are ignored by <a class="reference internal" href="#datetime.date.fromtimestamp" title="datetime.date.fromtimestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromtimestamp()</span></code></a>.</p>
</dd></dl>
<dl class="classmethod">
<dt id="datetime.date.fromordinal">
<em class="property">classmethod </em><code class="descclassname">date.</code><code class="descname">fromordinal</code><span class="sig-paren">(</span><em>ordinal</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.fromordinal" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the date corresponding to the proleptic Gregorian ordinal, where January
1 of year 1 has ordinal 1. <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised unless <code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre"><=</span> <span class="pre">ordinal</span> <span class="pre"><=</span>
<span class="pre">date.max.toordinal()</span></code>. For any date <em>d</em>, <code class="docutils literal notranslate"><span class="pre">date.fromordinal(d.toordinal())</span> <span class="pre">==</span>
<span class="pre">d</span></code>.</p>
</dd></dl>
<p>Class attributes:</p>
<dl class="attribute">
<dt id="datetime.date.min">
<code class="descclassname">date.</code><code class="descname">min</code><a class="headerlink" href="#datetime.date.min" title="Permalink to this definition">¶</a></dt>
<dd><p>The earliest representable date, <code class="docutils literal notranslate"><span class="pre">date(MINYEAR,</span> <span class="pre">1,</span> <span class="pre">1)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.date.max">
<code class="descclassname">date.</code><code class="descname">max</code><a class="headerlink" href="#datetime.date.max" title="Permalink to this definition">¶</a></dt>
<dd><p>The latest representable date, <code class="docutils literal notranslate"><span class="pre">date(MAXYEAR,</span> <span class="pre">12,</span> <span class="pre">31)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.date.resolution">
<code class="descclassname">date.</code><code class="descname">resolution</code><a class="headerlink" href="#datetime.date.resolution" title="Permalink to this definition">¶</a></dt>
<dd><p>The smallest possible difference between non-equal date objects,
<code class="docutils literal notranslate"><span class="pre">timedelta(days=1)</span></code>.</p>
</dd></dl>
<p>Instance attributes (read-only):</p>
<dl class="attribute">
<dt id="datetime.date.year">
<code class="descclassname">date.</code><code class="descname">year</code><a class="headerlink" href="#datetime.date.year" title="Permalink to this definition">¶</a></dt>
<dd><p>Between <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MINYEAR</span></code></a> and <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MAXYEAR</span></code></a> inclusive.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.date.month">
<code class="descclassname">date.</code><code class="descname">month</code><a class="headerlink" href="#datetime.date.month" title="Permalink to this definition">¶</a></dt>
<dd><p>Between 1 and 12 inclusive.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.date.day">
<code class="descclassname">date.</code><code class="descname">day</code><a class="headerlink" href="#datetime.date.day" title="Permalink to this definition">¶</a></dt>
<dd><p>Between 1 and the number of days in the given month of the given year.</p>
</dd></dl>
<p>Supported operations:</p>
<table border="1" class="docutils">
<colgroup>
<col width="40%" />
<col width="60%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Operation</th>
<th class="head">Result</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">date2</span> <span class="pre">=</span> <span class="pre">date1</span> <span class="pre">+</span> <span class="pre">timedelta</span></code></td>
<td><em>date2</em> is <code class="docutils literal notranslate"><span class="pre">timedelta.days</span></code> days removed
from <em>date1</em>. (1)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">date2</span> <span class="pre">=</span> <span class="pre">date1</span> <span class="pre">-</span> <span class="pre">timedelta</span></code></td>
<td>Computes <em>date2</em> such that <code class="docutils literal notranslate"><span class="pre">date2</span> <span class="pre">+</span>
<span class="pre">timedelta</span> <span class="pre">==</span> <span class="pre">date1</span></code>. (2)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">timedelta</span> <span class="pre">=</span> <span class="pre">date1</span> <span class="pre">-</span> <span class="pre">date2</span></code></td>
<td>(3)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">date1</span> <span class="pre"><</span> <span class="pre">date2</span></code></td>
<td><em>date1</em> is considered less than <em>date2</em> when
<em>date1</em> precedes <em>date2</em> in time. (4)</td>
</tr>
</tbody>
</table>
<p>Notes:</p>
<ol class="arabic simple">
<li><em>date2</em> is moved forward in time if <code class="docutils literal notranslate"><span class="pre">timedelta.days</span> <span class="pre">></span> <span class="pre">0</span></code>, or backward if
<code class="docutils literal notranslate"><span class="pre">timedelta.days</span> <span class="pre"><</span> <span class="pre">0</span></code>. Afterward <code class="docutils literal notranslate"><span class="pre">date2</span> <span class="pre">-</span> <span class="pre">date1</span> <span class="pre">==</span> <span class="pre">timedelta.days</span></code>.
<code class="docutils literal notranslate"><span class="pre">timedelta.seconds</span></code> and <code class="docutils literal notranslate"><span class="pre">timedelta.microseconds</span></code> are ignored.
<a class="reference internal" href="exceptions.html#exceptions.OverflowError" title="exceptions.OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> is raised if <code class="docutils literal notranslate"><span class="pre">date2.year</span></code> would be smaller than
<a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MINYEAR</span></code></a> or larger than <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MAXYEAR</span></code></a>.</li>
<li>This isn’t quite equivalent to date1 + (-timedelta), because -timedelta in
isolation can overflow in cases where date1 - timedelta does not.
<code class="docutils literal notranslate"><span class="pre">timedelta.seconds</span></code> and <code class="docutils literal notranslate"><span class="pre">timedelta.microseconds</span></code> are ignored.</li>
<li>This is exact, and cannot overflow. timedelta.seconds and
timedelta.microseconds are 0, and date2 + timedelta == date1 after.</li>
<li>In other words, <code class="docutils literal notranslate"><span class="pre">date1</span> <span class="pre"><</span> <span class="pre">date2</span></code> if and only if <code class="docutils literal notranslate"><span class="pre">date1.toordinal()</span> <span class="pre"><</span>
<span class="pre">date2.toordinal()</span></code>. In order to stop comparison from falling back to the
default scheme of comparing object addresses, date comparison normally raises
<a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if the other comparand isn’t also a <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> object.
However, <code class="docutils literal notranslate"><span class="pre">NotImplemented</span></code> is returned instead if the other comparand has a
<code class="xref py py-meth docutils literal notranslate"><span class="pre">timetuple()</span></code> attribute. This hook gives other kinds of date objects a
chance at implementing mixed-type comparison. If not, when a <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a>
object is compared to an object of a different type, <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised
unless the comparison is <code class="docutils literal notranslate"><span class="pre">==</span></code> or <code class="docutils literal notranslate"><span class="pre">!=</span></code>. The latter cases return
<a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a> or <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>, respectively.</li>
</ol>
<p>Dates can be used as dictionary keys. In Boolean contexts, all <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a>
objects are considered to be true.</p>
<p>Instance methods:</p>
<dl class="method">
<dt id="datetime.date.replace">
<code class="descclassname">date.</code><code class="descname">replace</code><span class="sig-paren">(</span><em>year</em>, <em>month</em>, <em>day</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.replace" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a date with the same value, except for those parameters given new
values by whichever keyword arguments are specified. For example, if <code class="docutils literal notranslate"><span class="pre">d</span> <span class="pre">==</span>
<span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">31)</span></code>, then <code class="docutils literal notranslate"><span class="pre">d.replace(day=26)</span> <span class="pre">==</span> <span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">26)</span></code>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.date.timetuple">
<code class="descclassname">date.</code><code class="descname">timetuple</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.timetuple" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a <a class="reference internal" href="time.html#time.struct_time" title="time.struct_time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time.struct_time</span></code></a> such as returned by <a class="reference internal" href="time.html#time.localtime" title="time.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.localtime()</span></code></a>.
The hours, minutes and seconds are 0, and the DST flag is -1. <code class="docutils literal notranslate"><span class="pre">d.timetuple()</span></code>
is equivalent to <code class="docutils literal notranslate"><span class="pre">time.struct_time((d.year,</span> <span class="pre">d.month,</span> <span class="pre">d.day,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">0,</span>
<span class="pre">d.weekday(),</span> <span class="pre">yday,</span> <span class="pre">-1))</span></code>, where <code class="docutils literal notranslate"><span class="pre">yday</span> <span class="pre">=</span> <span class="pre">d.toordinal()</span> <span class="pre">-</span> <span class="pre">date(d.year,</span> <span class="pre">1,</span>
<span class="pre">1).toordinal()</span> <span class="pre">+</span> <span class="pre">1</span></code> is the day number within the current year starting with
<code class="docutils literal notranslate"><span class="pre">1</span></code> for January 1st.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.date.toordinal">
<code class="descclassname">date.</code><code class="descname">toordinal</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.toordinal" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the proleptic Gregorian ordinal of the date, where January 1 of year 1
has ordinal 1. For any <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> object <em>d</em>,
<code class="docutils literal notranslate"><span class="pre">date.fromordinal(d.toordinal())</span> <span class="pre">==</span> <span class="pre">d</span></code>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.date.weekday">
<code class="descclassname">date.</code><code class="descname">weekday</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.weekday" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the day of the week as an integer, where Monday is 0 and Sunday is 6.
For example, <code class="docutils literal notranslate"><span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">4).weekday()</span> <span class="pre">==</span> <span class="pre">2</span></code>, a Wednesday. See also
<a class="reference internal" href="#datetime.date.isoweekday" title="datetime.date.isoweekday"><code class="xref py py-meth docutils literal notranslate"><span class="pre">isoweekday()</span></code></a>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.date.isoweekday">
<code class="descclassname">date.</code><code class="descname">isoweekday</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.isoweekday" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the day of the week as an integer, where Monday is 1 and Sunday is 7.
For example, <code class="docutils literal notranslate"><span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">4).isoweekday()</span> <span class="pre">==</span> <span class="pre">3</span></code>, a Wednesday. See also
<a class="reference internal" href="#datetime.date.weekday" title="datetime.date.weekday"><code class="xref py py-meth docutils literal notranslate"><span class="pre">weekday()</span></code></a>, <a class="reference internal" href="#datetime.date.isocalendar" title="datetime.date.isocalendar"><code class="xref py py-meth docutils literal notranslate"><span class="pre">isocalendar()</span></code></a>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.date.isocalendar">
<code class="descclassname">date.</code><code class="descname">isocalendar</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.isocalendar" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a 3-tuple, (ISO year, ISO week number, ISO weekday).</p>
<p>The ISO calendar is a widely used variant of the Gregorian calendar. See
<a class="reference external" href="https://www.staff.science.uu.nl/~gent0113/calendar/isocalendar.htm">https://www.staff.science.uu.nl/~gent0113/calendar/isocalendar.htm</a> for a good
explanation.</p>
<p>The ISO year consists of 52 or 53 full weeks, and where a week starts on a
Monday and ends on a Sunday. The first week of an ISO year is the first
(Gregorian) calendar week of a year containing a Thursday. This is called week
number 1, and the ISO year of that Thursday is the same as its Gregorian year.</p>
<p>For example, 2004 begins on a Thursday, so the first week of ISO year 2004
begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan 2004, so that
<code class="docutils literal notranslate"><span class="pre">date(2003,</span> <span class="pre">12,</span> <span class="pre">29).isocalendar()</span> <span class="pre">==</span> <span class="pre">(2004,</span> <span class="pre">1,</span> <span class="pre">1)</span></code> and <code class="docutils literal notranslate"><span class="pre">date(2004,</span> <span class="pre">1,</span>
<span class="pre">4).isocalendar()</span> <span class="pre">==</span> <span class="pre">(2004,</span> <span class="pre">1,</span> <span class="pre">7)</span></code>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.date.isoformat">
<code class="descclassname">date.</code><code class="descname">isoformat</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.isoformat" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a string representing the date in ISO 8601 format, ‘YYYY-MM-DD’. For
example, <code class="docutils literal notranslate"><span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">4).isoformat()</span> <span class="pre">==</span> <span class="pre">'2002-12-04'</span></code>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.date.__str__">
<code class="descclassname">date.</code><code class="descname">__str__</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.__str__" title="Permalink to this definition">¶</a></dt>
<dd><p>For a date <em>d</em>, <code class="docutils literal notranslate"><span class="pre">str(d)</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">d.isoformat()</span></code>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.date.ctime">
<code class="descclassname">date.</code><code class="descname">ctime</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.ctime" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a string representing the date, for example <code class="docutils literal notranslate"><span class="pre">date(2002,</span> <span class="pre">12,</span>
<span class="pre">4).ctime()</span> <span class="pre">==</span> <span class="pre">'Wed</span> <span class="pre">Dec</span> <span class="pre">4</span> <span class="pre">00:00:00</span> <span class="pre">2002'</span></code>. <code class="docutils literal notranslate"><span class="pre">d.ctime()</span></code> is equivalent to
<code class="docutils literal notranslate"><span class="pre">time.ctime(time.mktime(d.timetuple()))</span></code> on platforms where the native C
<code class="xref c c-func docutils literal notranslate"><span class="pre">ctime()</span></code> function (which <a class="reference internal" href="time.html#time.ctime" title="time.ctime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.ctime()</span></code></a> invokes, but which
<a class="reference internal" href="#datetime.date.ctime" title="datetime.date.ctime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">date.ctime()</span></code></a> does not invoke) conforms to the C standard.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.date.strftime">
<code class="descclassname">date.</code><code class="descname">strftime</code><span class="sig-paren">(</span><em>format</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.strftime" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a string representing the date, controlled by an explicit format string.
Format codes referring to hours, minutes or seconds will see 0 values. For a
complete list of formatting directives, see section
<a class="reference internal" href="#strftime-strptime-behavior"><span class="std std-ref">strftime() and strptime() Behavior</span></a>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.date.__format__">
<code class="descclassname">date.</code><code class="descname">__format__</code><span class="sig-paren">(</span><em>format</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.__format__" title="Permalink to this definition">¶</a></dt>
<dd><p>Same as <a class="reference internal" href="#datetime.date.strftime" title="datetime.date.strftime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">date.strftime()</span></code></a>. This makes it possible to specify a format
string for a <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> object when using <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a>.
See section <a class="reference internal" href="#strftime-strptime-behavior"><span class="std std-ref">strftime() and strptime() Behavior</span></a>.</p>
</dd></dl>
<p>Example of counting days to an event:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">time</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">date</span>
<span class="gp">>>> </span><span class="n">today</span> <span class="o">=</span> <span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">today</span>
<span class="go">datetime.date(2007, 12, 5)</span>
<span class="gp">>>> </span><span class="n">today</span> <span class="o">==</span> <span class="n">date</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">my_birthday</span> <span class="o">=</span> <span class="n">date</span><span class="p">(</span><span class="n">today</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">24</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">if</span> <span class="n">my_birthday</span> <span class="o"><</span> <span class="n">today</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">my_birthday</span> <span class="o">=</span> <span class="n">my_birthday</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">year</span><span class="o">=</span><span class="n">today</span><span class="o">.</span><span class="n">year</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">my_birthday</span>
<span class="go">datetime.date(2008, 6, 24)</span>
<span class="gp">>>> </span><span class="n">time_to_birthday</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">my_birthday</span> <span class="o">-</span> <span class="n">today</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">time_to_birthday</span><span class="o">.</span><span class="n">days</span>
<span class="go">202</span>
</pre></div>
</div>
<p>Example of working with <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a>:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">date</span>
<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">date</span><span class="o">.</span><span class="n">fromordinal</span><span class="p">(</span><span class="mi">730920</span><span class="p">)</span> <span class="c1"># 730920th day after 1. 1. 0001</span>
<span class="gp">>>> </span><span class="n">d</span>
<span class="go">datetime.date(2002, 3, 11)</span>
<span class="gp">>>> </span><span class="n">t</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">timetuple</span><span class="p">()</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">t</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">print</span> <span class="n">i</span>
<span class="go">2002 # year</span>
<span class="go">3 # month</span>
<span class="go">11 # day</span>
<span class="go">0</span>
<span class="go">0</span>
<span class="go">0</span>
<span class="go">0 # weekday (0 = Monday)</span>
<span class="go">70 # 70th day in the year</span>
<span class="go">-1</span>
<span class="gp">>>> </span><span class="n">ic</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">isocalendar</span><span class="p">()</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">ic</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">print</span> <span class="n">i</span>
<span class="go">2002 # ISO year</span>
<span class="go">11 # ISO week number</span>
<span class="go">1 # ISO day number ( 1 = Monday )</span>
<span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">isoformat</span><span class="p">()</span>
<span class="go">'2002-03-11'</span>
<span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">"</span><span class="si">%d</span><span class="s2">/%m/%y"</span><span class="p">)</span>
<span class="go">'11/03/02'</span>
<span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">"%A </span><span class="si">%d</span><span class="s2">. %B %Y"</span><span class="p">)</span>
<span class="go">'Monday 11. March 2002'</span>
<span class="gp">>>> </span><span class="s1">'The {1} is {0:</span><span class="si">%d</span><span class="s1">}, the {2} is {0:%B}.'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="s2">"day"</span><span class="p">,</span> <span class="s2">"month"</span><span class="p">)</span>
<span class="go">'The day is 11, the month is March.'</span>
</pre></div>
</div>
</div>
<div class="section" id="datetime-objects">
<span id="datetime-datetime"></span><h2>8.1.4. <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> Objects<a class="headerlink" href="#datetime-objects" title="Permalink to this headline">¶</a></h2>
<p>A <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object is a single object containing all the information
from a <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> object and a <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> object. Like a <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a>
object, <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> assumes the current Gregorian calendar extended in
both directions; like a time object, <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> assumes there are exactly
3600*24 seconds in every day.</p>
<p>Constructor:</p>
<dl class="class">
<dt id="datetime.datetime">
<em class="property">class </em><code class="descclassname">datetime.</code><code class="descname">datetime</code><span class="sig-paren">(</span><em>year</em>, <em>month</em>, <em>day</em><span class="optional">[</span>, <em>hour</em><span class="optional">[</span>, <em>minute</em><span class="optional">[</span>, <em>second</em><span class="optional">[</span>, <em>microsecond</em><span class="optional">[</span>, <em>tzinfo</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime" title="Permalink to this definition">¶</a></dt>
<dd><p>The year, month and day arguments are required. <em>tzinfo</em> may be <code class="docutils literal notranslate"><span class="pre">None</span></code>, or an
instance of a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> subclass. The remaining arguments may be ints or
longs, in the following ranges:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">MINYEAR</span> <span class="pre"><=</span> <span class="pre">year</span> <span class="pre"><=</span> <span class="pre">MAXYEAR</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre"><=</span> <span class="pre">month</span> <span class="pre"><=</span> <span class="pre">12</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre"><=</span> <span class="pre">day</span> <span class="pre"><=</span> <span class="pre">number</span> <span class="pre">of</span> <span class="pre">days</span> <span class="pre">in</span> <span class="pre">the</span> <span class="pre">given</span> <span class="pre">month</span> <span class="pre">and</span> <span class="pre">year</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">hour</span> <span class="pre"><</span> <span class="pre">24</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">minute</span> <span class="pre"><</span> <span class="pre">60</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">second</span> <span class="pre"><</span> <span class="pre">60</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">microsecond</span> <span class="pre"><</span> <span class="pre">1000000</span></code></li>
</ul>
<p>If an argument outside those ranges is given, <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.</p>
</dd></dl>
<p>Other constructors, all class methods:</p>
<dl class="classmethod">
<dt id="datetime.datetime.today">
<em class="property">classmethod </em><code class="descclassname">datetime.</code><code class="descname">today</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.today" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the current local datetime, with <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> <code class="docutils literal notranslate"><span class="pre">None</span></code>. This is
equivalent to <code class="docutils literal notranslate"><span class="pre">datetime.fromtimestamp(time.time())</span></code>. See also <a class="reference internal" href="#datetime.datetime.now" title="datetime.datetime.now"><code class="xref py py-meth docutils literal notranslate"><span class="pre">now()</span></code></a>,
<a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromtimestamp()</span></code></a>.</p>
</dd></dl>
<dl class="classmethod">
<dt id="datetime.datetime.now">
<em class="property">classmethod </em><code class="descclassname">datetime.</code><code class="descname">now</code><span class="sig-paren">(</span><span class="optional">[</span><em>tz</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.now" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the current local date and time. If optional argument <em>tz</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>
or not specified, this is like <a class="reference internal" href="#datetime.datetime.today" title="datetime.datetime.today"><code class="xref py py-meth docutils literal notranslate"><span class="pre">today()</span></code></a>, but, if possible, supplies more
precision than can be gotten from going through a <a class="reference internal" href="time.html#time.time" title="time.time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.time()</span></code></a> timestamp
(for example, this may be possible on platforms supplying the C
<code class="xref c c-func docutils literal notranslate"><span class="pre">gettimeofday()</span></code> function).</p>
<p>If <em>tz</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, it must be an instance of a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> subclass, and the
current date and time are converted to <em>tz</em>’s time zone. In this case the
result is equivalent to <code class="docutils literal notranslate"><span class="pre">tz.fromutc(datetime.utcnow().replace(tzinfo=tz))</span></code>.
See also <a class="reference internal" href="#datetime.datetime.today" title="datetime.datetime.today"><code class="xref py py-meth docutils literal notranslate"><span class="pre">today()</span></code></a>, <a class="reference internal" href="#datetime.datetime.utcnow" title="datetime.datetime.utcnow"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcnow()</span></code></a>.</p>
</dd></dl>
<dl class="classmethod">
<dt id="datetime.datetime.utcnow">
<em class="property">classmethod </em><code class="descclassname">datetime.</code><code class="descname">utcnow</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.utcnow" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the current UTC date and time, with <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> <code class="docutils literal notranslate"><span class="pre">None</span></code>. This is like
<a class="reference internal" href="#datetime.datetime.now" title="datetime.datetime.now"><code class="xref py py-meth docutils literal notranslate"><span class="pre">now()</span></code></a>, but returns the current UTC date and time, as a naive
<a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object. See also <a class="reference internal" href="#datetime.datetime.now" title="datetime.datetime.now"><code class="xref py py-meth docutils literal notranslate"><span class="pre">now()</span></code></a>.</p>
</dd></dl>
<dl class="classmethod">
<dt id="datetime.datetime.fromtimestamp">
<em class="property">classmethod </em><code class="descclassname">datetime.</code><code class="descname">fromtimestamp</code><span class="sig-paren">(</span><em>timestamp</em><span class="optional">[</span>, <em>tz</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.fromtimestamp" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the local date and time corresponding to the POSIX timestamp, such as is
returned by <a class="reference internal" href="time.html#time.time" title="time.time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.time()</span></code></a>. If optional argument <em>tz</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> or not
specified, the timestamp is converted to the platform’s local date and time, and
the returned <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object is naive.</p>
<p>If <em>tz</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, it must be an instance of a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> subclass, and the
timestamp is converted to <em>tz</em>’s time zone. In this case the result is
equivalent to
<code class="docutils literal notranslate"><span class="pre">tz.fromutc(datetime.utcfromtimestamp(timestamp).replace(tzinfo=tz))</span></code>.</p>
<p><a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromtimestamp()</span></code></a> may raise <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>, if the timestamp is out of
the range of values supported by the platform C <code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> or
<code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code> functions. It’s common for this to be restricted to years in
1970 through 2038. Note that on non-POSIX systems that include leap seconds in
their notion of a timestamp, leap seconds are ignored by <a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromtimestamp()</span></code></a>,
and then it’s possible to have two timestamps differing by a second that yield
identical <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> objects. See also <a class="reference internal" href="#datetime.datetime.utcfromtimestamp" title="datetime.datetime.utcfromtimestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcfromtimestamp()</span></code></a>.</p>
</dd></dl>
<dl class="classmethod">
<dt id="datetime.datetime.utcfromtimestamp">
<em class="property">classmethod </em><code class="descclassname">datetime.</code><code class="descname">utcfromtimestamp</code><span class="sig-paren">(</span><em>timestamp</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.utcfromtimestamp" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the UTC <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> corresponding to the POSIX timestamp, with
<a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> <code class="docutils literal notranslate"><span class="pre">None</span></code>. This may raise <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>, if the timestamp is
out of the range of values supported by the platform C <code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code> function.
It’s common for this to be restricted to years in 1970 through 2038. See also
<a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromtimestamp()</span></code></a>.</p>
</dd></dl>
<dl class="classmethod">
<dt id="datetime.datetime.fromordinal">
<em class="property">classmethod </em><code class="descclassname">datetime.</code><code class="descname">fromordinal</code><span class="sig-paren">(</span><em>ordinal</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.fromordinal" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> corresponding to the proleptic Gregorian ordinal,
where January 1 of year 1 has ordinal 1. <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised unless <code class="docutils literal notranslate"><span class="pre">1</span>
<span class="pre"><=</span> <span class="pre">ordinal</span> <span class="pre"><=</span> <span class="pre">datetime.max.toordinal()</span></code>. The hour, minute, second and
microsecond of the result are all 0, and <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> is <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
</dd></dl>
<dl class="classmethod">
<dt id="datetime.datetime.combine">
<em class="property">classmethod </em><code class="descclassname">datetime.</code><code class="descname">combine</code><span class="sig-paren">(</span><em>date</em>, <em>time</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.combine" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a new <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object whose date components are equal to the
given <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> object’s, and whose time components and <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a>
attributes are equal to the given <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> object’s. For any
<a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object <em>d</em>,
<code class="docutils literal notranslate"><span class="pre">d</span> <span class="pre">==</span> <span class="pre">datetime.combine(d.date(),</span> <span class="pre">d.timetz())</span></code>. If date is a
<a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object, its time components and <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> attributes
are ignored.</p>
</dd></dl>
<dl class="classmethod">
<dt id="datetime.datetime.strptime">
<em class="property">classmethod </em><code class="descclassname">datetime.</code><code class="descname">strptime</code><span class="sig-paren">(</span><em>date_string</em>, <em>format</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.strptime" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> corresponding to <em>date_string</em>, parsed according to
<em>format</em>. This is equivalent to <code class="docutils literal notranslate"><span class="pre">datetime(*(time.strptime(date_string,</span>
<span class="pre">format)[0:6]))</span></code>. <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised if the date_string and format
can’t be parsed by <a class="reference internal" href="time.html#time.strptime" title="time.strptime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strptime()</span></code></a> or if it returns a value which isn’t a
time tuple. For a complete list of formatting directives, see section
<a class="reference internal" href="#strftime-strptime-behavior"><span class="std std-ref">strftime() and strptime() Behavior</span></a>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.5.</span></p>
</div>
</dd></dl>
<p>Class attributes:</p>
<dl class="attribute">
<dt id="datetime.datetime.min">
<code class="descclassname">datetime.</code><code class="descname">min</code><a class="headerlink" href="#datetime.datetime.min" title="Permalink to this definition">¶</a></dt>
<dd><p>The earliest representable <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>, <code class="docutils literal notranslate"><span class="pre">datetime(MINYEAR,</span> <span class="pre">1,</span> <span class="pre">1,</span>
<span class="pre">tzinfo=None)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.datetime.max">
<code class="descclassname">datetime.</code><code class="descname">max</code><a class="headerlink" href="#datetime.datetime.max" title="Permalink to this definition">¶</a></dt>
<dd><p>The latest representable <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>, <code class="docutils literal notranslate"><span class="pre">datetime(MAXYEAR,</span> <span class="pre">12,</span> <span class="pre">31,</span> <span class="pre">23,</span> <span class="pre">59,</span>
<span class="pre">59,</span> <span class="pre">999999,</span> <span class="pre">tzinfo=None)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.datetime.resolution">
<code class="descclassname">datetime.</code><code class="descname">resolution</code><a class="headerlink" href="#datetime.datetime.resolution" title="Permalink to this definition">¶</a></dt>
<dd><p>The smallest possible difference between non-equal <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> objects,
<code class="docutils literal notranslate"><span class="pre">timedelta(microseconds=1)</span></code>.</p>
</dd></dl>
<p>Instance attributes (read-only):</p>
<dl class="attribute">
<dt id="datetime.datetime.year">
<code class="descclassname">datetime.</code><code class="descname">year</code><a class="headerlink" href="#datetime.datetime.year" title="Permalink to this definition">¶</a></dt>
<dd><p>Between <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MINYEAR</span></code></a> and <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MAXYEAR</span></code></a> inclusive.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.datetime.month">
<code class="descclassname">datetime.</code><code class="descname">month</code><a class="headerlink" href="#datetime.datetime.month" title="Permalink to this definition">¶</a></dt>
<dd><p>Between 1 and 12 inclusive.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.datetime.day">
<code class="descclassname">datetime.</code><code class="descname">day</code><a class="headerlink" href="#datetime.datetime.day" title="Permalink to this definition">¶</a></dt>
<dd><p>Between 1 and the number of days in the given month of the given year.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.datetime.hour">
<code class="descclassname">datetime.</code><code class="descname">hour</code><a class="headerlink" href="#datetime.datetime.hour" title="Permalink to this definition">¶</a></dt>
<dd><p>In <code class="docutils literal notranslate"><span class="pre">range(24)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.datetime.minute">
<code class="descclassname">datetime.</code><code class="descname">minute</code><a class="headerlink" href="#datetime.datetime.minute" title="Permalink to this definition">¶</a></dt>
<dd><p>In <code class="docutils literal notranslate"><span class="pre">range(60)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.datetime.second">
<code class="descclassname">datetime.</code><code class="descname">second</code><a class="headerlink" href="#datetime.datetime.second" title="Permalink to this definition">¶</a></dt>
<dd><p>In <code class="docutils literal notranslate"><span class="pre">range(60)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.datetime.microsecond">
<code class="descclassname">datetime.</code><code class="descname">microsecond</code><a class="headerlink" href="#datetime.datetime.microsecond" title="Permalink to this definition">¶</a></dt>
<dd><p>In <code class="docutils literal notranslate"><span class="pre">range(1000000)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.datetime.tzinfo">
<code class="descclassname">datetime.</code><code class="descname">tzinfo</code><a class="headerlink" href="#datetime.datetime.tzinfo" title="Permalink to this definition">¶</a></dt>
<dd><p>The object passed as the <em>tzinfo</em> argument to the <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> constructor,
or <code class="docutils literal notranslate"><span class="pre">None</span></code> if none was passed.</p>
</dd></dl>
<p>Supported operations:</p>
<table border="1" class="docutils">
<colgroup>
<col width="55%" />
<col width="45%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Operation</th>
<th class="head">Result</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">datetime2</span> <span class="pre">=</span> <span class="pre">datetime1</span> <span class="pre">+</span> <span class="pre">timedelta</span></code></td>
<td>(1)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">datetime2</span> <span class="pre">=</span> <span class="pre">datetime1</span> <span class="pre">-</span> <span class="pre">timedelta</span></code></td>
<td>(2)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">timedelta</span> <span class="pre">=</span> <span class="pre">datetime1</span> <span class="pre">-</span> <span class="pre">datetime2</span></code></td>
<td>(3)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">datetime1</span> <span class="pre"><</span> <span class="pre">datetime2</span></code></td>
<td>Compares <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> to
<a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>. (4)</td>
</tr>
</tbody>
</table>
<ol class="arabic">
<li><p class="first">datetime2 is a duration of timedelta removed from datetime1, moving forward in
time if <code class="docutils literal notranslate"><span class="pre">timedelta.days</span></code> > 0, or backward if <code class="docutils literal notranslate"><span class="pre">timedelta.days</span></code> < 0. The
result has the same <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> attribute as the input datetime, and
datetime2 - datetime1 == timedelta after. <a class="reference internal" href="exceptions.html#exceptions.OverflowError" title="exceptions.OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> is raised if
datetime2.year would be smaller than <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MINYEAR</span></code></a> or larger than
<a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MAXYEAR</span></code></a>. Note that no time zone adjustments are done even if the
input is an aware object.</p>
</li>
<li><p class="first">Computes the datetime2 such that datetime2 + timedelta == datetime1. As for
addition, the result has the same <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> attribute as the input
datetime, and no time zone adjustments are done even if the input is aware.
This isn’t quite equivalent to datetime1 + (-timedelta), because -timedelta
in isolation can overflow in cases where datetime1 - timedelta does not.</p>
</li>
<li><p class="first">Subtraction of a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> from a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> is defined only if
both operands are naive, or if both are aware. If one is aware and the other is
naive, <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised.</p>
<p>If both are naive, or both are aware and have the same <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> attribute,
the <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> attributes are ignored, and the result is a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a>
object <em>t</em> such that <code class="docutils literal notranslate"><span class="pre">datetime2</span> <span class="pre">+</span> <span class="pre">t</span> <span class="pre">==</span> <span class="pre">datetime1</span></code>. No time zone adjustments
are done in this case.</p>
<p>If both are aware and have different <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> attributes, <code class="docutils literal notranslate"><span class="pre">a-b</span></code> acts
as if <em>a</em> and <em>b</em> were first converted to naive UTC datetimes first. The
result is <code class="docutils literal notranslate"><span class="pre">(a.replace(tzinfo=None)</span> <span class="pre">-</span> <span class="pre">a.utcoffset())</span> <span class="pre">-</span> <span class="pre">(b.replace(tzinfo=None)</span>
<span class="pre">-</span> <span class="pre">b.utcoffset())</span></code> except that the implementation never overflows.</p>
</li>
<li><p class="first"><em>datetime1</em> is considered less than <em>datetime2</em> when <em>datetime1</em> precedes
<em>datetime2</em> in time.</p>
<p>If one comparand is naive and the other is aware, <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised.
If both comparands are aware, and have the same <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> attribute, the
common <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> attribute is ignored and the base datetimes are
compared. If both comparands are aware and have different <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a>
attributes, the comparands are first adjusted by subtracting their UTC
offsets (obtained from <code class="docutils literal notranslate"><span class="pre">self.utcoffset()</span></code>).</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">In order to stop comparison from falling back to the default scheme of comparing
object addresses, datetime comparison normally raises <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if the
other comparand isn’t also a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object. However,
<code class="docutils literal notranslate"><span class="pre">NotImplemented</span></code> is returned instead if the other comparand has a
<code class="xref py py-meth docutils literal notranslate"><span class="pre">timetuple()</span></code> attribute. This hook gives other kinds of date objects a
chance at implementing mixed-type comparison. If not, when a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>
object is compared to an object of a different type, <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised
unless the comparison is <code class="docutils literal notranslate"><span class="pre">==</span></code> or <code class="docutils literal notranslate"><span class="pre">!=</span></code>. The latter cases return
<a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a> or <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>, respectively.</p>
</div>
</li>
</ol>
<p><a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> objects can be used as dictionary keys. In Boolean contexts,
all <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> objects are considered to be true.</p>
<p>Instance methods:</p>
<dl class="method">
<dt id="datetime.datetime.date">
<code class="descclassname">datetime.</code><code class="descname">date</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.date" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> object with same year, month and day.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.datetime.time">
<code class="descclassname">datetime.</code><code class="descname">time</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.time" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> object with same hour, minute, second and microsecond.
<a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> is <code class="docutils literal notranslate"><span class="pre">None</span></code>. See also method <a class="reference internal" href="#datetime.datetime.timetz" title="datetime.datetime.timetz"><code class="xref py py-meth docutils literal notranslate"><span class="pre">timetz()</span></code></a>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.datetime.timetz">
<code class="descclassname">datetime.</code><code class="descname">timetz</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.timetz" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> object with same hour, minute, second, microsecond, and
tzinfo attributes. See also method <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><code class="xref py py-meth docutils literal notranslate"><span class="pre">time()</span></code></a>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.datetime.replace">
<code class="descclassname">datetime.</code><code class="descname">replace</code><span class="sig-paren">(</span><span class="optional">[</span><em>year</em><span class="optional">[</span>, <em>month</em><span class="optional">[</span>, <em>day</em><span class="optional">[</span>, <em>hour</em><span class="optional">[</span>, <em>minute</em><span class="optional">[</span>, <em>second</em><span class="optional">[</span>, <em>microsecond</em><span class="optional">[</span>, <em>tzinfo</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.replace" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a datetime with the same attributes, except for those attributes given
new values by whichever keyword arguments are specified. Note that
<code class="docutils literal notranslate"><span class="pre">tzinfo=None</span></code> can be specified to create a naive datetime from an aware
datetime with no conversion of date and time data.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.datetime.astimezone">
<code class="descclassname">datetime.</code><code class="descname">astimezone</code><span class="sig-paren">(</span><em>tz</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.astimezone" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object with new <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> attribute <em>tz</em>,
adjusting the date and time data so the result is the same UTC time as
<em>self</em>, but in <em>tz</em>’s local time.</p>
<p><em>tz</em> must be an instance of a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> subclass, and its
<a class="reference internal" href="#datetime.datetime.utcoffset" title="datetime.datetime.utcoffset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code></a> and <a class="reference internal" href="#datetime.datetime.dst" title="datetime.datetime.dst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dst()</span></code></a> methods must not return <code class="docutils literal notranslate"><span class="pre">None</span></code>. <em>self</em> must
be aware (<code class="docutils literal notranslate"><span class="pre">self.tzinfo</span></code> must not be <code class="docutils literal notranslate"><span class="pre">None</span></code>, and <code class="docutils literal notranslate"><span class="pre">self.utcoffset()</span></code> must
not return <code class="docutils literal notranslate"><span class="pre">None</span></code>).</p>
<p>If <code class="docutils literal notranslate"><span class="pre">self.tzinfo</span></code> is <em>tz</em>, <code class="docutils literal notranslate"><span class="pre">self.astimezone(tz)</span></code> is equal to <em>self</em>: no
adjustment of date or time data is performed. Else the result is local
time in time zone <em>tz</em>, representing the same UTC time as <em>self</em>: after
<code class="docutils literal notranslate"><span class="pre">astz</span> <span class="pre">=</span> <span class="pre">dt.astimezone(tz)</span></code>, <code class="docutils literal notranslate"><span class="pre">astz</span> <span class="pre">-</span> <span class="pre">astz.utcoffset()</span></code> will usually have
the same date and time data as <code class="docutils literal notranslate"><span class="pre">dt</span> <span class="pre">-</span> <span class="pre">dt.utcoffset()</span></code>. The discussion
of class <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> explains the cases at Daylight Saving Time transition
boundaries where this cannot be achieved (an issue only if <em>tz</em> models both
standard and daylight time).</p>
<p>If you merely want to attach a time zone object <em>tz</em> to a datetime <em>dt</em> without
adjustment of date and time data, use <code class="docutils literal notranslate"><span class="pre">dt.replace(tzinfo=tz)</span></code>. If you
merely want to remove the time zone object from an aware datetime <em>dt</em> without
conversion of date and time data, use <code class="docutils literal notranslate"><span class="pre">dt.replace(tzinfo=None)</span></code>.</p>
<p>Note that the default <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tzinfo.fromutc()</span></code></a> method can be overridden in a
<a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> subclass to affect the result returned by <a class="reference internal" href="#datetime.datetime.astimezone" title="datetime.datetime.astimezone"><code class="xref py py-meth docutils literal notranslate"><span class="pre">astimezone()</span></code></a>.
Ignoring error cases, <a class="reference internal" href="#datetime.datetime.astimezone" title="datetime.datetime.astimezone"><code class="xref py py-meth docutils literal notranslate"><span class="pre">astimezone()</span></code></a> acts like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">astimezone</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tz</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">tzinfo</span> <span class="ow">is</span> <span class="n">tz</span><span class="p">:</span>
<span class="k">return</span> <span class="bp">self</span>
<span class="c1"># Convert self to UTC, and attach the new time zone object.</span>
<span class="n">utc</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">utcoffset</span><span class="p">())</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="n">tz</span><span class="p">)</span>
<span class="c1"># Convert from UTC to tz's local time.</span>
<span class="k">return</span> <span class="n">tz</span><span class="o">.</span><span class="n">fromutc</span><span class="p">(</span><span class="n">utc</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="datetime.datetime.utcoffset">
<code class="descclassname">datetime.</code><code class="descname">utcoffset</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.utcoffset" title="Permalink to this definition">¶</a></dt>
<dd><p>If <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, returns <code class="docutils literal notranslate"><span class="pre">None</span></code>, else returns
<code class="docutils literal notranslate"><span class="pre">self.tzinfo.utcoffset(self)</span></code>, and raises an exception if the latter doesn’t
return <code class="docutils literal notranslate"><span class="pre">None</span></code>, or a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object representing a whole number of
minutes with magnitude less than one day.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.datetime.dst">
<code class="descclassname">datetime.</code><code class="descname">dst</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.dst" title="Permalink to this definition">¶</a></dt>
<dd><p>If <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, returns <code class="docutils literal notranslate"><span class="pre">None</span></code>, else returns
<code class="docutils literal notranslate"><span class="pre">self.tzinfo.dst(self)</span></code>, and raises an exception if the latter doesn’t return
<code class="docutils literal notranslate"><span class="pre">None</span></code>, or a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object representing a whole number of minutes
with magnitude less than one day.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.datetime.tzname">
<code class="descclassname">datetime.</code><code class="descname">tzname</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.tzname" title="Permalink to this definition">¶</a></dt>
<dd><p>If <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, returns <code class="docutils literal notranslate"><span class="pre">None</span></code>, else returns
<code class="docutils literal notranslate"><span class="pre">self.tzinfo.tzname(self)</span></code>, raises an exception if the latter doesn’t return
<code class="docutils literal notranslate"><span class="pre">None</span></code> or a string object,</p>
</dd></dl>
<dl class="method">
<dt id="datetime.datetime.timetuple">
<code class="descclassname">datetime.</code><code class="descname">timetuple</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.timetuple" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a <a class="reference internal" href="time.html#time.struct_time" title="time.struct_time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time.struct_time</span></code></a> such as returned by <a class="reference internal" href="time.html#time.localtime" title="time.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.localtime()</span></code></a>.
<code class="docutils literal notranslate"><span class="pre">d.timetuple()</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">time.struct_time((d.year,</span> <span class="pre">d.month,</span> <span class="pre">d.day,</span>
<span class="pre">d.hour,</span> <span class="pre">d.minute,</span> <span class="pre">d.second,</span> <span class="pre">d.weekday(),</span> <span class="pre">yday,</span> <span class="pre">dst))</span></code>, where <code class="docutils literal notranslate"><span class="pre">yday</span> <span class="pre">=</span>
<span class="pre">d.toordinal()</span> <span class="pre">-</span> <span class="pre">date(d.year,</span> <span class="pre">1,</span> <span class="pre">1).toordinal()</span> <span class="pre">+</span> <span class="pre">1</span></code> is the day number within
the current year starting with <code class="docutils literal notranslate"><span class="pre">1</span></code> for January 1st. The <code class="xref py py-attr docutils literal notranslate"><span class="pre">tm_isdst</span></code> flag
of the result is set according to the <a class="reference internal" href="#datetime.datetime.dst" title="datetime.datetime.dst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dst()</span></code></a> method: <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> is
<code class="docutils literal notranslate"><span class="pre">None</span></code> or <a class="reference internal" href="#datetime.datetime.dst" title="datetime.datetime.dst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dst()</span></code></a> returns <code class="docutils literal notranslate"><span class="pre">None</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">tm_isdst</span></code> is set to <code class="docutils literal notranslate"><span class="pre">-1</span></code>;
else if <a class="reference internal" href="#datetime.datetime.dst" title="datetime.datetime.dst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dst()</span></code></a> returns a non-zero value, <code class="xref py py-attr docutils literal notranslate"><span class="pre">tm_isdst</span></code> is set to <code class="docutils literal notranslate"><span class="pre">1</span></code>;
else <code class="xref py py-attr docutils literal notranslate"><span class="pre">tm_isdst</span></code> is set to <code class="docutils literal notranslate"><span class="pre">0</span></code>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.datetime.utctimetuple">
<code class="descclassname">datetime.</code><code class="descname">utctimetuple</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.utctimetuple" title="Permalink to this definition">¶</a></dt>
<dd><p>If <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> instance <em>d</em> is naive, this is the same as
<code class="docutils literal notranslate"><span class="pre">d.timetuple()</span></code> except that <code class="xref py py-attr docutils literal notranslate"><span class="pre">tm_isdst</span></code> is forced to 0 regardless of what
<code class="docutils literal notranslate"><span class="pre">d.dst()</span></code> returns. DST is never in effect for a UTC time.</p>
<p>If <em>d</em> is aware, <em>d</em> is normalized to UTC time, by subtracting
<code class="docutils literal notranslate"><span class="pre">d.utcoffset()</span></code>, and a <a class="reference internal" href="time.html#time.struct_time" title="time.struct_time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time.struct_time</span></code></a> for the normalized time is
returned. <code class="xref py py-attr docutils literal notranslate"><span class="pre">tm_isdst</span></code> is forced to 0. Note that the result’s
<code class="xref py py-attr docutils literal notranslate"><span class="pre">tm_year</span></code> member may be <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MINYEAR</span></code></a>-1 or <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MAXYEAR</span></code></a>+1, if
<em>d</em>.year was <code class="docutils literal notranslate"><span class="pre">MINYEAR</span></code> or <code class="docutils literal notranslate"><span class="pre">MAXYEAR</span></code> and UTC adjustment spills over a year
boundary.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.datetime.toordinal">
<code class="descclassname">datetime.</code><code class="descname">toordinal</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.toordinal" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the proleptic Gregorian ordinal of the date. The same as
<code class="docutils literal notranslate"><span class="pre">self.date().toordinal()</span></code>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.datetime.weekday">
<code class="descclassname">datetime.</code><code class="descname">weekday</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.weekday" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the day of the week as an integer, where Monday is 0 and Sunday is 6.
The same as <code class="docutils literal notranslate"><span class="pre">self.date().weekday()</span></code>. See also <a class="reference internal" href="#datetime.datetime.isoweekday" title="datetime.datetime.isoweekday"><code class="xref py py-meth docutils literal notranslate"><span class="pre">isoweekday()</span></code></a>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.datetime.isoweekday">
<code class="descclassname">datetime.</code><code class="descname">isoweekday</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.isoweekday" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the day of the week as an integer, where Monday is 1 and Sunday is 7.
The same as <code class="docutils literal notranslate"><span class="pre">self.date().isoweekday()</span></code>. See also <a class="reference internal" href="#datetime.datetime.weekday" title="datetime.datetime.weekday"><code class="xref py py-meth docutils literal notranslate"><span class="pre">weekday()</span></code></a>,
<a class="reference internal" href="#datetime.datetime.isocalendar" title="datetime.datetime.isocalendar"><code class="xref py py-meth docutils literal notranslate"><span class="pre">isocalendar()</span></code></a>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.datetime.isocalendar">
<code class="descclassname">datetime.</code><code class="descname">isocalendar</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.isocalendar" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a 3-tuple, (ISO year, ISO week number, ISO weekday). The same as
<code class="docutils literal notranslate"><span class="pre">self.date().isocalendar()</span></code>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.datetime.isoformat">
<code class="descclassname">datetime.</code><code class="descname">isoformat</code><span class="sig-paren">(</span><span class="optional">[</span><em>sep</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.isoformat" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a string representing the date and time in ISO 8601 format,
YYYY-MM-DDTHH:MM:SS.mmmmmm or, if <a class="reference internal" href="#datetime.datetime.microsecond" title="datetime.datetime.microsecond"><code class="xref py py-attr docutils literal notranslate"><span class="pre">microsecond</span></code></a> is 0,
YYYY-MM-DDTHH:MM:SS</p>
<p>If <a class="reference internal" href="#datetime.datetime.utcoffset" title="datetime.datetime.utcoffset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code></a> does not return <code class="docutils literal notranslate"><span class="pre">None</span></code>, a 6-character string is
appended, giving the UTC offset in (signed) hours and minutes:
YYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MM or, if <a class="reference internal" href="#datetime.datetime.microsecond" title="datetime.datetime.microsecond"><code class="xref py py-attr docutils literal notranslate"><span class="pre">microsecond</span></code></a> is 0
YYYY-MM-DDTHH:MM:SS+HH:MM</p>
<p>The optional argument <em>sep</em> (default <code class="docutils literal notranslate"><span class="pre">'T'</span></code>) is a one-character separator,
placed between the date and time portions of the result. For example,</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">tzinfo</span><span class="p">,</span> <span class="n">timedelta</span><span class="p">,</span> <span class="n">datetime</span>
<span class="gp">>>> </span><span class="k">class</span> <span class="nc">TZ</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">minutes</span><span class="o">=-</span><span class="mi">399</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">datetime</span><span class="p">(</span><span class="mi">2002</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">25</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">TZ</span><span class="p">())</span><span class="o">.</span><span class="n">isoformat</span><span class="p">(</span><span class="s1">' '</span><span class="p">)</span>
<span class="go">'2002-12-25 00:00:00-06:39'</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="datetime.datetime.__str__">
<code class="descclassname">datetime.</code><code class="descname">__str__</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.__str__" title="Permalink to this definition">¶</a></dt>
<dd><p>For a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> instance <em>d</em>, <code class="docutils literal notranslate"><span class="pre">str(d)</span></code> is equivalent to
<code class="docutils literal notranslate"><span class="pre">d.isoformat('</span> <span class="pre">')</span></code>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.datetime.ctime">
<code class="descclassname">datetime.</code><code class="descname">ctime</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.ctime" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a string representing the date and time, for example <code class="docutils literal notranslate"><span class="pre">datetime(2002,</span> <span class="pre">12,</span>
<span class="pre">4,</span> <span class="pre">20,</span> <span class="pre">30,</span> <span class="pre">40).ctime()</span> <span class="pre">==</span> <span class="pre">'Wed</span> <span class="pre">Dec</span>  <span class="pre">4</span> <span class="pre">20:30:40</span> <span class="pre">2002'</span></code>. <code class="docutils literal notranslate"><span class="pre">d.ctime()</span></code> is
equivalent to <code class="docutils literal notranslate"><span class="pre">time.ctime(time.mktime(d.timetuple()))</span></code> on platforms where the
native C <code class="xref c c-func docutils literal notranslate"><span class="pre">ctime()</span></code> function (which <a class="reference internal" href="time.html#time.ctime" title="time.ctime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.ctime()</span></code></a> invokes, but which
<a class="reference internal" href="#datetime.datetime.ctime" title="datetime.datetime.ctime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.ctime()</span></code></a> does not invoke) conforms to the C standard.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.datetime.strftime">
<code class="descclassname">datetime.</code><code class="descname">strftime</code><span class="sig-paren">(</span><em>format</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.strftime" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a string representing the date and time, controlled by an explicit format
string. For a complete list of formatting directives, see section
<a class="reference internal" href="#strftime-strptime-behavior"><span class="std std-ref">strftime() and strptime() Behavior</span></a>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.datetime.__format__">
<code class="descclassname">datetime.</code><code class="descname">__format__</code><span class="sig-paren">(</span><em>format</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.__format__" title="Permalink to this definition">¶</a></dt>
<dd><p>Same as <a class="reference internal" href="#datetime.datetime.strftime" title="datetime.datetime.strftime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.strftime()</span></code></a>. This makes it possible to specify a format
string for a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object when using <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a>.
See section <a class="reference internal" href="#strftime-strptime-behavior"><span class="std std-ref">strftime() and strptime() Behavior</span></a>.</p>
</dd></dl>
<p>Examples of working with datetime objects:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span><span class="p">,</span> <span class="n">date</span><span class="p">,</span> <span class="n">time</span>
<span class="gp">>>> </span><span class="c1"># Using datetime.combine()</span>
<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">date</span><span class="p">(</span><span class="mi">2005</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">14</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">t</span> <span class="o">=</span> <span class="n">time</span><span class="p">(</span><span class="mi">12</span><span class="p">,</span> <span class="mi">30</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">datetime</span><span class="o">.</span><span class="n">combine</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span>
<span class="go">datetime.datetime(2005, 7, 14, 12, 30)</span>
<span class="gp">>>> </span><span class="c1"># Using datetime.now() or datetime.utcnow()</span>
<span class="gp">>>> </span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
<span class="go">datetime.datetime(2007, 12, 6, 16, 29, 43, 79043) # GMT +1</span>
<span class="gp">>>> </span><span class="n">datetime</span><span class="o">.</span><span class="n">utcnow</span><span class="p">()</span>
<span class="go">datetime.datetime(2007, 12, 6, 15, 29, 43, 79060)</span>
<span class="gp">>>> </span><span class="c1"># Using datetime.strptime()</span>
<span class="gp">>>> </span><span class="n">dt</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="s2">"21/11/06 16:30"</span><span class="p">,</span> <span class="s2">"</span><span class="si">%d</span><span class="s2">/%m/%y %H:%M"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">dt</span>
<span class="go">datetime.datetime(2006, 11, 21, 16, 30)</span>
<span class="gp">>>> </span><span class="c1"># Using datetime.timetuple() to get tuple of all attributes</span>
<span class="gp">>>> </span><span class="n">tt</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">timetuple</span><span class="p">()</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">it</span> <span class="ow">in</span> <span class="n">tt</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">print</span> <span class="n">it</span>
<span class="gp">...</span>
<span class="go">2006 # year</span>
<span class="go">11 # month</span>
<span class="go">21 # day</span>
<span class="go">16 # hour</span>
<span class="go">30 # minute</span>
<span class="go">0 # second</span>
<span class="go">1 # weekday (0 = Monday)</span>
<span class="go">325 # number of days since 1st January</span>
<span class="go">-1 # dst - method tzinfo.dst() returned None</span>
<span class="gp">>>> </span><span class="c1"># Date in ISO format</span>
<span class="gp">>>> </span><span class="n">ic</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">isocalendar</span><span class="p">()</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">it</span> <span class="ow">in</span> <span class="n">ic</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">print</span> <span class="n">it</span>
<span class="gp">...</span>
<span class="go">2006 # ISO year</span>
<span class="go">47 # ISO week</span>
<span class="go">2 # ISO weekday</span>
<span class="gp">>>> </span><span class="c1"># Formatting datetime</span>
<span class="gp">>>> </span><span class="n">dt</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">"%A, </span><span class="si">%d</span><span class="s2">. %B %Y %I:%M%p"</span><span class="p">)</span>
<span class="go">'Tuesday, 21. November 2006 04:30PM'</span>
<span class="gp">>>> </span><span class="s1">'The {1} is {0:</span><span class="si">%d</span><span class="s1">}, the {2} is {0:%B}, the {3} is {0:%I:%M%p}.'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">dt</span><span class="p">,</span> <span class="s2">"day"</span><span class="p">,</span> <span class="s2">"month"</span><span class="p">,</span> <span class="s2">"time"</span><span class="p">)</span>
<span class="go">'The day is 21, the month is November, the time is 04:30PM.'</span>
</pre></div>
</div>
<p>Using datetime with tzinfo:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">timedelta</span><span class="p">,</span> <span class="n">datetime</span><span class="p">,</span> <span class="n">tzinfo</span>
<span class="gp">>>> </span><span class="k">class</span> <span class="nc">GMT1</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">dst</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="gp">... </span> <span class="c1"># DST starts last Sunday in March</span>
<span class="gp">... </span> <span class="n">d</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c1"># ends last Sunday in October</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">dston</span> <span class="o">=</span> <span class="n">d</span> <span class="o">-</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">weekday</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">d</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">dstoff</span> <span class="o">=</span> <span class="n">d</span> <span class="o">-</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">weekday</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">dston</span> <span class="o"><=</span> <span class="n">dt</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> <span class="o"><</span> <span class="bp">self</span><span class="o">.</span><span class="n">dstoff</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">else</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">dt</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">return</span> <span class="s2">"GMT +1"</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="k">class</span> <span class="nc">GMT2</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">dst</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">d</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">dston</span> <span class="o">=</span> <span class="n">d</span> <span class="o">-</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">weekday</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">d</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">dstoff</span> <span class="o">=</span> <span class="n">d</span> <span class="o">-</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">weekday</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">dston</span> <span class="o"><=</span> <span class="n">dt</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> <span class="o"><</span> <span class="bp">self</span><span class="o">.</span><span class="n">dstoff</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">else</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">dt</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">return</span> <span class="s2">"GMT +2"</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">gmt1</span> <span class="o">=</span> <span class="n">GMT1</span><span class="p">()</span>
<span class="gp">>>> </span><span class="c1"># Daylight Saving Time</span>
<span class="gp">>>> </span><span class="n">dt1</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2006</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">21</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">gmt1</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">dt1</span><span class="o">.</span><span class="n">dst</span><span class="p">()</span>
<span class="go">datetime.timedelta(0)</span>
<span class="gp">>>> </span><span class="n">dt1</span><span class="o">.</span><span class="n">utcoffset</span><span class="p">()</span>
<span class="go">datetime.timedelta(0, 3600)</span>
<span class="gp">>>> </span><span class="n">dt2</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2006</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">gmt1</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">dt2</span><span class="o">.</span><span class="n">dst</span><span class="p">()</span>
<span class="go">datetime.timedelta(0, 3600)</span>
<span class="gp">>>> </span><span class="n">dt2</span><span class="o">.</span><span class="n">utcoffset</span><span class="p">()</span>
<span class="go">datetime.timedelta(0, 7200)</span>
<span class="gp">>>> </span><span class="c1"># Convert datetime to another time zone</span>
<span class="gp">>>> </span><span class="n">dt3</span> <span class="o">=</span> <span class="n">dt2</span><span class="o">.</span><span class="n">astimezone</span><span class="p">(</span><span class="n">GMT2</span><span class="p">())</span>
<span class="gp">>>> </span><span class="n">dt3</span> <span class="c1"># doctest: +ELLIPSIS</span>
<span class="go">datetime.datetime(2006, 6, 14, 14, 0, tzinfo=<GMT2 object at 0x...>)</span>
<span class="gp">>>> </span><span class="n">dt2</span> <span class="c1"># doctest: +ELLIPSIS</span>
<span class="go">datetime.datetime(2006, 6, 14, 13, 0, tzinfo=<GMT1 object at 0x...>)</span>
<span class="gp">>>> </span><span class="n">dt2</span><span class="o">.</span><span class="n">utctimetuple</span><span class="p">()</span> <span class="o">==</span> <span class="n">dt3</span><span class="o">.</span><span class="n">utctimetuple</span><span class="p">()</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="time-objects">
<span id="datetime-time"></span><h2>8.1.5. <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> Objects<a class="headerlink" href="#time-objects" title="Permalink to this headline">¶</a></h2>
<p>A time object represents a (local) time of day, independent of any particular
day, and subject to adjustment via a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> object.</p>
<dl class="class">
<dt id="datetime.time">
<em class="property">class </em><code class="descclassname">datetime.</code><code class="descname">time</code><span class="sig-paren">(</span><span class="optional">[</span><em>hour</em><span class="optional">[</span>, <em>minute</em><span class="optional">[</span>, <em>second</em><span class="optional">[</span>, <em>microsecond</em><span class="optional">[</span>, <em>tzinfo</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.time" title="Permalink to this definition">¶</a></dt>
<dd><p>All arguments are optional. <em>tzinfo</em> may be <code class="docutils literal notranslate"><span class="pre">None</span></code>, or an instance of a
<a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> subclass. The remaining arguments may be ints or longs, in the
following ranges:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">hour</span> <span class="pre"><</span> <span class="pre">24</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">minute</span> <span class="pre"><</span> <span class="pre">60</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">second</span> <span class="pre"><</span> <span class="pre">60</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">microsecond</span> <span class="pre"><</span> <span class="pre">1000000</span></code>.</li>
</ul>
<p>If an argument outside those ranges is given, <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised. All
default to <code class="docutils literal notranslate"><span class="pre">0</span></code> except <em>tzinfo</em>, which defaults to <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>.</p>
</dd></dl>
<p>Class attributes:</p>
<dl class="attribute">
<dt id="datetime.time.min">
<code class="descclassname">time.</code><code class="descname">min</code><a class="headerlink" href="#datetime.time.min" title="Permalink to this definition">¶</a></dt>
<dd><p>The earliest representable <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a>, <code class="docutils literal notranslate"><span class="pre">time(0,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">0)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.time.max">
<code class="descclassname">time.</code><code class="descname">max</code><a class="headerlink" href="#datetime.time.max" title="Permalink to this definition">¶</a></dt>
<dd><p>The latest representable <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a>, <code class="docutils literal notranslate"><span class="pre">time(23,</span> <span class="pre">59,</span> <span class="pre">59,</span> <span class="pre">999999)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.time.resolution">
<code class="descclassname">time.</code><code class="descname">resolution</code><a class="headerlink" href="#datetime.time.resolution" title="Permalink to this definition">¶</a></dt>
<dd><p>The smallest possible difference between non-equal <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> objects,
<code class="docutils literal notranslate"><span class="pre">timedelta(microseconds=1)</span></code>, although note that arithmetic on
<a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> objects is not supported.</p>
</dd></dl>
<p>Instance attributes (read-only):</p>
<dl class="attribute">
<dt id="datetime.time.hour">
<code class="descclassname">time.</code><code class="descname">hour</code><a class="headerlink" href="#datetime.time.hour" title="Permalink to this definition">¶</a></dt>
<dd><p>In <code class="docutils literal notranslate"><span class="pre">range(24)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.time.minute">
<code class="descclassname">time.</code><code class="descname">minute</code><a class="headerlink" href="#datetime.time.minute" title="Permalink to this definition">¶</a></dt>
<dd><p>In <code class="docutils literal notranslate"><span class="pre">range(60)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.time.second">
<code class="descclassname">time.</code><code class="descname">second</code><a class="headerlink" href="#datetime.time.second" title="Permalink to this definition">¶</a></dt>
<dd><p>In <code class="docutils literal notranslate"><span class="pre">range(60)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.time.microsecond">
<code class="descclassname">time.</code><code class="descname">microsecond</code><a class="headerlink" href="#datetime.time.microsecond" title="Permalink to this definition">¶</a></dt>
<dd><p>In <code class="docutils literal notranslate"><span class="pre">range(1000000)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.time.tzinfo">
<code class="descclassname">time.</code><code class="descname">tzinfo</code><a class="headerlink" href="#datetime.time.tzinfo" title="Permalink to this definition">¶</a></dt>
<dd><p>The object passed as the tzinfo argument to the <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> constructor, or
<code class="docutils literal notranslate"><span class="pre">None</span></code> if none was passed.</p>
</dd></dl>
<p>Supported operations:</p>
<ul class="simple">
<li>comparison of <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> to <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a>, where <em>a</em> is considered less
than <em>b</em> when <em>a</em> precedes <em>b</em> in time. If one comparand is naive and the other
is aware, <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised. If both comparands are aware, and have
the same <a class="reference internal" href="#datetime.time.tzinfo" title="datetime.time.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> attribute, the common <a class="reference internal" href="#datetime.time.tzinfo" title="datetime.time.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> attribute is
ignored and the base times are compared. If both comparands are aware and
have different <a class="reference internal" href="#datetime.time.tzinfo" title="datetime.time.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> attributes, the comparands are first adjusted by
subtracting their UTC offsets (obtained from <code class="docutils literal notranslate"><span class="pre">self.utcoffset()</span></code>). In order
to stop mixed-type comparisons from falling back to the default comparison by
object address, when a <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> object is compared to an object of a
different type, <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised unless the comparison is <code class="docutils literal notranslate"><span class="pre">==</span></code> or
<code class="docutils literal notranslate"><span class="pre">!=</span></code>. The latter cases return <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a> or <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>, respectively.</li>
<li>hash, use as dict key</li>
<li>efficient pickling</li>
<li>in Boolean contexts, a <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> object is considered to be true if and
only if, after converting it to minutes and subtracting <code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code> (or
<code class="docutils literal notranslate"><span class="pre">0</span></code> if that’s <code class="docutils literal notranslate"><span class="pre">None</span></code>), the result is non-zero.</li>
</ul>
<p>Instance methods:</p>
<dl class="method">
<dt id="datetime.time.replace">
<code class="descclassname">time.</code><code class="descname">replace</code><span class="sig-paren">(</span><span class="optional">[</span><em>hour</em><span class="optional">[</span>, <em>minute</em><span class="optional">[</span>, <em>second</em><span class="optional">[</span>, <em>microsecond</em><span class="optional">[</span>, <em>tzinfo</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.time.replace" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> with the same value, except for those attributes given
new values by whichever keyword arguments are specified. Note that
<code class="docutils literal notranslate"><span class="pre">tzinfo=None</span></code> can be specified to create a naive <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> from an
aware <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a>, without conversion of the time data.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.time.isoformat">
<code class="descclassname">time.</code><code class="descname">isoformat</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.time.isoformat" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a string representing the time in ISO 8601 format, HH:MM:SS.mmmmmm or, if
self.microsecond is 0, HH:MM:SS If <a class="reference internal" href="#datetime.time.utcoffset" title="datetime.time.utcoffset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code></a> does not return <code class="docutils literal notranslate"><span class="pre">None</span></code>, a
6-character string is appended, giving the UTC offset in (signed) hours and
minutes: HH:MM:SS.mmmmmm+HH:MM or, if self.microsecond is 0, HH:MM:SS+HH:MM</p>
</dd></dl>
<dl class="method">
<dt id="datetime.time.__str__">
<code class="descclassname">time.</code><code class="descname">__str__</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.time.__str__" title="Permalink to this definition">¶</a></dt>
<dd><p>For a time <em>t</em>, <code class="docutils literal notranslate"><span class="pre">str(t)</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">t.isoformat()</span></code>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.time.strftime">
<code class="descclassname">time.</code><code class="descname">strftime</code><span class="sig-paren">(</span><em>format</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.time.strftime" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a string representing the time, controlled by an explicit format string.
For a complete list of formatting directives, see section
<a class="reference internal" href="#strftime-strptime-behavior"><span class="std std-ref">strftime() and strptime() Behavior</span></a>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.time.__format__">
<code class="descclassname">time.</code><code class="descname">__format__</code><span class="sig-paren">(</span><em>format</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.time.__format__" title="Permalink to this definition">¶</a></dt>
<dd><p>Same as <a class="reference internal" href="#datetime.time.strftime" title="datetime.time.strftime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">time.strftime()</span></code></a>. This makes it possible to specify a format string
for a <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> object when using <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a>.
See section <a class="reference internal" href="#strftime-strptime-behavior"><span class="std std-ref">strftime() and strptime() Behavior</span></a>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.time.utcoffset">
<code class="descclassname">time.</code><code class="descname">utcoffset</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.time.utcoffset" title="Permalink to this definition">¶</a></dt>
<dd><p>If <a class="reference internal" href="#datetime.time.tzinfo" title="datetime.time.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, returns <code class="docutils literal notranslate"><span class="pre">None</span></code>, else returns
<code class="docutils literal notranslate"><span class="pre">self.tzinfo.utcoffset(None)</span></code>, and raises an exception if the latter doesn’t
return <code class="docutils literal notranslate"><span class="pre">None</span></code> or a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object representing a whole number of
minutes with magnitude less than one day.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.time.dst">
<code class="descclassname">time.</code><code class="descname">dst</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.time.dst" title="Permalink to this definition">¶</a></dt>
<dd><p>If <a class="reference internal" href="#datetime.time.tzinfo" title="datetime.time.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, returns <code class="docutils literal notranslate"><span class="pre">None</span></code>, else returns
<code class="docutils literal notranslate"><span class="pre">self.tzinfo.dst(None)</span></code>, and raises an exception if the latter doesn’t return
<code class="docutils literal notranslate"><span class="pre">None</span></code>, or a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object representing a whole number of minutes
with magnitude less than one day.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.time.tzname">
<code class="descclassname">time.</code><code class="descname">tzname</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.time.tzname" title="Permalink to this definition">¶</a></dt>
<dd><p>If <a class="reference internal" href="#datetime.time.tzinfo" title="datetime.time.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, returns <code class="docutils literal notranslate"><span class="pre">None</span></code>, else returns
<code class="docutils literal notranslate"><span class="pre">self.tzinfo.tzname(None)</span></code>, or raises an exception if the latter doesn’t
return <code class="docutils literal notranslate"><span class="pre">None</span></code> or a string object.</p>
</dd></dl>
<p>Example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">time</span><span class="p">,</span> <span class="n">tzinfo</span><span class="p">,</span> <span class="n">timedelta</span>
<span class="gp">>>> </span><span class="k">class</span> <span class="nc">GMT1</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">dt</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">return</span> <span class="s2">"Europe/Prague"</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">t</span> <span class="o">=</span> <span class="n">time</span><span class="p">(</span><span class="mi">12</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">GMT1</span><span class="p">())</span>
<span class="gp">>>> </span><span class="n">t</span> <span class="c1"># doctest: +ELLIPSIS</span>
<span class="go">datetime.time(12, 10, 30, tzinfo=<GMT1 object at 0x...>)</span>
<span class="gp">>>> </span><span class="n">gmt</span> <span class="o">=</span> <span class="n">GMT1</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">t</span><span class="o">.</span><span class="n">isoformat</span><span class="p">()</span>
<span class="go">'12:10:30+01:00'</span>
<span class="gp">>>> </span><span class="n">t</span><span class="o">.</span><span class="n">dst</span><span class="p">()</span>
<span class="go">datetime.timedelta(0)</span>
<span class="gp">>>> </span><span class="n">t</span><span class="o">.</span><span class="n">tzname</span><span class="p">()</span>
<span class="go">'Europe/Prague'</span>
<span class="gp">>>> </span><span class="n">t</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">"%H:%M:%S %Z"</span><span class="p">)</span>
<span class="go">'12:10:30 Europe/Prague'</span>
<span class="gp">>>> </span><span class="s1">'The </span><span class="si">{}</span><span class="s1"> is {:%H:%M}.'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s2">"time"</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span>
<span class="go">'The time is 12:10.'</span>
</pre></div>
</div>
</div>
<div class="section" id="tzinfo-objects">
<span id="datetime-tzinfo"></span><h2>8.1.6. <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> Objects<a class="headerlink" href="#tzinfo-objects" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="datetime.tzinfo">
<em class="property">class </em><code class="descclassname">datetime.</code><code class="descname">tzinfo</code><a class="headerlink" href="#datetime.tzinfo" title="Permalink to this definition">¶</a></dt>
<dd><p>This is an abstract base class, meaning that this class should not be
instantiated directly. You need to derive a concrete subclass, and (at least)
supply implementations of the standard <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> methods needed by the
<a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> methods you use. The <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> module does not supply
any concrete subclasses of <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a>.</p>
<p>An instance of (a concrete subclass of) <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> can be passed to the
constructors for <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> and <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> objects. The latter objects
view their attributes as being in local time, and the <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> object
supports methods revealing offset of local time from UTC, the name of the time
zone, and DST offset, all relative to a date or time object passed to them.</p>
<p>Special requirement for pickling: A <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> subclass must have an
<a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code></a> method that can be called with no arguments, else it can be
pickled but possibly not unpickled again. This is a technical requirement that
may be relaxed in the future.</p>
<p>A concrete subclass of <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> may need to implement the following
methods. Exactly which methods are needed depends on the uses made of aware
<a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> objects. If in doubt, simply implement all of them.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.tzinfo.utcoffset">
<code class="descclassname">tzinfo.</code><code class="descname">utcoffset</code><span class="sig-paren">(</span><em>self</em>, <em>dt</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.tzinfo.utcoffset" title="Permalink to this definition">¶</a></dt>
<dd><p>Return offset of local time from UTC, in minutes east of UTC. If local time is
west of UTC, this should be negative. Note that this is intended to be the
total offset from UTC; for example, if a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> object represents both
time zone and DST adjustments, <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code></a> should return their sum. If
the UTC offset isn’t known, return <code class="docutils literal notranslate"><span class="pre">None</span></code>. Else the value returned must be a
<a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object specifying a whole number of minutes in the range
-1439 to 1439 inclusive (1440 = 24*60; the magnitude of the offset must be less
than one day). Most implementations of <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code></a> will probably look
like one of these two:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">return</span> <span class="n">CONSTANT</span> <span class="c1"># fixed-offset class</span>
<span class="k">return</span> <span class="n">CONSTANT</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">dst</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span> <span class="c1"># daylight-aware class</span>
</pre></div>
</div>
<p>If <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code></a> does not return <code class="docutils literal notranslate"><span class="pre">None</span></code>, <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dst()</span></code></a> should not return
<code class="docutils literal notranslate"><span class="pre">None</span></code> either.</p>
<p>The default implementation of <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code></a> raises
<a class="reference internal" href="exceptions.html#exceptions.NotImplementedError" title="exceptions.NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.tzinfo.dst">
<code class="descclassname">tzinfo.</code><code class="descname">dst</code><span class="sig-paren">(</span><em>self</em>, <em>dt</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.tzinfo.dst" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the daylight saving time (DST) adjustment, in minutes east of UTC, or
<code class="docutils literal notranslate"><span class="pre">None</span></code> if DST information isn’t known. Return <code class="docutils literal notranslate"><span class="pre">timedelta(0)</span></code> if DST is not
in effect. If DST is in effect, return the offset as a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object
(see <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code></a> for details). Note that DST offset, if applicable, has
already been added to the UTC offset returned by <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code></a>, so there’s
no need to consult <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dst()</span></code></a> unless you’re interested in obtaining DST info
separately. For example, <a class="reference internal" href="#datetime.datetime.timetuple" title="datetime.datetime.timetuple"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.timetuple()</span></code></a> calls its <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a>
attribute’s <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dst()</span></code></a> method to determine how the <code class="xref py py-attr docutils literal notranslate"><span class="pre">tm_isdst</span></code> flag
should be set, and <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tzinfo.fromutc()</span></code></a> calls <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dst()</span></code></a> to account for
DST changes when crossing time zones.</p>
<p>An instance <em>tz</em> of a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> subclass that models both standard and
daylight times must be consistent in this sense:</p>
<p><code class="docutils literal notranslate"><span class="pre">tz.utcoffset(dt)</span> <span class="pre">-</span> <span class="pre">tz.dst(dt)</span></code></p>
<p>must return the same result for every <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> <em>dt</em> with <code class="docutils literal notranslate"><span class="pre">dt.tzinfo</span> <span class="pre">==</span>
<span class="pre">tz</span></code> For sane <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> subclasses, this expression yields the time
zone’s “standard offset”, which should not depend on the date or the time, but
only on geographic location. The implementation of <a class="reference internal" href="#datetime.datetime.astimezone" title="datetime.datetime.astimezone"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.astimezone()</span></code></a>
relies on this, but cannot detect violations; it’s the programmer’s
responsibility to ensure it. If a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> subclass cannot guarantee
this, it may be able to override the default implementation of
<a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tzinfo.fromutc()</span></code></a> to work correctly with <code class="xref py py-meth docutils literal notranslate"><span class="pre">astimezone()</span></code> regardless.</p>
<p>Most implementations of <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dst()</span></code></a> will probably look like one of these two:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="c1"># a fixed-offset class: doesn't account for DST</span>
<span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
<p>or</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="c1"># Code to set dston and dstoff to the time zone's DST</span>
<span class="c1"># transition times based on the input dt.year, and expressed</span>
<span class="c1"># in standard local time. Then</span>
<span class="k">if</span> <span class="n">dston</span> <span class="o"><=</span> <span class="n">dt</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> <span class="o"><</span> <span class="n">dstoff</span><span class="p">:</span>
<span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
<p>The default implementation of <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dst()</span></code></a> raises <a class="reference internal" href="exceptions.html#exceptions.NotImplementedError" title="exceptions.NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.tzinfo.tzname">
<code class="descclassname">tzinfo.</code><code class="descname">tzname</code><span class="sig-paren">(</span><em>self</em>, <em>dt</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.tzinfo.tzname" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the time zone name corresponding to the <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object <em>dt</em>, as
a string. Nothing about string names is defined by the <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> module,
and there’s no requirement that it mean anything in particular. For example,
“GMT”, “UTC”, “-500”, “-5:00”, “EDT”, “US/Eastern”, “America/New York” are all
valid replies. Return <code class="docutils literal notranslate"><span class="pre">None</span></code> if a string name isn’t known. Note that this is
a method rather than a fixed string primarily because some <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a>
subclasses will wish to return different names depending on the specific value
of <em>dt</em> passed, especially if the <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> class is accounting for
daylight time.</p>
<p>The default implementation of <a class="reference internal" href="#datetime.tzinfo.tzname" title="datetime.tzinfo.tzname"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tzname()</span></code></a> raises <a class="reference internal" href="exceptions.html#exceptions.NotImplementedError" title="exceptions.NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p>
</dd></dl>
<p>These methods are called by a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> or <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> object, in
response to their methods of the same names. A <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object passes
itself as the argument, and a <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> object passes <code class="docutils literal notranslate"><span class="pre">None</span></code> as the
argument. A <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> subclass’s methods should therefore be prepared to
accept a <em>dt</em> argument of <code class="docutils literal notranslate"><span class="pre">None</span></code>, or of class <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>.</p>
<p>When <code class="docutils literal notranslate"><span class="pre">None</span></code> is passed, it’s up to the class designer to decide the best
response. For example, returning <code class="docutils literal notranslate"><span class="pre">None</span></code> is appropriate if the class wishes to
say that time objects don’t participate in the <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> protocols. It
may be more useful for <code class="docutils literal notranslate"><span class="pre">utcoffset(None)</span></code> to return the standard UTC offset, as
there is no other convention for discovering the standard offset.</p>
<p>When a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object is passed in response to a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>
method, <code class="docutils literal notranslate"><span class="pre">dt.tzinfo</span></code> is the same object as <em>self</em>. <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> methods can
rely on this, unless user code calls <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> methods directly. The
intent is that the <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> methods interpret <em>dt</em> as being in local
time, and not need worry about objects in other timezones.</p>
<p>There is one more <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> method that a subclass may wish to override:</p>
<dl class="method">
<dt id="datetime.tzinfo.fromutc">
<code class="descclassname">tzinfo.</code><code class="descname">fromutc</code><span class="sig-paren">(</span><em>self</em>, <em>dt</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.tzinfo.fromutc" title="Permalink to this definition">¶</a></dt>
<dd><p>This is called from the default <a class="reference internal" href="#datetime.datetime.astimezone" title="datetime.datetime.astimezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.astimezone()</span></code></a>
implementation. When called from that, <code class="docutils literal notranslate"><span class="pre">dt.tzinfo</span></code> is <em>self</em>, and <em>dt</em>’s
date and time data are to be viewed as expressing a UTC time. The purpose
of <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromutc()</span></code></a> is to adjust the date and time data, returning an
equivalent datetime in <em>self</em>’s local time.</p>
<p>Most <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> subclasses should be able to inherit the default
<a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromutc()</span></code></a> implementation without problems. It’s strong enough to handle
fixed-offset time zones, and time zones accounting for both standard and
daylight time, and the latter even if the DST transition times differ in
different years. An example of a time zone the default <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromutc()</span></code></a>
implementation may not handle correctly in all cases is one where the standard
offset (from UTC) depends on the specific date and time passed, which can happen
for political reasons. The default implementations of <code class="xref py py-meth docutils literal notranslate"><span class="pre">astimezone()</span></code> and
<a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromutc()</span></code></a> may not produce the result you want if the result is one of the
hours straddling the moment the standard offset changes.</p>
<p>Skipping code for error cases, the default <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromutc()</span></code></a> implementation acts
like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">fromutc</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="c1"># raise ValueError error if dt.tzinfo is not self</span>
<span class="n">dtoff</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">utcoffset</span><span class="p">()</span>
<span class="n">dtdst</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">dst</span><span class="p">()</span>
<span class="c1"># raise ValueError if dtoff is None or dtdst is None</span>
<span class="n">delta</span> <span class="o">=</span> <span class="n">dtoff</span> <span class="o">-</span> <span class="n">dtdst</span> <span class="c1"># this is self's standard offset</span>
<span class="k">if</span> <span class="n">delta</span><span class="p">:</span>
<span class="n">dt</span> <span class="o">+=</span> <span class="n">delta</span> <span class="c1"># convert to standard local time</span>
<span class="n">dtdst</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">dst</span><span class="p">()</span>
<span class="c1"># raise ValueError if dtdst is None</span>
<span class="k">if</span> <span class="n">dtdst</span><span class="p">:</span>
<span class="k">return</span> <span class="n">dt</span> <span class="o">+</span> <span class="n">dtdst</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="n">dt</span>
</pre></div>
</div>
</dd></dl>
<p>Example <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> classes:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">tzinfo</span><span class="p">,</span> <span class="n">timedelta</span><span class="p">,</span> <span class="n">datetime</span>
<span class="n">ZERO</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">HOUR</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="c1"># A UTC class.</span>
<span class="k">class</span> <span class="nc">UTC</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span>
<span class="sd">"""UTC"""</span>
<span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="k">return</span> <span class="n">ZERO</span>
<span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="k">return</span> <span class="s2">"UTC"</span>
<span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="k">return</span> <span class="n">ZERO</span>
<span class="n">utc</span> <span class="o">=</span> <span class="n">UTC</span><span class="p">()</span>
<span class="c1"># A class building tzinfo objects for fixed-offset time zones.</span>
<span class="c1"># Note that FixedOffset(0, "UTC") is a different way to build a</span>
<span class="c1"># UTC tzinfo object.</span>
<span class="k">class</span> <span class="nc">FixedOffset</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span>
<span class="sd">"""Fixed offset in minutes east from UTC."""</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">offset</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__offset</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">minutes</span> <span class="o">=</span> <span class="n">offset</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__name</span> <span class="o">=</span> <span class="n">name</span>
<span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__offset</span>
<span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__name</span>
<span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="k">return</span> <span class="n">ZERO</span>
<span class="c1"># A class capturing the platform's idea of local time.</span>
<span class="kn">import</span> <span class="nn">time</span> <span class="k">as</span> <span class="nn">_time</span>
<span class="n">STDOFFSET</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">seconds</span> <span class="o">=</span> <span class="o">-</span><span class="n">_time</span><span class="o">.</span><span class="n">timezone</span><span class="p">)</span>
<span class="k">if</span> <span class="n">_time</span><span class="o">.</span><span class="n">daylight</span><span class="p">:</span>
<span class="n">DSTOFFSET</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">seconds</span> <span class="o">=</span> <span class="o">-</span><span class="n">_time</span><span class="o">.</span><span class="n">altzone</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">DSTOFFSET</span> <span class="o">=</span> <span class="n">STDOFFSET</span>
<span class="n">DSTDIFF</span> <span class="o">=</span> <span class="n">DSTOFFSET</span> <span class="o">-</span> <span class="n">STDOFFSET</span>
<span class="k">class</span> <span class="nc">LocalTimezone</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_isdst</span><span class="p">(</span><span class="n">dt</span><span class="p">):</span>
<span class="k">return</span> <span class="n">DSTOFFSET</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="n">STDOFFSET</span>
<span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_isdst</span><span class="p">(</span><span class="n">dt</span><span class="p">):</span>
<span class="k">return</span> <span class="n">DSTDIFF</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="n">ZERO</span>
<span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="k">return</span> <span class="n">_time</span><span class="o">.</span><span class="n">tzname</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_isdst</span><span class="p">(</span><span class="n">dt</span><span class="p">)]</span>
<span class="k">def</span> <span class="nf">_isdst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="n">tt</span> <span class="o">=</span> <span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="n">dt</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="n">dt</span><span class="o">.</span><span class="n">day</span><span class="p">,</span>
<span class="n">dt</span><span class="o">.</span><span class="n">hour</span><span class="p">,</span> <span class="n">dt</span><span class="o">.</span><span class="n">minute</span><span class="p">,</span> <span class="n">dt</span><span class="o">.</span><span class="n">second</span><span class="p">,</span>
<span class="n">dt</span><span class="o">.</span><span class="n">weekday</span><span class="p">(),</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">stamp</span> <span class="o">=</span> <span class="n">_time</span><span class="o">.</span><span class="n">mktime</span><span class="p">(</span><span class="n">tt</span><span class="p">)</span>
<span class="n">tt</span> <span class="o">=</span> <span class="n">_time</span><span class="o">.</span><span class="n">localtime</span><span class="p">(</span><span class="n">stamp</span><span class="p">)</span>
<span class="k">return</span> <span class="n">tt</span><span class="o">.</span><span class="n">tm_isdst</span> <span class="o">></span> <span class="mi">0</span>
<span class="n">Local</span> <span class="o">=</span> <span class="n">LocalTimezone</span><span class="p">()</span>
<span class="c1"># A complete implementation of current DST rules for major US time zones.</span>
<span class="k">def</span> <span class="nf">first_sunday_on_or_after</span><span class="p">(</span><span class="n">dt</span><span class="p">):</span>
<span class="n">days_to_go</span> <span class="o">=</span> <span class="mi">6</span> <span class="o">-</span> <span class="n">dt</span><span class="o">.</span><span class="n">weekday</span><span class="p">()</span>
<span class="k">if</span> <span class="n">days_to_go</span><span class="p">:</span>
<span class="n">dt</span> <span class="o">+=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days_to_go</span><span class="p">)</span>
<span class="k">return</span> <span class="n">dt</span>
<span class="c1"># US DST Rules</span>
<span class="c1">#</span>
<span class="c1"># This is a simplified (i.e., wrong for a few cases) set of rules for US</span>
<span class="c1"># DST start and end times. For a complete and up-to-date set of DST rules</span>
<span class="c1"># and timezone definitions, visit the Olson Database (or try pytz):</span>
<span class="c1"># http://www.twinsun.com/tz/tz-link.htm</span>
<span class="c1"># http://sourceforge.net/projects/pytz/ (might not be up-to-date)</span>
<span class="c1">#</span>
<span class="c1"># In the US, since 2007, DST starts at 2am (standard time) on the second</span>
<span class="c1"># Sunday in March, which is the first Sunday on or after Mar 8.</span>
<span class="n">DSTSTART_2007</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="c1"># and ends at 2am (DST time; 1am standard time) on the first Sunday of Nov.</span>
<span class="n">DSTEND_2007</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">11</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="c1"># From 1987 to 2006, DST used to start at 2am (standard time) on the first</span>
<span class="c1"># Sunday in April and to end at 2am (DST time; 1am standard time) on the last</span>
<span class="c1"># Sunday of October, which is the first Sunday on or after Oct 25.</span>
<span class="n">DSTSTART_1987_2006</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="n">DSTEND_1987_2006</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">25</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="c1"># From 1967 to 1986, DST used to start at 2am (standard time) on the last</span>
<span class="c1"># Sunday in April (the one on or after April 24) and to end at 2am (DST time;</span>
<span class="c1"># 1am standard time) on the last Sunday of October, which is the first Sunday</span>
<span class="c1"># on or after Oct 25.</span>
<span class="n">DSTSTART_1967_1986</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">24</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="n">DSTEND_1967_1986</span> <span class="o">=</span> <span class="n">DSTEND_1987_2006</span>
<span class="k">class</span> <span class="nc">USTimeZone</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">hours</span><span class="p">,</span> <span class="n">reprname</span><span class="p">,</span> <span class="n">stdname</span><span class="p">,</span> <span class="n">dstname</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">stdoffset</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="n">hours</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">reprname</span> <span class="o">=</span> <span class="n">reprname</span>
<span class="bp">self</span><span class="o">.</span><span class="n">stdname</span> <span class="o">=</span> <span class="n">stdname</span>
<span class="bp">self</span><span class="o">.</span><span class="n">dstname</span> <span class="o">=</span> <span class="n">dstname</span>
<span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">reprname</span>
<span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">dst</span><span class="p">(</span><span class="n">dt</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">dstname</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">stdname</span>
<span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">stdoffset</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">dst</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span>
<span class="k">if</span> <span class="n">dt</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">dt</span><span class="o">.</span><span class="n">tzinfo</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="c1"># An exception may be sensible here, in one or both cases.</span>
<span class="c1"># It depends on how you want to treat them. The default</span>
<span class="c1"># fromutc() implementation (called by the default astimezone()</span>
<span class="c1"># implementation) passes a datetime with dt.tzinfo is self.</span>
<span class="k">return</span> <span class="n">ZERO</span>
<span class="k">assert</span> <span class="n">dt</span><span class="o">.</span><span class="n">tzinfo</span> <span class="ow">is</span> <span class="bp">self</span>
<span class="c1"># Find start and end times for US DST. For years before 1967, return</span>
<span class="c1"># ZERO for no DST.</span>
<span class="k">if</span> <span class="mi">2006</span> <span class="o"><</span> <span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">:</span>
<span class="n">dststart</span><span class="p">,</span> <span class="n">dstend</span> <span class="o">=</span> <span class="n">DSTSTART_2007</span><span class="p">,</span> <span class="n">DSTEND_2007</span>
<span class="k">elif</span> <span class="mi">1986</span> <span class="o"><</span> <span class="n">dt</span><span class="o">.</span><span class="n">year</span> <span class="o"><</span> <span class="mi">2007</span><span class="p">:</span>
<span class="n">dststart</span><span class="p">,</span> <span class="n">dstend</span> <span class="o">=</span> <span class="n">DSTSTART_1987_2006</span><span class="p">,</span> <span class="n">DSTEND_1987_2006</span>
<span class="k">elif</span> <span class="mi">1966</span> <span class="o"><</span> <span class="n">dt</span><span class="o">.</span><span class="n">year</span> <span class="o"><</span> <span class="mi">1987</span><span class="p">:</span>
<span class="n">dststart</span><span class="p">,</span> <span class="n">dstend</span> <span class="o">=</span> <span class="n">DSTSTART_1967_1986</span><span class="p">,</span> <span class="n">DSTEND_1967_1986</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="n">ZERO</span>
<span class="n">start</span> <span class="o">=</span> <span class="n">first_sunday_on_or_after</span><span class="p">(</span><span class="n">dststart</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">year</span><span class="o">=</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">))</span>
<span class="n">end</span> <span class="o">=</span> <span class="n">first_sunday_on_or_after</span><span class="p">(</span><span class="n">dstend</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">year</span><span class="o">=</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">))</span>
<span class="c1"># Can't compare naive to aware objects, so strip the timezone from</span>
<span class="c1"># dt first.</span>
<span class="k">if</span> <span class="n">start</span> <span class="o"><=</span> <span class="n">dt</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> <span class="o"><</span> <span class="n">end</span><span class="p">:</span>
<span class="k">return</span> <span class="n">HOUR</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="n">ZERO</span>
<span class="n">Eastern</span> <span class="o">=</span> <span class="n">USTimeZone</span><span class="p">(</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span> <span class="s2">"Eastern"</span><span class="p">,</span> <span class="s2">"EST"</span><span class="p">,</span> <span class="s2">"EDT"</span><span class="p">)</span>
<span class="n">Central</span> <span class="o">=</span> <span class="n">USTimeZone</span><span class="p">(</span><span class="o">-</span><span class="mi">6</span><span class="p">,</span> <span class="s2">"Central"</span><span class="p">,</span> <span class="s2">"CST"</span><span class="p">,</span> <span class="s2">"CDT"</span><span class="p">)</span>
<span class="n">Mountain</span> <span class="o">=</span> <span class="n">USTimeZone</span><span class="p">(</span><span class="o">-</span><span class="mi">7</span><span class="p">,</span> <span class="s2">"Mountain"</span><span class="p">,</span> <span class="s2">"MST"</span><span class="p">,</span> <span class="s2">"MDT"</span><span class="p">)</span>
<span class="n">Pacific</span> <span class="o">=</span> <span class="n">USTimeZone</span><span class="p">(</span><span class="o">-</span><span class="mi">8</span><span class="p">,</span> <span class="s2">"Pacific"</span><span class="p">,</span> <span class="s2">"PST"</span><span class="p">,</span> <span class="s2">"PDT"</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that there are unavoidable subtleties twice per year in a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a>
subclass accounting for both standard and daylight time, at the DST transition
points. For concreteness, consider US Eastern (UTC -0500), where EDT begins the
minute after 1:59 (EST) on the second Sunday in March, and ends the minute after
1:59 (EDT) on the first Sunday in November:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="n">UTC</span> <span class="mi">3</span><span class="p">:</span><span class="n">MM</span> <span class="mi">4</span><span class="p">:</span><span class="n">MM</span> <span class="mi">5</span><span class="p">:</span><span class="n">MM</span> <span class="mi">6</span><span class="p">:</span><span class="n">MM</span> <span class="mi">7</span><span class="p">:</span><span class="n">MM</span> <span class="mi">8</span><span class="p">:</span><span class="n">MM</span>
<span class="n">EST</span> <span class="mi">22</span><span class="p">:</span><span class="n">MM</span> <span class="mi">23</span><span class="p">:</span><span class="n">MM</span> <span class="mi">0</span><span class="p">:</span><span class="n">MM</span> <span class="mi">1</span><span class="p">:</span><span class="n">MM</span> <span class="mi">2</span><span class="p">:</span><span class="n">MM</span> <span class="mi">3</span><span class="p">:</span><span class="n">MM</span>
<span class="n">EDT</span> <span class="mi">23</span><span class="p">:</span><span class="n">MM</span> <span class="mi">0</span><span class="p">:</span><span class="n">MM</span> <span class="mi">1</span><span class="p">:</span><span class="n">MM</span> <span class="mi">2</span><span class="p">:</span><span class="n">MM</span> <span class="mi">3</span><span class="p">:</span><span class="n">MM</span> <span class="mi">4</span><span class="p">:</span><span class="n">MM</span>
<span class="n">start</span> <span class="mi">22</span><span class="p">:</span><span class="n">MM</span> <span class="mi">23</span><span class="p">:</span><span class="n">MM</span> <span class="mi">0</span><span class="p">:</span><span class="n">MM</span> <span class="mi">1</span><span class="p">:</span><span class="n">MM</span> <span class="mi">3</span><span class="p">:</span><span class="n">MM</span> <span class="mi">4</span><span class="p">:</span><span class="n">MM</span>
<span class="n">end</span> <span class="mi">23</span><span class="p">:</span><span class="n">MM</span> <span class="mi">0</span><span class="p">:</span><span class="n">MM</span> <span class="mi">1</span><span class="p">:</span><span class="n">MM</span> <span class="mi">1</span><span class="p">:</span><span class="n">MM</span> <span class="mi">2</span><span class="p">:</span><span class="n">MM</span> <span class="mi">3</span><span class="p">:</span><span class="n">MM</span>
</pre></div>
</div>
<p>When DST starts (the “start” line), the local wall clock leaps from 1:59 to
3:00. A wall time of the form 2:MM doesn’t really make sense on that day, so
<code class="docutils literal notranslate"><span class="pre">astimezone(Eastern)</span></code> won’t deliver a result with <code class="docutils literal notranslate"><span class="pre">hour</span> <span class="pre">==</span> <span class="pre">2</span></code> on the day DST
begins. In order for <code class="xref py py-meth docutils literal notranslate"><span class="pre">astimezone()</span></code> to make this guarantee, the
<code class="xref py py-meth docutils literal notranslate"><span class="pre">rzinfo.dst()</span></code> method must consider times in the “missing hour” (2:MM for
Eastern) to be in daylight time.</p>
<p>When DST ends (the “end” line), there’s a potentially worse problem: there’s an
hour that can’t be spelled unambiguously in local wall time: the last hour of
daylight time. In Eastern, that’s times of the form 5:MM UTC on the day
daylight time ends. The local wall clock leaps from 1:59 (daylight time) back
to 1:00 (standard time) again. Local times of the form 1:MM are ambiguous.
<code class="xref py py-meth docutils literal notranslate"><span class="pre">astimezone()</span></code> mimics the local clock’s behavior by mapping two adjacent UTC
hours into the same local hour then. In the Eastern example, UTC times of the
form 5:MM and 6:MM both map to 1:MM when converted to Eastern. In order for
<code class="xref py py-meth docutils literal notranslate"><span class="pre">astimezone()</span></code> to make this guarantee, the <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tzinfo.dst()</span></code></a> method must
consider times in the “repeated hour” to be in standard time. This is easily
arranged, as in the example, by expressing DST switch times in the time zone’s
standard local time.</p>
<p>Applications that can’t bear such ambiguities should avoid using hybrid
<a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> subclasses; there are no ambiguities when using UTC, or any
other fixed-offset <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> subclass (such as a class representing only
EST (fixed offset -5 hours), or only EDT (fixed offset -4 hours)).</p>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference external" href="https://pypi.org/project/pytz/">pytz</a></dt>
<dd><p class="first">The standard library has no <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> instances, but
there exists a third-party library which brings the <em>IANA timezone
database</em> (also known as the Olson database) to Python: <em>pytz</em>.</p>
<p class="last"><em>pytz</em> contains up-to-date information and its usage is recommended.</p>
</dd>
<dt><a class="reference external" href="https://www.iana.org/time-zones">IANA timezone database</a></dt>
<dd>The Time Zone Database (often called tz or zoneinfo) contains code and
data that represent the history of local time for many representative
locations around the globe. It is updated periodically to reflect changes
made by political bodies to time zone boundaries, UTC offsets, and
daylight-saving rules.</dd>
</dl>
</div>
</div>
<div class="section" id="strftime-and-strptime-behavior">
<span id="strftime-strptime-behavior"></span><h2>8.1.7. <code class="xref py py-meth docutils literal notranslate"><span class="pre">strftime()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">strptime()</span></code> Behavior<a class="headerlink" href="#strftime-and-strptime-behavior" title="Permalink to this headline">¶</a></h2>
<p><a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a>, <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>, and <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> objects all support a
<code class="docutils literal notranslate"><span class="pre">strftime(format)</span></code> method, to create a string representing the time under the
control of an explicit format string. Broadly speaking, <code class="docutils literal notranslate"><span class="pre">d.strftime(fmt)</span></code>
acts like the <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</span></code></a> module’s <code class="docutils literal notranslate"><span class="pre">time.strftime(fmt,</span> <span class="pre">d.timetuple())</span></code>
although not all objects support a <code class="xref py py-meth docutils literal notranslate"><span class="pre">timetuple()</span></code> method.</p>
<p>Conversely, the <a class="reference internal" href="#datetime.datetime.strptime" title="datetime.datetime.strptime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.strptime()</span></code></a> class method creates a
<a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object from a string representing a date and time and a
corresponding format string. <code class="docutils literal notranslate"><span class="pre">datetime.strptime(date_string,</span> <span class="pre">format)</span></code> is
equivalent to <code class="docutils literal notranslate"><span class="pre">datetime(*(time.strptime(date_string,</span> <span class="pre">format)[0:6]))</span></code>, except
when the format includes sub-second components or timezone offset information,
which are supported in <code class="docutils literal notranslate"><span class="pre">datetime.strptime</span></code> but are discarded by <code class="docutils literal notranslate"><span class="pre">time.strptime</span></code>.</p>
<p>For <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> objects, the format codes for year, month, and day should not
be used, as time objects have no such values. If they’re used anyway, <code class="docutils literal notranslate"><span class="pre">1900</span></code>
is substituted for the year, and <code class="docutils literal notranslate"><span class="pre">1</span></code> for the month and day.</p>
<p>For <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> objects, the format codes for hours, minutes, seconds, and
microseconds should not be used, as <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> objects have no such
values. If they’re used anyway, <code class="docutils literal notranslate"><span class="pre">0</span></code> is substituted for them.</p>
<p>The full set of format codes supported varies across platforms, because Python
calls the platform C library’s <code class="xref py py-func docutils literal notranslate"><span class="pre">strftime()</span></code> function, and platform
variations are common. To see the full set of format codes supported on your
platform, consult the <em class="manpage">strftime(3)</em> documentation.</p>
<p>For the same reason, handling of format strings containing Unicode code points
that can’t be represented in the charset of the current locale is also
platform-dependent. On some platforms such code points are preserved intact in
the output, while on others <code class="docutils literal notranslate"><span class="pre">strftime</span></code> may raise <a class="reference internal" href="exceptions.html#exceptions.UnicodeError" title="exceptions.UnicodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeError</span></code></a> or return
an empty string instead.</p>
<p>The following is a list of all the format codes that the C standard (1989
version) requires, and these work on all platforms with a standard C
implementation. Note that the 1999 version of the C standard added additional
format codes.</p>
<p>The exact range of years for which <code class="xref py py-meth docutils literal notranslate"><span class="pre">strftime()</span></code> works also varies across
platforms. Regardless of platform, years before 1900 cannot be used.</p>
<table border="1" class="docutils">
<colgroup>
<col width="15%" />
<col width="43%" />
<col width="32%" />
<col width="9%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Directive</th>
<th class="head">Meaning</th>
<th class="head">Example</th>
<th class="head">Notes</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">%a</span></code></td>
<td>Weekday as locale’s
abbreviated name.</td>
<td><div class="first last line-block">
<div class="line">Sun, Mon, …, Sat
(en_US);</div>
<div class="line">So, Mo, …, Sa
(de_DE)</div>
</div>
</td>
<td>(1)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">%A</span></code></td>
<td>Weekday as locale’s full name.</td>
<td><div class="first last line-block">
<div class="line">Sunday, Monday, …,
Saturday (en_US);</div>
<div class="line">Sonntag, Montag, …,
Samstag (de_DE)</div>
</div>
</td>
<td>(1)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">%w</span></code></td>
<td>Weekday as a decimal number,
where 0 is Sunday and 6 is
Saturday.</td>
<td>0, 1, …, 6</td>
<td> </td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">%d</span></code></td>
<td>Day of the month as a
zero-padded decimal number.</td>
<td>01, 02, …, 31</td>
<td> </td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">%b</span></code></td>
<td>Month as locale’s abbreviated
name.</td>
<td><div class="first last line-block">
<div class="line">Jan, Feb, …, Dec
(en_US);</div>
<div class="line">Jan, Feb, …, Dez
(de_DE)</div>
</div>
</td>
<td>(1)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">%B</span></code></td>
<td>Month as locale’s full name.</td>
<td><div class="first last line-block">
<div class="line">January, February,
…, December (en_US);</div>
<div class="line">Januar, Februar, …,
Dezember (de_DE)</div>
</div>
</td>
<td>(1)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">%m</span></code></td>
<td>Month as a zero-padded
decimal number.</td>
<td>01, 02, …, 12</td>
<td> </td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">%y</span></code></td>
<td>Year without century as a
zero-padded decimal number.</td>
<td>00, 01, …, 99</td>
<td> </td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">%Y</span></code></td>
<td>Year with century as a decimal
number.</td>
<td>1970, 1988, 2001, 2013</td>
<td> </td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">%H</span></code></td>
<td>Hour (24-hour clock) as a
zero-padded decimal number.</td>
<td>00, 01, …, 23</td>
<td> </td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">%I</span></code></td>
<td>Hour (12-hour clock) as a
zero-padded decimal number.</td>
<td>01, 02, …, 12</td>
<td> </td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">%p</span></code></td>
<td>Locale’s equivalent of either
AM or PM.</td>
<td><div class="first last line-block">
<div class="line">AM, PM (en_US);</div>
<div class="line">am, pm (de_DE)</div>
</div>
</td>
<td>(1),
(2)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">%M</span></code></td>
<td>Minute as a zero-padded
decimal number.</td>
<td>00, 01, …, 59</td>
<td> </td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">%S</span></code></td>
<td>Second as a zero-padded
decimal number.</td>
<td>00, 01, …, 59</td>
<td>(3)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">%f</span></code></td>
<td>Microsecond as a decimal
number, zero-padded on the
left.</td>
<td>000000, 000001, …,
999999</td>
<td>(4)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">%z</span></code></td>
<td>UTC offset in the form +HHMM
or -HHMM (empty string if the
the object is naive).</td>
<td>(empty), +0000, -0400,
+1030</td>
<td>(5)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">%Z</span></code></td>
<td>Time zone name (empty string
if the object is naive).</td>
<td>(empty), UTC, EST, CST</td>
<td> </td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">%j</span></code></td>
<td>Day of the year as a
zero-padded decimal number.</td>
<td>001, 002, …, 366</td>
<td> </td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">%U</span></code></td>
<td>Week number of the year
(Sunday as the first day of
the week) as a zero padded
decimal number. All days in a
new year preceding the first
Sunday are considered to be in
week 0.</td>
<td>00, 01, …, 53</td>
<td>(6)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">%W</span></code></td>
<td>Week number of the year
(Monday as the first day of
the week) as a decimal number.
All days in a new year
preceding the first Monday
are considered to be in
week 0.</td>
<td>00, 01, …, 53</td>
<td>(6)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">%c</span></code></td>
<td>Locale’s appropriate date and
time representation.</td>
<td><div class="first last line-block">
<div class="line">Tue Aug 16 21:30:00
1988 (en_US);</div>
<div class="line">Di 16 Aug 21:30:00
1988 (de_DE)</div>
</div>
</td>
<td>(1)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">%x</span></code></td>
<td>Locale’s appropriate date
representation.</td>
<td><div class="first last line-block">
<div class="line">08/16/88 (None);</div>
<div class="line">08/16/1988 (en_US);</div>
<div class="line">16.08.1988 (de_DE)</div>
</div>
</td>
<td>(1)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">%X</span></code></td>
<td>Locale’s appropriate time
representation.</td>
<td><div class="first last line-block">
<div class="line">21:30:00 (en_US);</div>
<div class="line">21:30:00 (de_DE)</div>
</div>
</td>
<td>(1)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">%%</span></code></td>
<td>A literal <code class="docutils literal notranslate"><span class="pre">'%'</span></code> character.</td>
<td>%</td>
<td> </td>
</tr>
</tbody>
</table>
<p>Notes:</p>
<ol class="arabic">
<li><p class="first">Because the format depends on the current locale, care should be taken when
making assumptions about the output value. Field orderings will vary (for
example, “month/day/year” versus “day/month/year”), and the output may
contain Unicode characters encoded using the locale’s default encoding (for
example, if the current locale is <code class="docutils literal notranslate"><span class="pre">ja_JP</span></code>, the default encoding could be
any one of <code class="docutils literal notranslate"><span class="pre">eucJP</span></code>, <code class="docutils literal notranslate"><span class="pre">SJIS</span></code>, or <code class="docutils literal notranslate"><span class="pre">utf-8</span></code>; use <a class="reference internal" href="locale.html#locale.getlocale" title="locale.getlocale"><code class="xref py py-meth docutils literal notranslate"><span class="pre">locale.getlocale()</span></code></a>
to determine the current locale’s encoding).</p>
</li>
<li><p class="first">When used with the <code class="xref py py-meth docutils literal notranslate"><span class="pre">strptime()</span></code> method, the <code class="docutils literal notranslate"><span class="pre">%p</span></code> directive only affects
the output hour field if the <code class="docutils literal notranslate"><span class="pre">%I</span></code> directive is used to parse the hour.</p>
</li>
<li><p class="first">Unlike the <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</span></code></a> module, the <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> module does not support
leap seconds.</p>
</li>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">%f</span></code> is an extension to the set of format characters in the C standard
(but implemented separately in datetime objects, and therefore always
available). When used with the <code class="xref py py-meth docutils literal notranslate"><span class="pre">strptime()</span></code> method, the <code class="docutils literal notranslate"><span class="pre">%f</span></code>
directive accepts from one to six digits and zero pads on the right.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.6.</span></p>
</div>
</li>
<li><p class="first">For a naive object, the <code class="docutils literal notranslate"><span class="pre">%z</span></code> and <code class="docutils literal notranslate"><span class="pre">%Z</span></code> format codes are replaced by empty
strings.</p>
<p>For an aware object:</p>
<dl class="docutils">
<dt><code class="docutils literal notranslate"><span class="pre">%z</span></code></dt>
<dd><p class="first last"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code> is transformed into a 5-character string of the form
+HHMM or -HHMM, where HH is a 2-digit string giving the number of UTC
offset hours, and MM is a 2-digit string giving the number of UTC offset
minutes. For example, if <code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code> returns
<code class="docutils literal notranslate"><span class="pre">timedelta(hours=-3,</span> <span class="pre">minutes=-30)</span></code>, <code class="docutils literal notranslate"><span class="pre">%z</span></code> is replaced with the string
<code class="docutils literal notranslate"><span class="pre">'-0330'</span></code>.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">%Z</span></code></dt>
<dd><p class="first last">If <code class="xref py py-meth docutils literal notranslate"><span class="pre">tzname()</span></code> returns <code class="docutils literal notranslate"><span class="pre">None</span></code>, <code class="docutils literal notranslate"><span class="pre">%Z</span></code> is replaced by an empty
string. Otherwise <code class="docutils literal notranslate"><span class="pre">%Z</span></code> is replaced by the returned value, which must
be a string.</p>
</dd>
</dl>
</li>
<li><p class="first">When used with the <code class="xref py py-meth docutils literal notranslate"><span class="pre">strptime()</span></code> method, <code class="docutils literal notranslate"><span class="pre">%U</span></code> and <code class="docutils literal notranslate"><span class="pre">%W</span></code> are only used
in calculations when the day of the week and the year are specified.</p>
</li>
</ol>
<p class="rubric">Footnotes</p>
<table class="docutils footnote" frame="void" id="id2" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>If, that is, we ignore the effects of Relativity</td></tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">8.1. <code class="docutils literal notranslate"><span class="pre">datetime</span></code> — Basic date and time types</a><ul>
<li><a class="reference internal" href="#available-types">8.1.1. Available Types</a></li>
<li><a class="reference internal" href="#timedelta-objects">8.1.2. <code class="docutils literal notranslate"><span class="pre">timedelta</span></code> Objects</a></li>
<li><a class="reference internal" href="#date-objects">8.1.3. <code class="docutils literal notranslate"><span class="pre">date</span></code> Objects</a></li>
<li><a class="reference internal" href="#datetime-objects">8.1.4. <code class="docutils literal notranslate"><span class="pre">datetime</span></code> Objects</a></li>
<li><a class="reference internal" href="#time-objects">8.1.5. <code class="docutils literal notranslate"><span class="pre">time</span></code> Objects</a></li>
<li><a class="reference internal" href="#tzinfo-objects">8.1.6. <code class="docutils literal notranslate"><span class="pre">tzinfo</span></code> Objects</a></li>
<li><a class="reference internal" href="#strftime-and-strptime-behavior">8.1.7. <code class="docutils literal notranslate"><span class="pre">strftime()</span></code> and <code class="docutils literal notranslate"><span class="pre">strptime()</span></code> Behavior</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="datatypes.html"
title="previous chapter">8. Data Types</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="calendar.html"
title="next chapter">8.2. <code class="docutils literal notranslate"><span class="pre">calendar</span></code> — General calendar-related functions</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/library/datetime.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="calendar.html" title="8.2. calendar — General calendar-related functions"
>next</a> |</li>
<li class="right" >
<a href="datatypes.html" title="8. Data Types"
>previous</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<a href="../index.html">Python 2.7.18 documentation</a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
<li class="nav-item nav-item-2"><a href="datatypes.html" >8. Data Types</a> »</li>
</ul>
</div>
<div class="footer">
© <a href="../copyright.html">Copyright</a> 1990-2024, Python Software Foundation.
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
Last updated on November 21, 2024.
<a href="../bugs.html">Found a bug</a>?
<br />
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.8.5.
</div>
</body>
</html>
-=[ KCW uplo4d3r c0ded by cJ_n4p573r ]=-
Ⓒ2017 ҠЄГѦLѦ СүѣЄГ ЩѦГГіѺГՏ