2814 lines
312 KiB
HTML
2814 lines
312 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<title>datetime — Basic date and time types — Python 3.7.4 documentation</title>
|
||
<link rel="stylesheet" href="../_static/pydoctheme.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 3.7.4 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="calendar — General calendar-related functions" href="calendar.html" />
|
||
<link rel="prev" title="Data Types" href="datatypes.html" />
|
||
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
|
||
<link rel="canonical" href="https://docs.python.org/3/library/datetime.html" />
|
||
|
||
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
||
<script type="text/javascript" src="../_static/switchers.js"></script>
|
||
|
||
|
||
|
||
<style>
|
||
@media only screen {
|
||
table.full-width-table {
|
||
width: 100%;
|
||
}
|
||
}
|
||
</style>
|
||
|
||
|
||
</head><body>
|
||
|
||
<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="calendar — General calendar-related functions"
|
||
accesskey="N">next</a> |</li>
|
||
<li class="right" >
|
||
<a href="datatypes.html" title="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>
|
||
<span class="language_switcher_placeholder">en</span>
|
||
<span class="version_switcher_placeholder">3.7.4</span>
|
||
<a href="../index.html">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">Data Types</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" style="display: none" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="Quick search" 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>
|
||
<script type="text/javascript">$('.inline-search').show(0);</script>
|
||
|
|
||
</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><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>
|
||
<p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/datetime.py">Lib/datetime.py</a></p>
|
||
<hr class="docutils" />
|
||
<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 brackets" href="#id3" 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 is 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 only one 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> class, the <a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> class, is
|
||
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. The <a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> class can
|
||
represent simple timezones with fixed offset from UTC, such as UTC itself or
|
||
North American EST and EDT timezones. Supporting timezones at deeper levels of
|
||
detail is up to the application. The rules for time adjustment across the
|
||
world are more political than rational, change frequently, and there is no
|
||
standard suitable for every application aside from UTC.</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="admonition-title">See also</p>
|
||
<dl class="simple">
|
||
<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><p>General calendar related functions.</p>
|
||
</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><p>Time access and conversions.</p>
|
||
</dd>
|
||
</dl>
|
||
</div>
|
||
<div class="section" id="available-types">
|
||
<h2>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>
|
||
|
||
<dl class="class">
|
||
<dt>
|
||
<em class="property">class </em><code class="descclassname">datetime.</code><code class="descname">timezone</code></dt>
|
||
<dd><p>A class that implements 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> abstract base class as a
|
||
fixed offset from the UTC.</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 3.2.</span></p>
|
||
</div>
|
||
</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-python3 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">timezone</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><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><em>days=0</em>, <em>seconds=0</em>, <em>microseconds=0</em>, <em>milliseconds=0</em>, <em>minutes=0</em>, <em>hours=0</em>, <em>weeks=0</em><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 integers
|
||
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><p>A millisecond is converted to 1000 microseconds.</p></li>
|
||
<li><p>A minute is converted to 60 seconds.</p></li>
|
||
<li><p>An hour is converted to 3600 seconds.</p></li>
|
||
<li><p>A week is converted to 7 days.</p></li>
|
||
</ul>
|
||
<p>and days, seconds and microseconds are then normalized so that the
|
||
representation is unique, with</p>
|
||
<ul class="simple">
|
||
<li><p><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></p></li>
|
||
<li><p><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)</p></li>
|
||
<li><p><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></p></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 using
|
||
round-half-to-even tiebreaker. 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#OverflowError" title="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 class="docutils align-center">
|
||
<colgroup>
|
||
<col style="width: 29%" />
|
||
<col style="width: 71%" />
|
||
</colgroup>
|
||
<thead>
|
||
<tr class="row-odd"><th class="head"><p>Attribute</p></th>
|
||
<th class="head"><p>Value</p></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">days</span></code></p></td>
|
||
<td><p>Between -999999999 and 999999999 inclusive</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">seconds</span></code></p></td>
|
||
<td><p>Between 0 and 86399 inclusive</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">microseconds</span></code></p></td>
|
||
<td><p>Between 0 and 999999 inclusive</p></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<p>Supported operations:</p>
|
||
<table class="docutils align-center">
|
||
<colgroup>
|
||
<col style="width: 41%" />
|
||
<col style="width: 59%" />
|
||
</colgroup>
|
||
<thead>
|
||
<tr class="row-odd"><th class="head"><p>Operation</p></th>
|
||
<th class="head"><p>Result</p></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr class="row-even"><td><p><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></p></td>
|
||
<td><p>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)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><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></p></td>
|
||
<td><p>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)(6)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><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></p></td>
|
||
<td><p>Delta multiplied by an integer.
|
||
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>.</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td></td>
|
||
<td><p>In general, <em>t1</em> * i == <em>t1</em> * (i-1) + <em>t1</em>
|
||
is true. (1)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><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">f</span> <span class="pre">or</span> <span class="pre">t1</span> <span class="pre">=</span> <span class="pre">f</span> <span class="pre">*</span> <span class="pre">t2</span></code></p></td>
|
||
<td><p>Delta multiplied by a float. The result is
|
||
rounded to the nearest multiple of
|
||
timedelta.resolution using round-half-to-even.</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">f</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">/</span> <span class="pre">t3</span></code></p></td>
|
||
<td><p>Division (3) of overall duration <em>t2</em> by
|
||
interval unit <em>t3</em>. Returns a <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a>
|
||
object.</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><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">f</span> <span class="pre">or</span> <span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">/</span> <span class="pre">i</span></code></p></td>
|
||
<td><p>Delta divided by a float or an int. The result
|
||
is rounded to the nearest multiple of
|
||
timedelta.resolution using round-half-to-even.</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><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> or
|
||
<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></p></td>
|
||
<td><p>The floor is computed and the remainder (if
|
||
any) is thrown away. In the second case, an
|
||
integer is returned. (3)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><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></p></td>
|
||
<td><p>The remainder is computed 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. (3)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">q,</span> <span class="pre">r</span> <span class="pre">=</span> <span class="pre">divmod(t1,</span> <span class="pre">t2)</span></code></p></td>
|
||
<td><p>Computes the quotient and the remainder:
|
||
<code class="docutils literal notranslate"><span class="pre">q</span> <span class="pre">=</span> <span class="pre">t1</span> <span class="pre">//</span> <span class="pre">t2</span></code> (3) and <code class="docutils literal notranslate"><span class="pre">r</span> <span class="pre">=</span> <span class="pre">t1</span> <span class="pre">%</span> <span class="pre">t2</span></code>.
|
||
q is an integer and r 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.</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">+t1</span></code></p></td>
|
||
<td><p>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)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">-t1</span></code></p></td>
|
||
<td><p>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)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">abs(t)</span></code></p></td>
|
||
<td><p>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)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">str(t)</span></code></p></td>
|
||
<td><p>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)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">repr(t)</span></code></p></td>
|
||
<td><p>Returns a string representation of 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 as a constructor
|
||
call with canonical attribute values.</p></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<p>Notes:</p>
|
||
<ol class="arabic">
|
||
<li><p>This is exact, but may overflow.</p></li>
|
||
<li><p>This is exact, and cannot overflow.</p></li>
|
||
<li><p>Division by 0 raises <a class="reference internal" href="exceptions.html#ZeroDivisionError" title="ZeroDivisionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZeroDivisionError</span></code></a>.</p></li>
|
||
<li><p>-<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>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(days=-1, seconds=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>
|
||
<li><p>The expression <code class="docutils literal notranslate"><span class="pre">t2</span> <span class="pre">-</span> <span class="pre">t3</span></code> will always be equal to the expression <code class="docutils literal notranslate"><span class="pre">t2</span> <span class="pre">+</span> <span class="pre">(-t3)</span></code> except
|
||
when t3 is equal to <code class="docutils literal notranslate"><span class="pre">timedelta.max</span></code>; in that case the former will produce a result
|
||
while the latter will overflow.</p></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>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.2: </span>Floor division and true division of 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 by another
|
||
<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 are now supported, as are remainder operations and
|
||
the <a class="reference internal" href="functions.html#divmod" title="divmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">divmod()</span></code></a> function. True division and multiplication of 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 by a <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> object are now supported.</p>
|
||
</div>
|
||
<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#TypeError" title="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</span> <span class="pre">/</span> <span class="pre">timedelta(seconds=1)</span></code>. For interval units other than seconds, use the
|
||
division form directly (e.g. <code class="docutils literal notranslate"><span class="pre">td</span> <span class="pre">/</span> <span class="pre">timedelta(microseconds=1)</span></code>).</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 added">New in version 3.2.</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(days=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(days=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="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(days=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><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 integers, in the following
|
||
ranges:</p>
|
||
<ul class="simple">
|
||
<li><p><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></p></li>
|
||
<li><p><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></p></li>
|
||
<li><p><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></p></li>
|
||
</ul>
|
||
<p>If an argument outside those ranges is given, <a class="reference internal" href="exceptions.html#ValueError" title="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#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</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,
|
||
and <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> on <code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> failure.
|
||
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>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.3: </span>Raise <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> instead of <a class="reference internal" href="exceptions.html#ValueError" title="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. Raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> instead of
|
||
<a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> on <code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> failure.</p>
|
||
</div>
|
||
</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#ValueError" title="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>
|
||
|
||
<dl class="classmethod">
|
||
<dt id="datetime.date.fromisoformat">
|
||
<em class="property">classmethod </em><code class="descclassname">date.</code><code class="descname">fromisoformat</code><span class="sig-paren">(</span><em>date_string</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.fromisoformat" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>Return 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> corresponding to a <em>date_string</em> in the format emitted
|
||
by <a class="reference internal" href="#datetime.date.isoformat" title="datetime.date.isoformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">date.isoformat()</span></code></a>. Specifically, this function supports strings in
|
||
the format(s) <code class="docutils literal notranslate"><span class="pre">YYYY-MM-DD</span></code>.</p>
|
||
<div class="admonition caution">
|
||
<p class="admonition-title">Caution</p>
|
||
<p>This does not support parsing arbitrary ISO 8601 strings - it is only intended
|
||
as the inverse operation of <a class="reference internal" href="#datetime.date.isoformat" title="datetime.date.isoformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">date.isoformat()</span></code></a>.</p>
|
||
</div>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 3.7.</span></p>
|
||
</div>
|
||
</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 class="docutils align-center">
|
||
<colgroup>
|
||
<col style="width: 40%" />
|
||
<col style="width: 60%" />
|
||
</colgroup>
|
||
<thead>
|
||
<tr class="row-odd"><th class="head"><p>Operation</p></th>
|
||
<th class="head"><p>Result</p></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr class="row-even"><td><p><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></p></td>
|
||
<td><p><em>date2</em> is <code class="docutils literal notranslate"><span class="pre">timedelta.days</span></code> days removed
|
||
from <em>date1</em>. (1)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><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></p></td>
|
||
<td><p>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)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><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></p></td>
|
||
<td><p>(3)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">date1</span> <span class="pre"><</span> <span class="pre">date2</span></code></p></td>
|
||
<td><p><em>date1</em> is considered less than <em>date2</em> when
|
||
<em>date1</em> precedes <em>date2</em> in time. (4)</p></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<p>Notes:</p>
|
||
<ol class="arabic simple">
|
||
<li><p><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#OverflowError" title="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>.</p></li>
|
||
<li><p><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.</p></li>
|
||
<li><p>This is exact, and cannot overflow. timedelta.seconds and
|
||
timedelta.microseconds are 0, and date2 + timedelta == date1 after.</p></li>
|
||
<li><p>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>. Date comparison raises <a class="reference internal" href="exceptions.html#TypeError" title="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#TypeError" title="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></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=self.year</em>, <em>month=self.month</em>, <em>day=self.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
|
||
<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 in <a class="reference internal" href="../reference/lexical_analysis.html#f-strings"><span class="std std-ref">formatted string
|
||
literals</span></a> and 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>. For a
|
||
complete list of formatting directives, see
|
||
<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-python3 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-pycon3 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">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="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</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="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</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 </span><span class="si">{1}</span><span class="s1"> is {0:</span><span class="si">%d</span><span class="s1">}, the </span><span class="si">{2}</span><span class="s1"> 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><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>, <em>hour=0</em>, <em>minute=0</em>, <em>second=0</em>, <em>microsecond=0</em>, <em>tzinfo=None</em>, <em>*</em>, <em>fold=0</em><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 integers,
|
||
in the following ranges:</p>
|
||
<ul class="simple">
|
||
<li><p><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>,</p></li>
|
||
<li><p><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>,</p></li>
|
||
<li><p><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>,</p></li>
|
||
<li><p><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>,</p></li>
|
||
<li><p><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>,</p></li>
|
||
<li><p><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>,</p></li>
|
||
<li><p><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>,</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">fold</span> <span class="pre">in</span> <span class="pre">[0,</span> <span class="pre">1]</span></code>.</p></li>
|
||
</ul>
|
||
<p>If an argument outside those ranges is given, <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 3.6: </span>Added the <code class="docutils literal notranslate"><span class="pre">fold</span></code> argument.</p>
|
||
</div>
|
||
</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><em>tz=None</em><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. An aware current UTC datetime can be obtained by
|
||
calling <code class="docutils literal notranslate"><span class="pre">datetime.now(timezone.utc)</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>.</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>, <em>tz=None</em><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#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</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, and <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> on <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> failure.
|
||
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>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.3: </span>Raise <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> instead of <a class="reference internal" href="exceptions.html#ValueError" title="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. Raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>
|
||
instead of <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> on <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>
|
||
failure.</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.6: </span><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 return instances with <a class="reference internal" href="#datetime.datetime.fold" title="datetime.datetime.fold"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fold</span></code></a> set to 1.</p>
|
||
</div>
|
||
</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#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</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,
|
||
and <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> on <code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code> failure.
|
||
It’s common for this to be restricted to years in 1970 through 2038.</p>
|
||
<p>To get an aware <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, call <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>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">datetime</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span><span class="n">timestamp</span><span class="p">,</span> <span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>On the POSIX compliant platforms, it is equivalent to the following
|
||
expression:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">datetime</span><span class="p">(</span><span class="mi">1970</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</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="n">timestamp</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>except the latter formula always supports the full years range: 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>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.3: </span>Raise <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> instead of <a class="reference internal" href="exceptions.html#ValueError" title="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. Raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> instead of
|
||
<a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> on <code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code> failure.</p>
|
||
</div>
|
||
</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#ValueError" title="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>, <em>tzinfo=self.tzinfo</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
|
||
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. If the <em>tzinfo</em>
|
||
argument is provided, its value is used to set 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> attribute
|
||
of the result, otherwise the <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 of the <em>time</em> argument
|
||
is used.</p>
|
||
<p>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.time(),</span> <span class="pre">d.tzinfo)</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>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.6: </span>Added the <em>tzinfo</em> argument.</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="classmethod">
|
||
<dt id="datetime.datetime.fromisoformat">
|
||
<em class="property">classmethod </em><code class="descclassname">datetime.</code><code class="descname">fromisoformat</code><span class="sig-paren">(</span><em>date_string</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.fromisoformat" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>Return a <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> corresponding to a <em>date_string</em> in one of the
|
||
formats emitted by <a class="reference internal" href="#datetime.date.isoformat" title="datetime.date.isoformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">date.isoformat()</span></code></a> and <a class="reference internal" href="#datetime.datetime.isoformat" title="datetime.datetime.isoformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.isoformat()</span></code></a>.
|
||
Specifically, this function supports strings in the format(s)
|
||
<code class="docutils literal notranslate"><span class="pre">YYYY-MM-DD[*HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]]</span></code>,
|
||
where <code class="docutils literal notranslate"><span class="pre">*</span></code> can match any single character.</p>
|
||
<div class="admonition caution">
|
||
<p class="admonition-title">Caution</p>
|
||
<p>This does not support parsing arbitrary ISO 8601 strings - it is only intended
|
||
as the inverse operation of <a class="reference internal" href="#datetime.datetime.isoformat" title="datetime.datetime.isoformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.isoformat()</span></code></a>.</p>
|
||
</div>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 3.7.</span></p>
|
||
</div>
|
||
</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#ValueError" title="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
|
||
<a class="reference internal" href="#strftime-strptime-behavior"><span class="std std-ref">strftime() and strptime() Behavior</span></a>.</p>
|
||
</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>
|
||
|
||
<dl class="attribute">
|
||
<dt id="datetime.datetime.fold">
|
||
<code class="descclassname">datetime.</code><code class="descname">fold</code><a class="headerlink" href="#datetime.datetime.fold" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>In <code class="docutils literal notranslate"><span class="pre">[0,</span> <span class="pre">1]</span></code>. Used to disambiguate wall times during a repeated interval. (A
|
||
repeated interval occurs when clocks are rolled back at the end of daylight saving
|
||
time or when the UTC offset for the current zone is decreased for political reasons.)
|
||
The value 0 (1) represents the earlier (later) of the two moments with the same wall
|
||
time representation.</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 3.6.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<p>Supported operations:</p>
|
||
<table class="docutils align-center">
|
||
<colgroup>
|
||
<col style="width: 55%" />
|
||
<col style="width: 45%" />
|
||
</colgroup>
|
||
<thead>
|
||
<tr class="row-odd"><th class="head"><p>Operation</p></th>
|
||
<th class="head"><p>Result</p></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr class="row-even"><td><p><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></p></td>
|
||
<td><p>(1)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><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></p></td>
|
||
<td><p>(2)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><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></p></td>
|
||
<td><p>(3)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">datetime1</span> <span class="pre"><</span> <span class="pre">datetime2</span></code></p></td>
|
||
<td><p>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)</p></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<ol class="arabic">
|
||
<li><p>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#OverflowError" title="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>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.</p></li>
|
||
<li><p>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#TypeError" title="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><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#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>
|
||
is raised if an order comparison is attempted. For equality
|
||
comparisons, naive instances are never equal to aware instances.</p>
|
||
<p>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="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.3: </span>Equality comparisons between naive and aware <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 don’t raise <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>.</p>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>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#TypeError" title="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#TypeError" title="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, microsecond and fold.
|
||
<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>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.6: </span>The fold value is copied to the returned <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.</p>
|
||
</div>
|
||
</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, fold, 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>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.6: </span>The fold value is copied to the returned <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.</p>
|
||
</div>
|
||
</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><em>year=self.year</em>, <em>month=self.month</em>, <em>day=self.day</em>, <em>hour=self.hour</em>, <em>minute=self.minute</em>, <em>second=self.second</em>, <em>microsecond=self.microsecond</em>, <em>tzinfo=self.tzinfo</em>, <em>* fold=0</em><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>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 3.6: </span>Added the <code class="docutils literal notranslate"><span class="pre">fold</span></code> argument.</p>
|
||
</div>
|
||
</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=None</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>If provided, <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>. If <em>self</em>
|
||
is naive, it is presumed to represent time in the system timezone.</p>
|
||
<p>If called without arguments (or with <code class="docutils literal notranslate"><span class="pre">tz=None</span></code>) the system local
|
||
timezone is assumed for the target timezone. The <code class="docutils literal notranslate"><span class="pre">.tzinfo</span></code> attribute of the converted
|
||
datetime instance will be set to an instance of <a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a>
|
||
with the zone name and offset obtained from the OS.</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 the timezone <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 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>.</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-python3 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>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.3: </span><em>tz</em> now can be omitted.</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.6: </span>The <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> method can now be called on naive instances that
|
||
are presumed to represent system local time.</p>
|
||
</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 with magnitude less than one day.</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.7: </span>The UTC offset is not restricted to a whole number of minutes.</p>
|
||
</div>
|
||
</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 with magnitude less than one day.</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.7: </span>The DST offset is not restricted to a whole number of minutes.</p>
|
||
</div>
|
||
</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 an <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> may be raised 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.timestamp">
|
||
<code class="descclassname">datetime.</code><code class="descname">timestamp</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.timestamp" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>Return POSIX timestamp 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>
|
||
instance. The return value is a <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> similar to that
|
||
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>.</p>
|
||
<p>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> instances are assumed to represent local
|
||
time and this method relies on the platform C <code class="xref c c-func docutils literal notranslate"><span class="pre">mktime()</span></code>
|
||
function to perform the conversion. Since <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>
|
||
supports wider range of values than <code class="xref c c-func docutils literal notranslate"><span class="pre">mktime()</span></code> on many
|
||
platforms, this method may raise <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> for times far
|
||
in the past or far in the future.</p>
|
||
<p>For aware <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, the return value is computed
|
||
as:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">dt</span> <span class="o">-</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1970</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">))</span><span class="o">.</span><span class="n">total_seconds</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 3.3.</span></p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.6: </span>The <a class="reference internal" href="#datetime.datetime.timestamp" title="datetime.datetime.timestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">timestamp()</span></code></a> method uses the <a class="reference internal" href="#datetime.datetime.fold" title="datetime.datetime.fold"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fold</span></code></a> attribute to
|
||
disambiguate the times during a repeated interval.</p>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>There is no method to obtain the POSIX timestamp directly from 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> instance representing UTC time. If your
|
||
application uses this convention and your system timezone is not
|
||
set to UTC, you can obtain the POSIX timestamp by supplying
|
||
<code class="docutils literal notranslate"><span class="pre">tzinfo=timezone.utc</span></code>:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">timestamp</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="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span><span class="o">.</span><span class="n">timestamp</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>or by calculating the timestamp directly:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">timestamp</span> <span class="o">=</span> <span class="p">(</span><span class="n">dt</span> <span class="o">-</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1970</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="o">/</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">seconds</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</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><em>sep='T'</em>, <em>timespec='auto'</em><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.ffffff 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 string is
|
||
appended, giving the UTC offset:
|
||
YYYY-MM-DDTHH:MM:SS.ffffff+HH:MM[:SS[.ffffff]] 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[:SS[.ffffff]].</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>
|
||
<p>The optional argument <em>timespec</em> specifies the number of additional
|
||
components of the time to include (the default is <code class="docutils literal notranslate"><span class="pre">'auto'</span></code>).
|
||
It can be one of the following:</p>
|
||
<ul class="simple">
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">'auto'</span></code>: Same as <code class="docutils literal notranslate"><span class="pre">'seconds'</span></code> 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,
|
||
same as <code class="docutils literal notranslate"><span class="pre">'microseconds'</span></code> otherwise.</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">'hours'</span></code>: Include the <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> in the two-digit HH format.</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">'minutes'</span></code>: Include <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> and <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> in HH:MM format.</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">'seconds'</span></code>: Include <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>, and <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>
|
||
in HH:MM:SS format.</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">'milliseconds'</span></code>: Include full time, but truncate fractional second
|
||
part to milliseconds. HH:MM:SS.sss format.</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">'microseconds'</span></code>: Include full time in HH:MM:SS.ffffff format.</p></li>
|
||
</ul>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>Excluded time components are truncated, not rounded.</p>
|
||
</div>
|
||
<p><a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> will be raised on an invalid <em>timespec</em> argument.</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">datetime</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="o">.</span><span class="n">isoformat</span><span class="p">(</span><span class="n">timespec</span><span class="o">=</span><span class="s1">'minutes'</span><span class="p">)</span> <span class="c1"># doctest: +SKIP</span>
|
||
<span class="go">'2002-12-25T00:00'</span>
|
||
<span class="gp">>>> </span><span class="n">dt</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2015</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="mi">12</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">59</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">dt</span><span class="o">.</span><span class="n">isoformat</span><span class="p">(</span><span class="n">timespec</span><span class="o">=</span><span class="s1">'microseconds'</span><span class="p">)</span>
|
||
<span class="go">'2015-01-01T12:30:59.000000'</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 3.6: </span>Added the <em>timespec</em> argument.</p>
|
||
</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
|
||
<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 in <a class="reference internal" href="../reference/lexical_analysis.html#f-strings"><span class="std std-ref">formatted string
|
||
literals</span></a> and 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>. For a
|
||
complete list of formatting directives, see
|
||
<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-pycon3 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">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="nb">print</span><span class="p">(</span><span class="n">it</span><span class="p">)</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="nb">print</span><span class="p">(</span><span class="n">it</span><span class="p">)</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 </span><span class="si">{1}</span><span class="s1"> is {0:</span><span class="si">%d</span><span class="s1">}, the </span><span class="si">{2}</span><span class="s1"> is {0:%B}, the </span><span class="si">{3}</span><span class="s1"> 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="p">,</span> <span class="n">timezone</span>
|
||
<span class="gp">>>> </span><span class="k">class</span> <span class="nc">KabulTz</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span>
|
||
<span class="gp">... </span> <span class="c1"># Kabul used +4 until 1945, when they moved to +4:30</span>
|
||
<span class="gp">... </span> <span class="n">UTC_MOVE_DATE</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1944</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">31</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</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">if</span> <span class="n">dt</span><span class="o">.</span><span class="n">year</span> <span class="o"><</span> <span class="mi">1945</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">4</span><span class="p">)</span>
|
||
<span class="gp">... </span> <span class="k">elif</span> <span class="p">(</span><span class="mi">1945</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="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</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="mi">5</span><span class="p">]</span> <span class="o"><</span> <span class="p">(</span><span class="mi">1945</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="mi">0</span><span class="p">,</span> <span class="mi">30</span><span class="p">):</span>
|
||
<span class="gp">... </span> <span class="c1"># If dt falls in the imaginary range, use fold to decide how</span>
|
||
<span class="gp">... </span> <span class="c1"># to resolve. See PEP495</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">4</span><span class="p">,</span> <span class="n">minutes</span><span class="o">=</span><span class="p">(</span><span class="mi">30</span> <span class="k">if</span> <span class="n">dt</span><span class="o">.</span><span class="n">fold</span> <span class="k">else</span> <span class="mi">0</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="n">hours</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">minutes</span><span class="o">=</span><span class="mi">30</span><span class="p">)</span>
|
||
<span class="gp">...</span>
|
||
<span class="gp">... </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="gp">... </span> <span class="c1"># A custom implementation is required for fromutc as</span>
|
||
<span class="gp">... </span> <span class="c1"># the input to this function is a datetime with utc values</span>
|
||
<span class="gp">... </span> <span class="c1"># but with a tzinfo set to self</span>
|
||
<span class="gp">... </span> <span class="c1"># See datetime.astimezone or fromtimestamp</span>
|
||
<span class="gp">...</span>
|
||
<span class="gp">... </span> <span class="c1"># Follow same validations as in datetime.tzinfo</span>
|
||
<span class="gp">... </span> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dt</span><span class="p">,</span> <span class="n">datetime</span><span class="p">):</span>
|
||
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">"fromutc() requires a datetime argument"</span><span class="p">)</span>
|
||
<span class="gp">... </span> <span class="k">if</span> <span class="n">dt</span><span class="o">.</span><span class="n">tzinfo</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">self</span><span class="p">:</span>
|
||
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">"dt.tzinfo is not self"</span><span class="p">)</span>
|
||
<span class="gp">...</span>
|
||
<span class="gp">... </span> <span class="k">if</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="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span> <span class="o">>=</span> <span class="bp">self</span><span class="o">.</span><span class="n">UTC_MOVE_DATE</span><span class="p">:</span>
|
||
<span class="gp">... </span> <span class="k">return</span> <span class="n">dt</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">4</span><span class="p">,</span> <span class="n">minutes</span><span class="o">=</span><span class="mi">30</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">dt</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">4</span><span class="p">)</span>
|
||
<span class="gp">...</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="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">if</span> <span class="n">dt</span> <span class="o">>=</span> <span class="bp">self</span><span class="o">.</span><span class="n">UTC_MOVE_DATE</span><span class="p">:</span>
|
||
<span class="gp">... </span> <span class="k">return</span> <span class="s2">"+04:30"</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="s2">"+04"</span>
|
||
<span class="gp">...</span>
|
||
<span class="gp">... </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="gp">... </span> <span class="k">return</span> <span class="n">f</span><span class="s2">"</span><span class="si">{self.__class__.__name__}</span><span class="s2">()"</span>
|
||
<span class="gp">...</span>
|
||
<span class="gp">>>> </span><span class="n">tz1</span> <span class="o">=</span> <span class="n">KabulTz</span><span class="p">()</span>
|
||
<span class="gp">>>> </span><span class="c1"># Datetime before the change</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">1900</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">tz1</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">dt1</span><span class="o">.</span><span class="n">utcoffset</span><span class="p">())</span>
|
||
<span class="go">4:00:00</span>
|
||
<span class="gp">>>> </span><span class="c1"># Datetime after the change</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">tz1</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">dt2</span><span class="o">.</span><span class="n">utcoffset</span><span class="p">())</span>
|
||
<span class="go">4:30:00</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">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">dt3</span>
|
||
<span class="go">datetime.datetime(2006, 6, 14, 8, 30, tzinfo=datetime.timezone.utc)</span>
|
||
<span class="gp">>>> </span><span class="n">dt2</span>
|
||
<span class="go">datetime.datetime(2006, 6, 14, 13, 0, tzinfo=KabulTz())</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><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><em>hour=0</em>, <em>minute=0</em>, <em>second=0</em>, <em>microsecond=0</em>, <em>tzinfo=None</em>, <em>*</em>, <em>fold=0</em><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 integers, in the
|
||
following ranges:</p>
|
||
<ul class="simple">
|
||
<li><p><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>,</p></li>
|
||
<li><p><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>,</p></li>
|
||
<li><p><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>,</p></li>
|
||
<li><p><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>,</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">fold</span> <span class="pre">in</span> <span class="pre">[0,</span> <span class="pre">1]</span></code>.</p></li>
|
||
</ul>
|
||
<p>If an argument outside those ranges is given, <a class="reference internal" href="exceptions.html#ValueError" title="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>
|
||
|
||
<dl class="attribute">
|
||
<dt id="datetime.time.fold">
|
||
<code class="descclassname">time.</code><code class="descname">fold</code><a class="headerlink" href="#datetime.time.fold" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>In <code class="docutils literal notranslate"><span class="pre">[0,</span> <span class="pre">1]</span></code>. Used to disambiguate wall times during a repeated interval. (A
|
||
repeated interval occurs when clocks are rolled back at the end of daylight saving
|
||
time or when the UTC offset for the current zone is decreased for political reasons.)
|
||
The value 0 (1) represents the earlier (later) of the two moments with the same wall
|
||
time representation.</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 3.6.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<p>Supported operations:</p>
|
||
<ul>
|
||
<li><p>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#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised if an order comparison is attempted. For equality
|
||
comparisons, naive instances are never equal to aware instances.</p>
|
||
<p>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#TypeError" title="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 class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.3: </span>Equality comparisons between naive and 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> instances
|
||
don’t raise <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>.</p>
|
||
</div>
|
||
</li>
|
||
<li><p>hash, use as dict key</p></li>
|
||
<li><p>efficient pickling</p></li>
|
||
</ul>
|
||
<p>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 always considered to be true.</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.5: </span>Before Python 3.5, 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 was considered to be false if it
|
||
represented midnight in UTC. This behavior was considered obscure and
|
||
error-prone and has been removed in Python 3.5. See <a class="reference external" href="https://bugs.python.org/issue13936">bpo-13936</a> for full
|
||
details.</p>
|
||
</div>
|
||
<p>Other constructor:</p>
|
||
<dl class="classmethod">
|
||
<dt id="datetime.time.fromisoformat">
|
||
<em class="property">classmethod </em><code class="descclassname">time.</code><code class="descname">fromisoformat</code><span class="sig-paren">(</span><em>time_string</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.time.fromisoformat" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>Return a <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> corresponding to a <em>time_string</em> in one of the
|
||
formats emitted by <a class="reference internal" href="#datetime.time.isoformat" title="datetime.time.isoformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">time.isoformat()</span></code></a>. Specifically, this function supports
|
||
strings in the format(s) <code class="docutils literal notranslate"><span class="pre">HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]</span></code>.</p>
|
||
<div class="admonition caution">
|
||
<p class="admonition-title">Caution</p>
|
||
<p>This does not support parsing arbitrary ISO 8601 strings - it is only intended
|
||
as the inverse operation of <a class="reference internal" href="#datetime.time.isoformat" title="datetime.time.isoformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">time.isoformat()</span></code></a>.</p>
|
||
</div>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 3.7.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<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><em>hour=self.hour</em>, <em>minute=self.minute</em>, <em>second=self.second</em>, <em>microsecond=self.microsecond</em>, <em>tzinfo=self.tzinfo</em>, <em>* fold=0</em><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>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 3.6: </span>Added the <code class="docutils literal notranslate"><span class="pre">fold</span></code> argument.</p>
|
||
</div>
|
||
</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><em>timespec='auto'</em><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.ffffff or, if
|
||
<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> 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
|
||
string is appended, giving the UTC offset: HH:MM:SS.ffffff+HH:MM[:SS[.ffffff]]
|
||
or, if self.microsecond is 0, HH:MM:SS+HH:MM[:SS[.ffffff]].</p>
|
||
<p>The optional argument <em>timespec</em> specifies the number of additional
|
||
components of the time to include (the default is <code class="docutils literal notranslate"><span class="pre">'auto'</span></code>).
|
||
It can be one of the following:</p>
|
||
<ul class="simple">
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">'auto'</span></code>: Same as <code class="docutils literal notranslate"><span class="pre">'seconds'</span></code> if <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> is 0,
|
||
same as <code class="docutils literal notranslate"><span class="pre">'microseconds'</span></code> otherwise.</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">'hours'</span></code>: Include the <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> in the two-digit HH format.</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">'minutes'</span></code>: Include <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> and <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> in HH:MM format.</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">'seconds'</span></code>: Include <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>, and <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>
|
||
in HH:MM:SS format.</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">'milliseconds'</span></code>: Include full time, but truncate fractional second
|
||
part to milliseconds. HH:MM:SS.sss format.</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">'microseconds'</span></code>: Include full time in HH:MM:SS.ffffff format.</p></li>
|
||
</ul>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>Excluded time components are truncated, not rounded.</p>
|
||
</div>
|
||
<p><a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> will be raised on an invalid <em>timespec</em> argument.</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="gp">>>> </span><span class="n">time</span><span class="p">(</span><span class="n">hour</span><span class="o">=</span><span class="mi">12</span><span class="p">,</span> <span class="n">minute</span><span class="o">=</span><span class="mi">34</span><span class="p">,</span> <span class="n">second</span><span class="o">=</span><span class="mi">56</span><span class="p">,</span> <span class="n">microsecond</span><span class="o">=</span><span class="mi">123456</span><span class="p">)</span><span class="o">.</span><span class="n">isoformat</span><span class="p">(</span><span class="n">timespec</span><span class="o">=</span><span class="s1">'minutes'</span><span class="p">)</span>
|
||
<span class="go">'12:34'</span>
|
||
<span class="gp">>>> </span><span class="n">dt</span> <span class="o">=</span> <span class="n">time</span><span class="p">(</span><span class="n">hour</span><span class="o">=</span><span class="mi">12</span><span class="p">,</span> <span class="n">minute</span><span class="o">=</span><span class="mi">34</span><span class="p">,</span> <span class="n">second</span><span class="o">=</span><span class="mi">56</span><span class="p">,</span> <span class="n">microsecond</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">dt</span><span class="o">.</span><span class="n">isoformat</span><span class="p">(</span><span class="n">timespec</span><span class="o">=</span><span class="s1">'microseconds'</span><span class="p">)</span>
|
||
<span class="go">'12:34:56.000000'</span>
|
||
<span class="gp">>>> </span><span class="n">dt</span><span class="o">.</span><span class="n">isoformat</span><span class="p">(</span><span class="n">timespec</span><span class="o">=</span><span class="s1">'auto'</span><span class="p">)</span>
|
||
<span class="go">'12:34:56'</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 3.6: </span>Added the <em>timespec</em> argument.</p>
|
||
</div>
|
||
</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
|
||
<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 in <a class="reference internal" href="../reference/lexical_analysis.html#f-strings"><span class="std std-ref">formatted string
|
||
literals</span></a> and 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>. For a
|
||
complete list of formatting directives, see
|
||
<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 with magnitude less than one day.</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.7: </span>The UTC offset is not restricted to a whole number of minutes.</p>
|
||
</div>
|
||
</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 with magnitude less than one day.</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.7: </span>The DST offset is not restricted to a whole number of minutes.</p>
|
||
</div>
|
||
</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">TZ1</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">"+01:00"</span>
|
||
<span class="gp">... </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="gp">... </span> <span class="k">return</span> <span class="n">f</span><span class="s2">"</span><span class="si">{self.__class__.__name__}</span><span class="s2">()"</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">TZ1</span><span class="p">())</span>
|
||
<span class="gp">>>> </span><span class="n">t</span>
|
||
<span class="go">datetime.time(12, 10, 30, tzinfo=TZ1())</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">'+01:00'</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 +01:00'</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><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 supplies
|
||
a simple 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>, <a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a>, which can represent
|
||
timezones with fixed offset from UTC such as UTC itself or North American EST and
|
||
EDT.</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>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, 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 that is
|
||
positive 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 strictly between <code class="docutils literal notranslate"><span class="pre">-timedelta(hours=24)</span></code> and
|
||
<code class="docutils literal notranslate"><span class="pre">timedelta(hours=24)</span></code> (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-python3 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#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.7: </span>The UTC offset is not restricted to a whole number of minutes.</p>
|
||
</div>
|
||
</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>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, 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 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-python3 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-python3 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#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.7: </span>The DST offset is not restricted to a whole number of minutes.</p>
|
||
</div>
|
||
</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>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#NotImplementedError" title="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>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-python3 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>In the following <a class="reference download internal" download="" href="../_downloads/6bc7a57013e3076d5e194caa123fe59c/tzinfo_examples.py"><code class="xref download docutils literal notranslate"><span class="pre">tzinfo_examples.py</span></code></a> file there are some examples 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> classes:</p>
|
||
<div class="highlight-python3 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="n">SECOND</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="mi">1</span><span class="p">)</span>
|
||
|
||
<span class="c1"># A class capturing the platform's idea of local time.</span>
|
||
<span class="c1"># (May result in wrong values on historical times in</span>
|
||
<span class="c1"># timezones where UTC offset and/or the DST rules had</span>
|
||
<span class="c1"># changed in the past.)</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">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="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="n">stamp</span> <span class="o">=</span> <span class="p">(</span><span class="n">dt</span> <span class="o">-</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1970</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="bp">self</span><span class="p">))</span> <span class="o">//</span> <span class="n">SECOND</span>
|
||
<span class="n">args</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="mi">6</span><span class="p">]</span>
|
||
<span class="n">dst_diff</span> <span class="o">=</span> <span class="n">DSTDIFF</span> <span class="o">//</span> <span class="n">SECOND</span>
|
||
<span class="c1"># Detect fold</span>
|
||
<span class="n">fold</span> <span class="o">=</span> <span class="p">(</span><span class="n">args</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="o">-</span> <span class="n">dst_diff</span><span class="p">))</span>
|
||
<span class="k">return</span> <span class="n">datetime</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">microsecond</span><span class="o">=</span><span class="n">dt</span><span class="o">.</span><span class="n">microsecond</span><span class="p">,</span>
|
||
<span class="n">tzinfo</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span> <span class="n">fold</span><span class="o">=</span><span class="n">fold</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) 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">2</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) 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">2</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"># 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">def</span> <span class="nf">us_dst_range</span><span class="p">(</span><span class="n">year</span><span class="p">):</span>
|
||
<span class="c1"># Find start and end times for US DST. For years before 1967, return</span>
|
||
<span class="c1"># start = end for no DST.</span>
|
||
<span class="k">if</span> <span class="mi">2006</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">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">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="p">(</span><span class="n">datetime</span><span class="p">(</span><span class="n">year</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="p">)</span> <span class="o">*</span> <span class="mi">2</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">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">year</span><span class="p">))</span>
|
||
<span class="k">return</span> <span class="n">start</span><span class="p">,</span> <span class="n">end</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="n">start</span><span class="p">,</span> <span class="n">end</span> <span class="o">=</span> <span class="n">us_dst_range</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="c1"># Can't compare naive to aware objects, so strip the timezone from</span>
|
||
<span class="c1"># dt first.</span>
|
||
<span class="n">dt</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="k">if</span> <span class="n">start</span> <span class="o">+</span> <span class="n">HOUR</span> <span class="o"><=</span> <span class="n">dt</span> <span class="o"><</span> <span class="n">end</span> <span class="o">-</span> <span class="n">HOUR</span><span class="p">:</span>
|
||
<span class="c1"># DST is in effect.</span>
|
||
<span class="k">return</span> <span class="n">HOUR</span>
|
||
<span class="k">if</span> <span class="n">end</span> <span class="o">-</span> <span class="n">HOUR</span> <span class="o"><=</span> <span class="n">dt</span> <span class="o"><</span> <span class="n">end</span><span class="p">:</span>
|
||
<span class="c1"># Fold (an ambiguous hour): use dt.fold to disambiguate.</span>
|
||
<span class="k">return</span> <span class="n">ZERO</span> <span class="k">if</span> <span class="n">dt</span><span class="o">.</span><span class="n">fold</span> <span class="k">else</span> <span class="n">HOUR</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">start</span> <span class="o">+</span> <span class="n">HOUR</span><span class="p">:</span>
|
||
<span class="c1"># Gap (a non-existent hour): reverse the fold rule.</span>
|
||
<span class="k">return</span> <span class="n">HOUR</span> <span class="k">if</span> <span class="n">dt</span><span class="o">.</span><span class="n">fold</span> <span class="k">else</span> <span class="n">ZERO</span>
|
||
<span class="c1"># DST is off.</span>
|
||
<span class="k">return</span> <span class="n">ZERO</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="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="n">start</span><span class="p">,</span> <span class="n">end</span> <span class="o">=</span> <span class="n">us_dst_range</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">start</span> <span class="o">=</span> <span class="n">start</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="bp">self</span><span class="p">)</span>
|
||
<span class="n">end</span> <span class="o">=</span> <span class="n">end</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="bp">self</span><span class="p">)</span>
|
||
<span class="n">std_time</span> <span class="o">=</span> <span class="n">dt</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">stdoffset</span>
|
||
<span class="n">dst_time</span> <span class="o">=</span> <span class="n">std_time</span> <span class="o">+</span> <span class="n">HOUR</span>
|
||
<span class="k">if</span> <span class="n">end</span> <span class="o"><=</span> <span class="n">dst_time</span> <span class="o"><</span> <span class="n">end</span> <span class="o">+</span> <span class="n">HOUR</span><span class="p">:</span>
|
||
<span class="c1"># Repeated hour</span>
|
||
<span class="k">return</span> <span class="n">std_time</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">fold</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
|
||
<span class="k">if</span> <span class="n">std_time</span> <span class="o"><</span> <span class="n">start</span> <span class="ow">or</span> <span class="n">dst_time</span> <span class="o">>=</span> <span class="n">end</span><span class="p">:</span>
|
||
<span class="c1"># Standard time</span>
|
||
<span class="k">return</span> <span class="n">std_time</span>
|
||
<span class="k">if</span> <span class="n">start</span> <span class="o"><=</span> <span class="n">std_time</span> <span class="o"><</span> <span class="n">end</span> <span class="o">-</span> <span class="n">HOUR</span><span class="p">:</span>
|
||
<span class="c1"># Daylight saving time</span>
|
||
<span class="k">return</span> <span class="n">dst_time</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-python3 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. For example, at the Spring forward transition of 2016, we get</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">datetime</span><span class="p">,</span> <span class="n">timezone</span>
|
||
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">tzinfo_examples</span> <span class="k">import</span> <span class="n">HOUR</span><span class="p">,</span> <span class="n">Eastern</span>
|
||
<span class="gp">>>> </span><span class="n">u0</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2016</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</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="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">):</span>
|
||
<span class="gp">... </span> <span class="n">u</span> <span class="o">=</span> <span class="n">u0</span> <span class="o">+</span> <span class="n">i</span><span class="o">*</span><span class="n">HOUR</span>
|
||
<span class="gp">... </span> <span class="n">t</span> <span class="o">=</span> <span class="n">u</span><span class="o">.</span><span class="n">astimezone</span><span class="p">(</span><span class="n">Eastern</span><span class="p">)</span>
|
||
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">u</span><span class="o">.</span><span class="n">time</span><span class="p">(),</span> <span class="s1">'UTC ='</span><span class="p">,</span> <span class="n">t</span><span class="o">.</span><span class="n">time</span><span class="p">(),</span> <span class="n">t</span><span class="o">.</span><span class="n">tzname</span><span class="p">())</span>
|
||
<span class="gp">...</span>
|
||
<span class="go">05:00:00 UTC = 00:00:00 EST</span>
|
||
<span class="go">06:00:00 UTC = 01:00:00 EST</span>
|
||
<span class="go">07:00:00 UTC = 03:00:00 EDT</span>
|
||
<span class="go">08:00:00 UTC = 04:00:00 EDT</span>
|
||
</pre></div>
|
||
</div>
|
||
<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, but earlier times
|
||
have the <a class="reference internal" href="#datetime.datetime.fold" title="datetime.datetime.fold"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fold</span></code></a> attribute set to 0 and the later times have it set to 1.
|
||
For example, at the Fall back transition of 2016, we get</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">u0</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2016</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</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="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">):</span>
|
||
<span class="gp">... </span> <span class="n">u</span> <span class="o">=</span> <span class="n">u0</span> <span class="o">+</span> <span class="n">i</span><span class="o">*</span><span class="n">HOUR</span>
|
||
<span class="gp">... </span> <span class="n">t</span> <span class="o">=</span> <span class="n">u</span><span class="o">.</span><span class="n">astimezone</span><span class="p">(</span><span class="n">Eastern</span><span class="p">)</span>
|
||
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">u</span><span class="o">.</span><span class="n">time</span><span class="p">(),</span> <span class="s1">'UTC ='</span><span class="p">,</span> <span class="n">t</span><span class="o">.</span><span class="n">time</span><span class="p">(),</span> <span class="n">t</span><span class="o">.</span><span class="n">tzname</span><span class="p">(),</span> <span class="n">t</span><span class="o">.</span><span class="n">fold</span><span class="p">)</span>
|
||
<span class="gp">...</span>
|
||
<span class="go">04:00:00 UTC = 00:00:00 EDT 0</span>
|
||
<span class="go">05:00:00 UTC = 01:00:00 EDT 0</span>
|
||
<span class="go">06:00:00 UTC = 01:00:00 EST 1</span>
|
||
<span class="go">07:00:00 UTC = 02:00:00 EST 0</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Note that the <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> instances that differ only by the value of the
|
||
<a class="reference internal" href="#datetime.datetime.fold" title="datetime.datetime.fold"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fold</span></code></a> attribute are considered equal in comparisons.</p>
|
||
<p>Applications that can’t bear wall-time ambiguities should explicitly check the
|
||
value of the <a class="reference internal" href="#datetime.datetime.fold" title="datetime.datetime.fold"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fold</span></code></a> attribute or 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 <a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a>,
|
||
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="admonition-title">See also</p>
|
||
<dl>
|
||
<dt><a class="reference external" href="https://dateutil.readthedocs.io/en/stable/tz.html">dateutil.tz</a></dt><dd><p>The standard library has <a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> class for handling arbitrary
|
||
fixed offsets from UTC and <a class="reference internal" href="#datetime.timezone.utc" title="datetime.timezone.utc"><code class="xref py py-attr docutils literal notranslate"><span class="pre">timezone.utc</span></code></a> as UTC timezone instance.</p>
|
||
<p><em>dateutil.tz</em> library brings the <em>IANA timezone database</em> (also known as the
|
||
Olson database) to Python 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><p>The Time Zone Database (often called tz, tzdata 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.</p>
|
||
</dd>
|
||
</dl>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="timezone-objects">
|
||
<span id="datetime-timezone"></span><h2><a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> Objects<a class="headerlink" href="#timezone-objects" title="Permalink to this headline">¶</a></h2>
|
||
<p>The <a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> class is a 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>, each
|
||
instance of which represents a timezone defined by a fixed offset from
|
||
UTC. Note that objects of this class cannot be used to represent
|
||
timezone information in the locations where different offsets are used
|
||
in different days of the year or where historical changes have been
|
||
made to civil time.</p>
|
||
<dl class="class">
|
||
<dt id="datetime.timezone">
|
||
<em class="property">class </em><code class="descclassname">datetime.</code><code class="descname">timezone</code><span class="sig-paren">(</span><em>offset</em>, <em>name=None</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.timezone" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>The <em>offset</em> argument must be specified 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 representing the difference between the local time and UTC. It must
|
||
be strictly between <code class="docutils literal notranslate"><span class="pre">-timedelta(hours=24)</span></code> and
|
||
<code class="docutils literal notranslate"><span class="pre">timedelta(hours=24)</span></code>, otherwise <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.</p>
|
||
<p>The <em>name</em> argument is optional. If specified it must be a string that
|
||
will be used as the value returned by the <a class="reference internal" href="#datetime.datetime.tzname" title="datetime.datetime.tzname"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.tzname()</span></code></a> method.</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 3.2.</span></p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.7: </span>The UTC offset is not restricted to a whole number of minutes.</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="datetime.timezone.utcoffset">
|
||
<code class="descclassname">timezone.</code><code class="descname">utcoffset</code><span class="sig-paren">(</span><em>dt</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.timezone.utcoffset" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>Return the fixed value specified when the <a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> instance is
|
||
constructed. The <em>dt</em> argument is ignored. The return value 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> instance equal to the difference between the
|
||
local time and UTC.</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.7: </span>The UTC offset is not restricted to a whole number of minutes.</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="datetime.timezone.tzname">
|
||
<code class="descclassname">timezone.</code><code class="descname">tzname</code><span class="sig-paren">(</span><em>dt</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.timezone.tzname" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>Return the fixed value specified when the <a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> instance
|
||
is constructed. If <em>name</em> is not provided in the constructor, the
|
||
name returned by <code class="docutils literal notranslate"><span class="pre">tzname(dt)</span></code> is generated from the value of the
|
||
<code class="docutils literal notranslate"><span class="pre">offset</span></code> as follows. If <em>offset</em> is <code class="docutils literal notranslate"><span class="pre">timedelta(0)</span></code>, the name
|
||
is “UTC”, otherwise it is a string ‘UTC±HH:MM’, where ± is the sign
|
||
of <code class="docutils literal notranslate"><span class="pre">offset</span></code>, HH and MM are two digits of <code class="docutils literal notranslate"><span class="pre">offset.hours</span></code> and
|
||
<code class="docutils literal notranslate"><span class="pre">offset.minutes</span></code> respectively.</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.6: </span>Name generated from <code class="docutils literal notranslate"><span class="pre">offset=timedelta(0)</span></code> is now plain ‘UTC’, not
|
||
‘UTC+00:00’.</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="datetime.timezone.dst">
|
||
<code class="descclassname">timezone.</code><code class="descname">dst</code><span class="sig-paren">(</span><em>dt</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.timezone.dst" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>Always returns <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="datetime.timezone.fromutc">
|
||
<code class="descclassname">timezone.</code><code class="descname">fromutc</code><span class="sig-paren">(</span><em>dt</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.timezone.fromutc" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>Return <code class="docutils literal notranslate"><span class="pre">dt</span> <span class="pre">+</span> <span class="pre">offset</span></code>. The <em>dt</em> argument must be an aware
|
||
<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, with <code class="docutils literal notranslate"><span class="pre">tzinfo</span></code> set to <code class="docutils literal notranslate"><span class="pre">self</span></code>.</p>
|
||
</dd></dl>
|
||
|
||
<p>Class attributes:</p>
|
||
<dl class="attribute">
|
||
<dt id="datetime.timezone.utc">
|
||
<code class="descclassname">timezone.</code><code class="descname">utc</code><a class="headerlink" href="#datetime.timezone.utc" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>The UTC timezone, <code class="docutils literal notranslate"><span class="pre">timezone(timedelta(0))</span></code>.</p>
|
||
</dd></dl>
|
||
|
||
</div>
|
||
<div class="section" id="strftime-and-strptime-behavior">
|
||
<span id="strftime-strptime-behavior"></span><span id="index-0"></span><h2><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>For 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, the default value is <code class="docutils literal notranslate"><span class="pre">1900-01-01T00:00:00.000</span></code>:
|
||
any components not specified in the format string will be pulled from the default value. <a class="footnote-reference brackets" href="#id4" id="id2">2</a></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#UnicodeError" title="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>
|
||
<table class="docutils align-center">
|
||
<colgroup>
|
||
<col style="width: 15%" />
|
||
<col style="width: 43%" />
|
||
<col style="width: 32%" />
|
||
<col style="width: 9%" />
|
||
</colgroup>
|
||
<thead>
|
||
<tr class="row-odd"><th class="head"><p>Directive</p></th>
|
||
<th class="head"><p>Meaning</p></th>
|
||
<th class="head"><p>Example</p></th>
|
||
<th class="head"><p>Notes</p></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%a</span></code></p></td>
|
||
<td><p>Weekday as locale’s
|
||
abbreviated name.</p></td>
|
||
<td><div class="line-block">
|
||
<div class="line">Sun, Mon, …, Sat
|
||
(en_US);</div>
|
||
<div class="line">So, Mo, …, Sa
|
||
(de_DE)</div>
|
||
</div>
|
||
</td>
|
||
<td><p>(1)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%A</span></code></p></td>
|
||
<td><p>Weekday as locale’s full name.</p></td>
|
||
<td><div class="line-block">
|
||
<div class="line">Sunday, Monday, …,
|
||
Saturday (en_US);</div>
|
||
<div class="line">Sonntag, Montag, …,
|
||
Samstag (de_DE)</div>
|
||
</div>
|
||
</td>
|
||
<td><p>(1)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%w</span></code></p></td>
|
||
<td><p>Weekday as a decimal number,
|
||
where 0 is Sunday and 6 is
|
||
Saturday.</p></td>
|
||
<td><p>0, 1, …, 6</p></td>
|
||
<td></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%d</span></code></p></td>
|
||
<td><p>Day of the month as a
|
||
zero-padded decimal number.</p></td>
|
||
<td><p>01, 02, …, 31</p></td>
|
||
<td><p>(9)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%b</span></code></p></td>
|
||
<td><p>Month as locale’s abbreviated
|
||
name.</p></td>
|
||
<td><div class="line-block">
|
||
<div class="line">Jan, Feb, …, Dec
|
||
(en_US);</div>
|
||
<div class="line">Jan, Feb, …, Dez
|
||
(de_DE)</div>
|
||
</div>
|
||
</td>
|
||
<td><p>(1)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%B</span></code></p></td>
|
||
<td><p>Month as locale’s full name.</p></td>
|
||
<td><div class="line-block">
|
||
<div class="line">January, February,
|
||
…, December (en_US);</div>
|
||
<div class="line">Januar, Februar, …,
|
||
Dezember (de_DE)</div>
|
||
</div>
|
||
</td>
|
||
<td><p>(1)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%m</span></code></p></td>
|
||
<td><p>Month as a zero-padded
|
||
decimal number.</p></td>
|
||
<td><p>01, 02, …, 12</p></td>
|
||
<td><p>(9)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%y</span></code></p></td>
|
||
<td><p>Year without century as a
|
||
zero-padded decimal number.</p></td>
|
||
<td><p>00, 01, …, 99</p></td>
|
||
<td><p>(9)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%Y</span></code></p></td>
|
||
<td><p>Year with century as a decimal
|
||
number.</p></td>
|
||
<td><p>0001, 0002, …, 2013,
|
||
2014, …, 9998, 9999</p></td>
|
||
<td><p>(2)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%H</span></code></p></td>
|
||
<td><p>Hour (24-hour clock) as a
|
||
zero-padded decimal number.</p></td>
|
||
<td><p>00, 01, …, 23</p></td>
|
||
<td><p>(9)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%I</span></code></p></td>
|
||
<td><p>Hour (12-hour clock) as a
|
||
zero-padded decimal number.</p></td>
|
||
<td><p>01, 02, …, 12</p></td>
|
||
<td><p>(9)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%p</span></code></p></td>
|
||
<td><p>Locale’s equivalent of either
|
||
AM or PM.</p></td>
|
||
<td><div class="line-block">
|
||
<div class="line">AM, PM (en_US);</div>
|
||
<div class="line">am, pm (de_DE)</div>
|
||
</div>
|
||
</td>
|
||
<td><p>(1),
|
||
(3)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%M</span></code></p></td>
|
||
<td><p>Minute as a zero-padded
|
||
decimal number.</p></td>
|
||
<td><p>00, 01, …, 59</p></td>
|
||
<td><p>(9)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%S</span></code></p></td>
|
||
<td><p>Second as a zero-padded
|
||
decimal number.</p></td>
|
||
<td><p>00, 01, …, 59</p></td>
|
||
<td><p>(4),
|
||
(9)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%f</span></code></p></td>
|
||
<td><p>Microsecond as a decimal
|
||
number, zero-padded on the
|
||
left.</p></td>
|
||
<td><p>000000, 000001, …,
|
||
999999</p></td>
|
||
<td><p>(5)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%z</span></code></p></td>
|
||
<td><p>UTC offset in the form
|
||
±HHMM[SS[.ffffff]] (empty
|
||
string if the object is
|
||
naive).</p></td>
|
||
<td><p>(empty), +0000,
|
||
-0400, +1030,
|
||
+063415,
|
||
-030712.345216</p></td>
|
||
<td><p>(6)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%Z</span></code></p></td>
|
||
<td><p>Time zone name (empty string
|
||
if the object is naive).</p></td>
|
||
<td><p>(empty), UTC, EST, CST</p></td>
|
||
<td></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%j</span></code></p></td>
|
||
<td><p>Day of the year as a
|
||
zero-padded decimal number.</p></td>
|
||
<td><p>001, 002, …, 366</p></td>
|
||
<td><p>(9)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%U</span></code></p></td>
|
||
<td><p>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.</p></td>
|
||
<td><p>00, 01, …, 53</p></td>
|
||
<td><p>(7),
|
||
(9)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%W</span></code></p></td>
|
||
<td><p>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.</p></td>
|
||
<td><p>00, 01, …, 53</p></td>
|
||
<td><p>(7),
|
||
(9)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%c</span></code></p></td>
|
||
<td><p>Locale’s appropriate date and
|
||
time representation.</p></td>
|
||
<td><div class="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><p>(1)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%x</span></code></p></td>
|
||
<td><p>Locale’s appropriate date
|
||
representation.</p></td>
|
||
<td><div class="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><p>(1)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%X</span></code></p></td>
|
||
<td><p>Locale’s appropriate time
|
||
representation.</p></td>
|
||
<td><div class="line-block">
|
||
<div class="line">21:30:00 (en_US);</div>
|
||
<div class="line">21:30:00 (de_DE)</div>
|
||
</div>
|
||
</td>
|
||
<td><p>(1)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%%</span></code></p></td>
|
||
<td><p>A literal <code class="docutils literal notranslate"><span class="pre">'%'</span></code> character.</p></td>
|
||
<td><p>%</p></td>
|
||
<td></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<p>Several additional directives not required by the C89 standard are included for
|
||
convenience. These parameters all correspond to ISO 8601 date values. These
|
||
may not be available on all platforms when used with the <code class="xref py py-meth docutils literal notranslate"><span class="pre">strftime()</span></code>
|
||
method. The ISO 8601 year and ISO 8601 week directives are not interchangeable
|
||
with the year and week number directives above. Calling <code class="xref py py-meth docutils literal notranslate"><span class="pre">strptime()</span></code> with
|
||
incomplete or ambiguous ISO 8601 directives will raise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p>
|
||
<table class="docutils align-center">
|
||
<colgroup>
|
||
<col style="width: 15%" />
|
||
<col style="width: 43%" />
|
||
<col style="width: 32%" />
|
||
<col style="width: 9%" />
|
||
</colgroup>
|
||
<thead>
|
||
<tr class="row-odd"><th class="head"><p>Directive</p></th>
|
||
<th class="head"><p>Meaning</p></th>
|
||
<th class="head"><p>Example</p></th>
|
||
<th class="head"><p>Notes</p></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%G</span></code></p></td>
|
||
<td><p>ISO 8601 year with century
|
||
representing the year that
|
||
contains the greater part of
|
||
the ISO week (<code class="docutils literal notranslate"><span class="pre">%V</span></code>).</p></td>
|
||
<td><p>0001, 0002, …, 2013,
|
||
2014, …, 9998, 9999</p></td>
|
||
<td><p>(8)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%u</span></code></p></td>
|
||
<td><p>ISO 8601 weekday as a decimal
|
||
number where 1 is Monday.</p></td>
|
||
<td><p>1, 2, …, 7</p></td>
|
||
<td></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%V</span></code></p></td>
|
||
<td><p>ISO 8601 week as a decimal
|
||
number with Monday as
|
||
the first day of the week.
|
||
Week 01 is the week containing
|
||
Jan 4.</p></td>
|
||
<td><p>01, 02, …, 53</p></td>
|
||
<td><p>(8),
|
||
(9)</p></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 3.6: </span><code class="docutils literal notranslate"><span class="pre">%G</span></code>, <code class="docutils literal notranslate"><span class="pre">%u</span></code> and <code class="docutils literal notranslate"><span class="pre">%V</span></code> were added.</p>
|
||
</div>
|
||
<p>Notes:</p>
|
||
<ol class="arabic">
|
||
<li><p>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>The <code class="xref py py-meth docutils literal notranslate"><span class="pre">strptime()</span></code> method can parse years in the full [1, 9999] range, but
|
||
years < 1000 must be zero-filled to 4-digit width.</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.2: </span>In previous versions, <code class="xref py py-meth docutils literal notranslate"><span class="pre">strftime()</span></code> method was restricted to
|
||
years >= 1900.</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.3: </span>In version 3.2, <code class="xref py py-meth docutils literal notranslate"><span class="pre">strftime()</span></code> method was restricted to
|
||
years >= 1000.</p>
|
||
</div>
|
||
</li>
|
||
<li><p>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>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>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. <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).</p></li>
|
||
<li><p>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="simple">
|
||
<dt><code class="docutils literal notranslate"><span class="pre">%z</span></code></dt><dd><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcoffset()</span></code> is transformed into a string of the form
|
||
±HHMM[SS[.ffffff]], where HH is a 2-digit string giving the number of UTC
|
||
offset hours, MM is a 2-digit string giving the number of UTC offset
|
||
minutes, SS is a 2-digit string giving the number of UTC offset
|
||
seconds and ffffff is a 6-digit string giving the number of UTC
|
||
offset microseconds. The ffffff part is omitted when the offset is a
|
||
whole number of seconds and both the ffffff and the SS part is omitted
|
||
when the offset is a whole number of 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>
|
||
</dl>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.7: </span>The UTC offset is not restricted to a whole number of minutes.</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.7: </span>When the <code class="docutils literal notranslate"><span class="pre">%z</span></code> directive is provided to the <code class="xref py py-meth docutils literal notranslate"><span class="pre">strptime()</span></code> method,
|
||
the UTC offsets can have a colon as a separator between hours, minutes
|
||
and seconds.
|
||
For example, <code class="docutils literal notranslate"><span class="pre">'+01:00:00'</span></code> will be parsed as an offset of one hour.
|
||
In addition, providing <code class="docutils literal notranslate"><span class="pre">'Z'</span></code> is identical to <code class="docutils literal notranslate"><span class="pre">'+00:00'</span></code>.</p>
|
||
</div>
|
||
<dl class="simple">
|
||
<dt><code class="docutils literal notranslate"><span class="pre">%Z</span></code></dt><dd><p>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>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.2: </span>When the <code class="docutils literal notranslate"><span class="pre">%z</span></code> directive is provided to the <code class="xref py py-meth docutils literal notranslate"><span class="pre">strptime()</span></code> method, an
|
||
aware <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 will be produced. The <code class="docutils literal notranslate"><span class="pre">tzinfo</span></code> of the
|
||
result will be set to a <a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> instance.</p>
|
||
</div>
|
||
</li>
|
||
<li><p>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 calendar year (<code class="docutils literal notranslate"><span class="pre">%Y</span></code>)
|
||
are specified.</p></li>
|
||
<li><p>Similar to <code class="docutils literal notranslate"><span class="pre">%U</span></code> and <code class="docutils literal notranslate"><span class="pre">%W</span></code>, <code class="docutils literal notranslate"><span class="pre">%V</span></code> is only used in calculations when the
|
||
day of the week and the ISO year (<code class="docutils literal notranslate"><span class="pre">%G</span></code>) are specified in a
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">strptime()</span></code> format string. Also note that <code class="docutils literal notranslate"><span class="pre">%G</span></code> and <code class="docutils literal notranslate"><span class="pre">%Y</span></code> are not
|
||
interchangeable.</p></li>
|
||
<li><p>When used with the <code class="xref py py-meth docutils literal notranslate"><span class="pre">strptime()</span></code> method, the leading zero is optional
|
||
for formats <code class="docutils literal notranslate"><span class="pre">%d</span></code>, <code class="docutils literal notranslate"><span class="pre">%m</span></code>, <code class="docutils literal notranslate"><span class="pre">%H</span></code>, <code class="docutils literal notranslate"><span class="pre">%I</span></code>, <code class="docutils literal notranslate"><span class="pre">%M</span></code>, <code class="docutils literal notranslate"><span class="pre">%S</span></code>, <code class="docutils literal notranslate"><span class="pre">%J</span></code>, <code class="docutils literal notranslate"><span class="pre">%U</span></code>,
|
||
<code class="docutils literal notranslate"><span class="pre">%W</span></code>, and <code class="docutils literal notranslate"><span class="pre">%V</span></code>. Format <code class="docutils literal notranslate"><span class="pre">%y</span></code> does require a leading zero.</p></li>
|
||
</ol>
|
||
<p class="rubric">Footnotes</p>
|
||
<dl class="footnote brackets">
|
||
<dt class="label" id="id3"><span class="brackets"><a class="fn-backref" href="#id1">1</a></span></dt>
|
||
<dd><p>If, that is, we ignore the effects of Relativity</p>
|
||
</dd>
|
||
<dt class="label" id="id4"><span class="brackets"><a class="fn-backref" href="#id2">2</a></span></dt>
|
||
<dd><p>Passing <code class="docutils literal notranslate"><span class="pre">datetime.strptime('Feb</span> <span class="pre">29',</span> <span class="pre">'%b</span> <span class="pre">%d')</span></code> will fail since <code class="docutils literal notranslate"><span class="pre">1900</span></code> is not a leap year.</p>
|
||
</dd>
|
||
</dl>
|
||
</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="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code> — Basic date and time types</a><ul>
|
||
<li><a class="reference internal" href="#available-types">Available Types</a></li>
|
||
<li><a class="reference internal" href="#timedelta-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code> Objects</a></li>
|
||
<li><a class="reference internal" href="#date-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code> Objects</a></li>
|
||
<li><a class="reference internal" href="#datetime-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code> Objects</a></li>
|
||
<li><a class="reference internal" href="#time-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code> Objects</a></li>
|
||
<li><a class="reference internal" href="#tzinfo-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code> Objects</a></li>
|
||
<li><a class="reference internal" href="#timezone-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code> Objects</a></li>
|
||
<li><a class="reference internal" href="#strftime-and-strptime-behavior"><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></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>Previous topic</h4>
|
||
<p class="topless"><a href="datatypes.html"
|
||
title="previous chapter">Data Types</a></p>
|
||
<h4>Next topic</h4>
|
||
<p class="topless"><a href="calendar.html"
|
||
title="next chapter"><code class="xref py py-mod 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="../bugs.html">Report a Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.7/Doc/library/datetime.rst"
|
||
rel="nofollow">Show Source
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</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="calendar — General calendar-related functions"
|
||
>next</a> |</li>
|
||
<li class="right" >
|
||
<a href="datatypes.html" title="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>
|
||
<span class="language_switcher_placeholder">en</span>
|
||
<span class="version_switcher_placeholder">3.7.4</span>
|
||
<a href="../index.html">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" >Data Types</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" style="display: none" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="Quick search" 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>
|
||
<script type="text/javascript">$('.inline-search').show(0);</script>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
<div class="footer">
|
||
© <a href="../copyright.html">Copyright</a> 2001-2019, 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 Jul 13, 2019.
|
||
<a href="../bugs.html">Found a bug</a>?
|
||
<br />
|
||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.0.1.
|
||
</div>
|
||
|
||
</body>
|
||
</html> |