Server : Apache System : Linux server1.cgrithy.com 3.10.0-1160.95.1.el7.x86_64 #1 SMP Mon Jul 24 13:59:37 UTC 2023 x86_64 User : nobody ( 99) PHP Version : 8.1.23 Disable Function : NONE Directory : /usr/share/doc/python-docs-2.7.5/html/library/ |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>8.1. datetime — Basic date and time types — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </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/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="8. Data Types" href="datatypes.html" /> <link rel="next" title="8.2. calendar — General calendar-related functions" href="calendar.html" /> <link rel="prev" title="8. Data Types" href="datatypes.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="calendar.html" title="8.2. calendar — General calendar-related functions" accesskey="N">next</a> |</li> <li class="right" > <a href="datatypes.html" title="8. Data Types" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="datatypes.html" accesskey="U">8. Data Types</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-datetime"> <span id="datetime-basic-date-and-time-types"></span><h1>8.1. <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><tt class="xref py py-mod docutils literal"><span class="pre">datetime</span></tt></a> — Basic date and time types<a class="headerlink" href="#module-datetime" title="Permalink to this headline">¶</a></h1> <p class="versionadded"> <span class="versionmodified">New in version 2.3.</span></p> <p>The <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><tt class="xref py py-mod docutils literal"><span class="pre">datetime</span></tt></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."><tt class="xref py py-mod docutils literal"><span class="pre">time</span></tt></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."><tt class="xref py py-mod docutils literal"><span class="pre">calendar</span></tt></a> modules.</p> <p>There are two kinds of date and time objects: “naive” and “aware”.</p> <p>An aware object has sufficient knowledge of applicable algorithmic and political time adjustments, such as time zone and daylight saving time information, to locate itself relative to other aware objects. An aware object is used to represent a specific moment in time that is not open to interpretation <a class="footnote-reference" href="#id2" id="id1">[1]</a>.</p> <p>A naive object does not contain enough information to unambiguously locate itself relative to other date/time objects. Whether a naive object represents Coordinated Universal Time (UTC), local time, or time in some other timezone is purely up to the program, just like it’s up to the program whether a particular number represents metres, miles, or mass. Naive objects are easy to understand and to work with, at the cost of ignoring some aspects of reality.</p> <p>For applications requiring aware objects, <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> and <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> objects have an optional time zone information attribute, <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a>, that can be set to an instance of a subclass of the abstract <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> class. These <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> objects capture information about the offset from UTC time, the time zone name, and whether Daylight Saving Time is in effect. Note that no concrete <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> classes are supplied by the <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><tt class="xref py py-mod docutils literal"><span class="pre">datetime</span></tt></a> module. Supporting timezones at whatever level of detail is required is up to the application. The rules for time adjustment across the world are more political than rational, and there is no standard suitable for every application.</p> <p>The <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><tt class="xref py py-mod docutils literal"><span class="pre">datetime</span></tt></a> module exports the following constants:</p> <dl class="data"> <dt id="datetime.MINYEAR"> <tt class="descclassname">datetime.</tt><tt class="descname">MINYEAR</tt><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"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> or <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object. <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MINYEAR</span></tt></a> is <tt class="docutils literal"><span class="pre">1</span></tt>.</p> </dd></dl> <dl class="data"> <dt id="datetime.MAXYEAR"> <tt class="descclassname">datetime.</tt><tt class="descname">MAXYEAR</tt><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"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> or <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object. <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MAXYEAR</span></tt></a> is <tt class="docutils literal"><span class="pre">9999</span></tt>.</p> </dd></dl> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt>Module <a class="reference internal" href="calendar.html#module-calendar" title="calendar: Functions for working with calendars, including some emulation of the Unix cal program."><tt class="xref py py-mod docutils literal"><span class="pre">calendar</span></tt></a></dt> <dd>General calendar related functions.</dd> <dt>Module <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><tt class="xref py py-mod docutils literal"><span class="pre">time</span></tt></a></dt> <dd>Time access and conversions.</dd> </dl> </div> <div class="section" id="available-types"> <h2>8.1.1. Available Types<a class="headerlink" href="#available-types" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt> <em class="property">class </em><tt class="descclassname">datetime.</tt><tt class="descname">date</tt></dt> <dd><p>An idealized naive date, assuming the current Gregorian calendar always was, and always will be, in effect. Attributes: <tt class="xref py py-attr docutils literal"><span class="pre">year</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">month</span></tt>, and <tt class="xref py py-attr docutils literal"><span class="pre">day</span></tt>.</p> </dd></dl> <dl class="class"> <dt> <em class="property">class </em><tt class="descclassname">datetime.</tt><tt class="descname">time</tt></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: <tt class="xref py py-attr docutils literal"><span class="pre">hour</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">minute</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">second</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">microsecond</span></tt>, and <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a>.</p> </dd></dl> <dl class="class"> <dt> <em class="property">class </em><tt class="descclassname">datetime.</tt><tt class="descname">datetime</tt></dt> <dd><p>A combination of a date and a time. Attributes: <tt class="xref py py-attr docutils literal"><span class="pre">year</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">month</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">day</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">hour</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">minute</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">second</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">microsecond</span></tt>, and <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a>.</p> </dd></dl> <dl class="class"> <dt> <em class="property">class </em><tt class="descclassname">datetime.</tt><tt class="descname">timedelta</tt></dt> <dd><p>A duration expressing the difference between two <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a>, <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a>, or <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> instances to microsecond resolution.</p> </dd></dl> <dl class="class"> <dt id="datetime.tzinfo"> <em class="property">class </em><tt class="descclassname">datetime.</tt><tt class="descname">tzinfo</tt><a class="headerlink" href="#datetime.tzinfo" title="Permalink to this definition">¶</a></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"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> and <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> classes to provide a customizable notion of time adjustment (for example, to account for time zone and/or daylight saving time).</p> </dd></dl> <p>Objects of these types are immutable.</p> <p>Objects of the <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> type are always naive.</p> <p>An object of type <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> or <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> may be naive or aware. A <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object <em>d</em> is aware if <tt class="docutils literal"><span class="pre">d.tzinfo</span></tt> is not <tt class="docutils literal"><span class="pre">None</span></tt> and <tt class="docutils literal"><span class="pre">d.tzinfo.utcoffset(d)</span></tt> does not return <tt class="docutils literal"><span class="pre">None</span></tt>. If <tt class="docutils literal"><span class="pre">d.tzinfo</span></tt> is <tt class="docutils literal"><span class="pre">None</span></tt>, or if <tt class="docutils literal"><span class="pre">d.tzinfo</span></tt> is not <tt class="docutils literal"><span class="pre">None</span></tt> but <tt class="docutils literal"><span class="pre">d.tzinfo.utcoffset(d)</span></tt> returns <tt class="docutils literal"><span class="pre">None</span></tt>, <em>d</em> is naive. A <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> object <em>t</em> is aware if <tt class="docutils literal"><span class="pre">t.tzinfo</span></tt> is not <tt class="docutils literal"><span class="pre">None</span></tt> and <tt class="docutils literal"><span class="pre">t.tzinfo.utcoffset(None)</span></tt> does not return <tt class="docutils literal"><span class="pre">None</span></tt>. 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"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> objects.</p> <p>Subclass relationships:</p> <div class="highlight-python"><pre>object timedelta tzinfo time date datetime</pre> </div> </div> <div class="section" id="timedelta-objects"> <span id="datetime-timedelta"></span><h2>8.1.2. <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></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><tt class="descclassname">datetime.</tt><tt class="descname">timedelta</tt><big>(</big><span class="optional">[</span><em>days</em><span class="optional">[</span>, <em>seconds</em><span class="optional">[</span>, <em>microseconds</em><span class="optional">[</span>, <em>milliseconds</em><span class="optional">[</span>, <em>minutes</em><span class="optional">[</span>, <em>hours</em><span class="optional">[</span>, <em>weeks</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#datetime.timedelta" title="Permalink to this definition">¶</a></dt> <dd><p>All arguments are optional and default to <tt class="docutils literal"><span class="pre">0</span></tt>. Arguments may be ints, longs, or floats, and may be positive or negative.</p> <p>Only <em>days</em>, <em>seconds</em> and <em>microseconds</em> are stored internally. Arguments are converted to those units:</p> <ul class="simple"> <li>A millisecond is converted to 1000 microseconds.</li> <li>A minute is converted to 60 seconds.</li> <li>An hour is converted to 3600 seconds.</li> <li>A week is converted to 7 days.</li> </ul> <p>and days, seconds and microseconds are then normalized so that the representation is unique, with</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">microseconds</span> <span class="pre"><</span> <span class="pre">1000000</span></tt></li> <li><tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">seconds</span> <span class="pre"><</span> <span class="pre">3600*24</span></tt> (the number of seconds in one day)</li> <li><tt class="docutils literal"><span class="pre">-999999999</span> <span class="pre"><=</span> <span class="pre">days</span> <span class="pre"><=</span> <span class="pre">999999999</span></tt></li> </ul> <p>If any argument is a float and there are fractional microseconds, the fractional microseconds left over from all arguments are combined and their sum is rounded to the nearest microsecond. If no argument is a float, the conversion and normalization processes are exact (no information is lost).</p> <p>If the normalized value of days lies outside the indicated range, <a class="reference internal" href="exceptions.html#exceptions.OverflowError" title="exceptions.OverflowError"><tt class="xref py py-exc docutils literal"><span class="pre">OverflowError</span></tt></a> is raised.</p> <p>Note that normalization of negative values may be surprising at first. For example,</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">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"> <tt class="descclassname">timedelta.</tt><tt class="descname">min</tt><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"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object, <tt class="docutils literal"><span class="pre">timedelta(-999999999)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.timedelta.max"> <tt class="descclassname">timedelta.</tt><tt class="descname">max</tt><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"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object, <tt class="docutils literal"><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></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.timedelta.resolution"> <tt class="descclassname">timedelta.</tt><tt class="descname">resolution</tt><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"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> objects, <tt class="docutils literal"><span class="pre">timedelta(microseconds=1)</span></tt>.</p> </dd></dl> <p>Note that, because of normalization, <tt class="docutils literal"><span class="pre">timedelta.max</span></tt> > <tt class="docutils literal"><span class="pre">-timedelta.min</span></tt>. <tt class="docutils literal"><span class="pre">-timedelta.max</span></tt> is not representable as a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object.</p> <p>Instance attributes (read-only):</p> <table border="1" class="docutils"> <colgroup> <col width="29%" /> <col width="71%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Attribute</th> <th class="head">Value</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">days</span></tt></td> <td>Between -999999999 and 999999999 inclusive</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">seconds</span></tt></td> <td>Between 0 and 86399 inclusive</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">microseconds</span></tt></td> <td>Between 0 and 999999 inclusive</td> </tr> </tbody> </table> <p>Supported operations:</p> <table border="1" class="docutils"> <colgroup> <col width="41%" /> <col width="59%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Operation</th> <th class="head">Result</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">+</span> <span class="pre">t3</span></tt></td> <td>Sum of <em>t2</em> and <em>t3</em>. Afterwards <em>t1</em>-<em>t2</em> == <em>t3</em> and <em>t1</em>-<em>t3</em> == <em>t2</em> are true. (1)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">-</span> <span class="pre">t3</span></tt></td> <td>Difference of <em>t2</em> and <em>t3</em>. Afterwards <em>t1</em> == <em>t2</em> - <em>t3</em> and <em>t2</em> == <em>t1</em> + <em>t3</em> are true. (1)</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><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></tt></td> <td>Delta multiplied by an integer or long. Afterwards <em>t1</em> // i == <em>t2</em> is true, provided <tt class="docutils literal"><span class="pre">i</span> <span class="pre">!=</span> <span class="pre">0</span></tt>.</td> </tr> <tr class="row-odd"><td> </td> <td>In general, <em>t1</em> * i == <em>t1</em> * (i-1) + <em>t1</em> is true. (1)</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">//</span> <span class="pre">i</span></tt></td> <td>The floor is computed and the remainder (if any) is thrown away. (3)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">+t1</span></tt></td> <td>Returns a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object with the same value. (2)</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">-t1</span></tt></td> <td>equivalent to <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a>(-<em>t1.days</em>, -<em>t1.seconds</em>, -<em>t1.microseconds</em>), and to <em>t1</em>* -1. (1)(4)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">abs(t)</span></tt></td> <td>equivalent to +<em>t</em> when <tt class="docutils literal"><span class="pre">t.days</span> <span class="pre">>=</span> <span class="pre">0</span></tt>, and to -<em>t</em> when <tt class="docutils literal"><span class="pre">t.days</span> <span class="pre"><</span> <span class="pre">0</span></tt>. (2)</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">str(t)</span></tt></td> <td>Returns a string in the form <tt class="docutils literal"><span class="pre">[D</span> <span class="pre">day[s],</span> <span class="pre">][H]H:MM:SS[.UUUUUU]</span></tt>, where D is negative for negative <tt class="docutils literal"><span class="pre">t</span></tt>. (5)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">repr(t)</span></tt></td> <td>Returns a string in the form <tt class="docutils literal"><span class="pre">datetime.timedelta(D[,</span> <span class="pre">S[,</span> <span class="pre">U]])</span></tt>, where D is negative for negative <tt class="docutils literal"><span class="pre">t</span></tt>. (5)</td> </tr> </tbody> </table> <p>Notes:</p> <ol class="arabic"> <li><p class="first">This is exact, but may overflow.</p> </li> <li><p class="first">This is exact, and cannot overflow.</p> </li> <li><p class="first">Division by 0 raises <a class="reference internal" href="exceptions.html#exceptions.ZeroDivisionError" title="exceptions.ZeroDivisionError"><tt class="xref py py-exc docutils literal"><span class="pre">ZeroDivisionError</span></tt></a>.</p> </li> <li><p class="first">-<em>timedelta.max</em> is not representable as a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object.</p> </li> <li><p class="first">String representations of <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> objects are normalized similarly to their internal representation. This leads to somewhat unusual results for negative timedeltas. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=-</span><span class="mi">5</span><span class="p">)</span> <span class="go">datetime.timedelta(-1, 68400)</span> <span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">_</span><span class="p">)</span> <span class="go">-1 day, 19:00:00</span> </pre></div> </div> </li> </ol> <p>In addition to the operations listed above <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> objects support certain additions and subtractions with <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> and <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> objects (see below).</p> <p>Comparisons of <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> objects are supported with the <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object is compared to an object of a different type, <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> is raised unless the comparison is <tt class="docutils literal"><span class="pre">==</span></tt> or <tt class="docutils literal"><span class="pre">!=</span></tt>. The latter cases return <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const docutils literal"><span class="pre">False</span></tt></a> or <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a>, respectively.</p> <p><a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> objects are <a class="reference internal" href="../glossary.html#term-hashable"><em class="xref std std-term">hashable</em></a> (usable as dictionary keys), support efficient pickling, and in Boolean contexts, a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object is considered to be true if and only if it isn’t equal to <tt class="docutils literal"><span class="pre">timedelta(0)</span></tt>.</p> <p>Instance methods:</p> <dl class="method"> <dt id="datetime.timedelta.total_seconds"> <tt class="descclassname">timedelta.</tt><tt class="descname">total_seconds</tt><big>(</big><big>)</big><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 <tt class="docutils literal"><span class="pre">(td.microseconds</span> <span class="pre">+</span> <span class="pre">(td.seconds</span> <span class="pre">+</span> <span class="pre">td.days</span> <span class="pre">*</span> <span class="pre">24</span> <span class="pre">*</span> <span class="pre">3600)</span> <span class="pre">*</span> <span class="pre">10**6)</span> <span class="pre">/</span> <span class="pre">10**6</span></tt> computed with true division enabled.</p> <p>Note that for very large time intervals (greater than 270 years on most platforms) this method will lose microsecond accuracy.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.7.</span></p> </dd></dl> <p>Example usage:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">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="c"># adds up to 365 days</span> <span class="gp">>>> </span><span class="n">year</span><span class="o">.</span><span class="n">total_seconds</span><span class="p">()</span> <span class="go">31536000.0</span> <span class="gp">>>> </span><span class="n">year</span> <span class="o">==</span> <span class="n">another_year</span> <span class="go">True</span> <span class="gp">>>> </span><span class="n">ten_years</span> <span class="o">=</span> <span class="mi">10</span> <span class="o">*</span> <span class="n">year</span> <span class="gp">>>> </span><span class="n">ten_years</span><span class="p">,</span> <span class="n">ten_years</span><span class="o">.</span><span class="n">days</span> <span class="o">//</span> <span class="mi">365</span> <span class="go">(datetime.timedelta(3650), 10)</span> <span class="gp">>>> </span><span class="n">nine_years</span> <span class="o">=</span> <span class="n">ten_years</span> <span class="o">-</span> <span class="n">year</span> <span class="gp">>>> </span><span class="n">nine_years</span><span class="p">,</span> <span class="n">nine_years</span><span class="o">.</span><span class="n">days</span> <span class="o">//</span> <span class="mi">365</span> <span class="go">(datetime.timedelta(3285), 9)</span> <span class="gp">>>> </span><span class="n">three_years</span> <span class="o">=</span> <span class="n">nine_years</span> <span class="o">//</span> <span class="mi">3</span><span class="p">;</span> <span class="gp">>>> </span><span class="n">three_years</span><span class="p">,</span> <span class="n">three_years</span><span class="o">.</span><span class="n">days</span> <span class="o">//</span> <span class="mi">365</span> <span class="go">(datetime.timedelta(1095), 3)</span> <span class="gp">>>> </span><span class="nb">abs</span><span class="p">(</span><span class="n">three_years</span> <span class="o">-</span> <span class="n">ten_years</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">three_years</span> <span class="o">+</span> <span class="n">year</span> <span class="go">True</span> </pre></div> </div> </div> <div class="section" id="date-objects"> <span id="datetime-date"></span><h2>8.1.3. <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></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><tt class="descclassname">datetime.</tt><tt class="descname">date</tt><big>(</big><em>year</em>, <em>month</em>, <em>day</em><big>)</big><a class="headerlink" href="#datetime.date" title="Permalink to this definition">¶</a></dt> <dd><p>All arguments are required. Arguments may be ints or longs, in the following ranges:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">MINYEAR</span> <span class="pre"><=</span> <span class="pre">year</span> <span class="pre"><=</span> <span class="pre">MAXYEAR</span></tt></li> <li><tt class="docutils literal"><span class="pre">1</span> <span class="pre"><=</span> <span class="pre">month</span> <span class="pre"><=</span> <span class="pre">12</span></tt></li> <li><tt class="docutils literal"><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></tt></li> </ul> <p>If an argument outside those ranges is given, <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></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><tt class="descclassname">date.</tt><tt class="descname">today</tt><big>(</big><big>)</big><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 <tt class="docutils literal"><span class="pre">date.fromtimestamp(time.time())</span></tt>.</p> </dd></dl> <dl class="classmethod"> <dt id="datetime.date.fromtimestamp"> <em class="property">classmethod </em><tt class="descclassname">date.</tt><tt class="descname">fromtimestamp</tt><big>(</big><em>timestamp</em><big>)</big><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"><tt class="xref py py-func docutils literal"><span class="pre">time.time()</span></tt></a>. This may raise <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a>, if the timestamp is out of the range of values supported by the platform C <tt class="xref c c-func docutils literal"><span class="pre">localtime()</span></tt> function. It’s common for this to be restricted to years from 1970 through 2038. Note that on non-POSIX systems that include leap seconds in their notion of a timestamp, leap seconds are ignored by <a class="reference internal" href="#datetime.date.fromtimestamp" title="datetime.date.fromtimestamp"><tt class="xref py py-meth docutils literal"><span class="pre">fromtimestamp()</span></tt></a>.</p> </dd></dl> <dl class="classmethod"> <dt id="datetime.date.fromordinal"> <em class="property">classmethod </em><tt class="descclassname">date.</tt><tt class="descname">fromordinal</tt><big>(</big><em>ordinal</em><big>)</big><a class="headerlink" href="#datetime.date.fromordinal" title="Permalink to this definition">¶</a></dt> <dd><p>Return the date corresponding to the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1. <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> is raised unless <tt class="docutils literal"><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></tt>. For any date <em>d</em>, <tt class="docutils literal"><span class="pre">date.fromordinal(d.toordinal())</span> <span class="pre">==</span> <span class="pre">d</span></tt>.</p> </dd></dl> <p>Class attributes:</p> <dl class="attribute"> <dt id="datetime.date.min"> <tt class="descclassname">date.</tt><tt class="descname">min</tt><a class="headerlink" href="#datetime.date.min" title="Permalink to this definition">¶</a></dt> <dd><p>The earliest representable date, <tt class="docutils literal"><span class="pre">date(MINYEAR,</span> <span class="pre">1,</span> <span class="pre">1)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.date.max"> <tt class="descclassname">date.</tt><tt class="descname">max</tt><a class="headerlink" href="#datetime.date.max" title="Permalink to this definition">¶</a></dt> <dd><p>The latest representable date, <tt class="docutils literal"><span class="pre">date(MAXYEAR,</span> <span class="pre">12,</span> <span class="pre">31)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.date.resolution"> <tt class="descclassname">date.</tt><tt class="descname">resolution</tt><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, <tt class="docutils literal"><span class="pre">timedelta(days=1)</span></tt>.</p> </dd></dl> <p>Instance attributes (read-only):</p> <dl class="attribute"> <dt id="datetime.date.year"> <tt class="descclassname">date.</tt><tt class="descname">year</tt><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"><tt class="xref py py-const docutils literal"><span class="pre">MINYEAR</span></tt></a> and <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MAXYEAR</span></tt></a> inclusive.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.date.month"> <tt class="descclassname">date.</tt><tt class="descname">month</tt><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"> <tt class="descclassname">date.</tt><tt class="descname">day</tt><a class="headerlink" href="#datetime.date.day" title="Permalink to this definition">¶</a></dt> <dd><p>Between 1 and the number of days in the given month of the given year.</p> </dd></dl> <p>Supported operations:</p> <table border="1" class="docutils"> <colgroup> <col width="40%" /> <col width="60%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Operation</th> <th class="head">Result</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">date2</span> <span class="pre">=</span> <span class="pre">date1</span> <span class="pre">+</span> <span class="pre">timedelta</span></tt></td> <td><em>date2</em> is <tt class="docutils literal"><span class="pre">timedelta.days</span></tt> days removed from <em>date1</em>. (1)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">date2</span> <span class="pre">=</span> <span class="pre">date1</span> <span class="pre">-</span> <span class="pre">timedelta</span></tt></td> <td>Computes <em>date2</em> such that <tt class="docutils literal"><span class="pre">date2</span> <span class="pre">+</span> <span class="pre">timedelta</span> <span class="pre">==</span> <span class="pre">date1</span></tt>. (2)</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">timedelta</span> <span class="pre">=</span> <span class="pre">date1</span> <span class="pre">-</span> <span class="pre">date2</span></tt></td> <td>(3)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">date1</span> <span class="pre"><</span> <span class="pre">date2</span></tt></td> <td><em>date1</em> is considered less than <em>date2</em> when <em>date1</em> precedes <em>date2</em> in time. (4)</td> </tr> </tbody> </table> <p>Notes:</p> <ol class="arabic simple"> <li><em>date2</em> is moved forward in time if <tt class="docutils literal"><span class="pre">timedelta.days</span> <span class="pre">></span> <span class="pre">0</span></tt>, or backward if <tt class="docutils literal"><span class="pre">timedelta.days</span> <span class="pre"><</span> <span class="pre">0</span></tt>. Afterward <tt class="docutils literal"><span class="pre">date2</span> <span class="pre">-</span> <span class="pre">date1</span> <span class="pre">==</span> <span class="pre">timedelta.days</span></tt>. <tt class="docutils literal"><span class="pre">timedelta.seconds</span></tt> and <tt class="docutils literal"><span class="pre">timedelta.microseconds</span></tt> are ignored. <a class="reference internal" href="exceptions.html#exceptions.OverflowError" title="exceptions.OverflowError"><tt class="xref py py-exc docutils literal"><span class="pre">OverflowError</span></tt></a> is raised if <tt class="docutils literal"><span class="pre">date2.year</span></tt> would be smaller than <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MINYEAR</span></tt></a> or larger than <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MAXYEAR</span></tt></a>.</li> <li>This isn’t quite equivalent to date1 + (-timedelta), because -timedelta in isolation can overflow in cases where date1 - timedelta does not. <tt class="docutils literal"><span class="pre">timedelta.seconds</span></tt> and <tt class="docutils literal"><span class="pre">timedelta.microseconds</span></tt> are ignored.</li> <li>This is exact, and cannot overflow. timedelta.seconds and timedelta.microseconds are 0, and date2 + timedelta == date1 after.</li> <li>In other words, <tt class="docutils literal"><span class="pre">date1</span> <span class="pre"><</span> <span class="pre">date2</span></tt> if and only if <tt class="docutils literal"><span class="pre">date1.toordinal()</span> <span class="pre"><</span> <span class="pre">date2.toordinal()</span></tt>. In order to stop comparison from falling back to the default scheme of comparing object addresses, date comparison normally raises <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> if the other comparand isn’t also a <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> object. However, <tt class="docutils literal"><span class="pre">NotImplemented</span></tt> is returned instead if the other comparand has a <tt class="xref py py-meth docutils literal"><span class="pre">timetuple()</span></tt> 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"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> object is compared to an object of a different type, <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> is raised unless the comparison is <tt class="docutils literal"><span class="pre">==</span></tt> or <tt class="docutils literal"><span class="pre">!=</span></tt>. The latter cases return <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const docutils literal"><span class="pre">False</span></tt></a> or <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a>, respectively.</li> </ol> <p>Dates can be used as dictionary keys. In Boolean contexts, all <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> objects are considered to be true.</p> <p>Instance methods:</p> <dl class="method"> <dt id="datetime.date.replace"> <tt class="descclassname">date.</tt><tt class="descname">replace</tt><big>(</big><em>year</em>, <em>month</em>, <em>day</em><big>)</big><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 <tt class="docutils literal"><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></tt>, then <tt class="docutils literal"><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></tt>.</p> </dd></dl> <dl class="method"> <dt id="datetime.date.timetuple"> <tt class="descclassname">date.</tt><tt class="descname">timetuple</tt><big>(</big><big>)</big><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"><tt class="xref py py-class docutils literal"><span class="pre">time.struct_time</span></tt></a> such as returned by <a class="reference internal" href="time.html#time.localtime" title="time.localtime"><tt class="xref py py-func docutils literal"><span class="pre">time.localtime()</span></tt></a>. The hours, minutes and seconds are 0, and the DST flag is -1. <tt class="docutils literal"><span class="pre">d.timetuple()</span></tt> is equivalent to <tt class="docutils literal"><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></tt>, where <tt class="docutils literal"><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></tt> is the day number within the current year starting with <tt class="docutils literal"><span class="pre">1</span></tt> for January 1st.</p> </dd></dl> <dl class="method"> <dt id="datetime.date.toordinal"> <tt class="descclassname">date.</tt><tt class="descname">toordinal</tt><big>(</big><big>)</big><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"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> object <em>d</em>, <tt class="docutils literal"><span class="pre">date.fromordinal(d.toordinal())</span> <span class="pre">==</span> <span class="pre">d</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="datetime.date.weekday"> <tt class="descclassname">date.</tt><tt class="descname">weekday</tt><big>(</big><big>)</big><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, <tt class="docutils literal"><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></tt>, a Wednesday. See also <a class="reference internal" href="#datetime.date.isoweekday" title="datetime.date.isoweekday"><tt class="xref py py-meth docutils literal"><span class="pre">isoweekday()</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.date.isoweekday"> <tt class="descclassname">date.</tt><tt class="descname">isoweekday</tt><big>(</big><big>)</big><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, <tt class="docutils literal"><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></tt>, a Wednesday. See also <a class="reference internal" href="#datetime.date.weekday" title="datetime.date.weekday"><tt class="xref py py-meth docutils literal"><span class="pre">weekday()</span></tt></a>, <a class="reference internal" href="#datetime.date.isocalendar" title="datetime.date.isocalendar"><tt class="xref py py-meth docutils literal"><span class="pre">isocalendar()</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.date.isocalendar"> <tt class="descclassname">date.</tt><tt class="descname">isocalendar</tt><big>(</big><big>)</big><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="http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm">http://www.phys.uu.nl/~vgent/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 <tt class="docutils literal"><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></tt> and <tt class="docutils literal"><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></tt>.</p> </dd></dl> <dl class="method"> <dt id="datetime.date.isoformat"> <tt class="descclassname">date.</tt><tt class="descname">isoformat</tt><big>(</big><big>)</big><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, <tt class="docutils literal"><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></tt>.</p> </dd></dl> <dl class="method"> <dt id="datetime.date.__str__"> <tt class="descclassname">date.</tt><tt class="descname">__str__</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.date.__str__" title="Permalink to this definition">¶</a></dt> <dd><p>For a date <em>d</em>, <tt class="docutils literal"><span class="pre">str(d)</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">d.isoformat()</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="datetime.date.ctime"> <tt class="descclassname">date.</tt><tt class="descname">ctime</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.date.ctime" title="Permalink to this definition">¶</a></dt> <dd><p>Return a string representing the date, for example <tt class="docutils literal"><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></tt>. <tt class="docutils literal"><span class="pre">d.ctime()</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">time.ctime(time.mktime(d.timetuple()))</span></tt> on platforms where the native C <tt class="xref c c-func docutils literal"><span class="pre">ctime()</span></tt> function (which <a class="reference internal" href="time.html#time.ctime" title="time.ctime"><tt class="xref py py-func docutils literal"><span class="pre">time.ctime()</span></tt></a> invokes, but which <a class="reference internal" href="#datetime.date.ctime" title="datetime.date.ctime"><tt class="xref py py-meth docutils literal"><span class="pre">date.ctime()</span></tt></a> does not invoke) conforms to the C standard.</p> </dd></dl> <dl class="method"> <dt id="datetime.date.strftime"> <tt class="descclassname">date.</tt><tt class="descname">strftime</tt><big>(</big><em>format</em><big>)</big><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. See section <a class="reference internal" href="#strftime-strptime-behavior"><em>strftime() and strptime() Behavior</em></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.date.__format__"> <tt class="descclassname">date.</tt><tt class="descname">__format__</tt><big>(</big><em>format</em><big>)</big><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"><tt class="xref py py-meth docutils literal"><span class="pre">date.strftime()</span></tt></a>. This makes it possible to specify format string for a <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> object when using <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><tt class="xref py py-meth docutils literal"><span class="pre">str.format()</span></tt></a>. See section <a class="reference internal" href="#strftime-strptime-behavior"><em>strftime() and strptime() Behavior</em></a>.</p> </dd></dl> <p>Example of counting days to an event:</p> <div class="highlight-python"><div class="highlight"><pre><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="kn">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"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">date</span> <span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">date</span><span class="o">.</span><span class="n">fromordinal</span><span class="p">(</span><span class="mi">730920</span><span class="p">)</span> <span class="c"># 730920th day after 1. 1. 0001</span> <span class="gp">>>> </span><span class="n">d</span> <span class="go">datetime.date(2002, 3, 11)</span> <span class="gp">>>> </span><span class="n">t</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">timetuple</span><span class="p">()</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">t</span><span class="p">:</span> <span class="gp">... </span> <span class="k">print</span> <span class="n">i</span> <span class="go">2002 # year</span> <span class="go">3 # month</span> <span class="go">11 # day</span> <span class="go">0</span> <span class="go">0</span> <span class="go">0</span> <span class="go">0 # weekday (0 = Monday)</span> <span class="go">70 # 70th day in the year</span> <span class="go">-1</span> <span class="gp">>>> </span><span class="n">ic</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">isocalendar</span><span class="p">()</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">ic</span><span class="p">:</span> <span class="gp">... </span> <span class="k">print</span> <span class="n">i</span> <span class="go">2002 # ISO year</span> <span class="go">11 # ISO week number</span> <span class="go">1 # ISO day number ( 1 = Monday )</span> <span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">isoformat</span><span class="p">()</span> <span class="go">'2002-03-11'</span> <span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s">"</span><span class="si">%d</span><span class="s">/%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="s">"%A </span><span class="si">%d</span><span class="s">. %B %Y"</span><span class="p">)</span> <span class="go">'Monday 11. March 2002'</span> <span class="gp">>>> </span><span class="s">'The {1} is {0:</span><span class="si">%d</span><span class="s">}, the {2} is {0:%B}.'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="s">"day"</span><span class="p">,</span> <span class="s">"month"</span><span class="p">)</span> <span class="go">'The day is 11, the month is March.'</span> </pre></div> </div> </div> <div class="section" id="datetime-objects"> <span id="datetime-datetime"></span><h2>8.1.4. <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object is a single object containing all the information from a <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> object and a <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> object. Like a <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> object, <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> assumes the current Gregorian calendar extended in both directions; like a time object, <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></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><tt class="descclassname">datetime.</tt><tt class="descname">datetime</tt><big>(</big><em>year</em>, <em>month</em>, <em>day</em><span class="optional">[</span>, <em>hour</em><span class="optional">[</span>, <em>minute</em><span class="optional">[</span>, <em>second</em><span class="optional">[</span>, <em>microsecond</em><span class="optional">[</span>, <em>tzinfo</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><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 <tt class="docutils literal"><span class="pre">None</span></tt>, or an instance of a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclass. The remaining arguments may be ints or longs, in the following ranges:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">MINYEAR</span> <span class="pre"><=</span> <span class="pre">year</span> <span class="pre"><=</span> <span class="pre">MAXYEAR</span></tt></li> <li><tt class="docutils literal"><span class="pre">1</span> <span class="pre"><=</span> <span class="pre">month</span> <span class="pre"><=</span> <span class="pre">12</span></tt></li> <li><tt class="docutils literal"><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></tt></li> <li><tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">hour</span> <span class="pre"><</span> <span class="pre">24</span></tt></li> <li><tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">minute</span> <span class="pre"><</span> <span class="pre">60</span></tt></li> <li><tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">second</span> <span class="pre"><</span> <span class="pre">60</span></tt></li> <li><tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">microsecond</span> <span class="pre"><</span> <span class="pre">1000000</span></tt></li> </ul> <p>If an argument outside those ranges is given, <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> is raised.</p> </dd></dl> <p>Other constructors, all class methods:</p> <dl class="classmethod"> <dt id="datetime.datetime.today"> <em class="property">classmethod </em><tt class="descclassname">datetime.</tt><tt class="descname">today</tt><big>(</big><big>)</big><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.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> <tt class="docutils literal"><span class="pre">None</span></tt>. This is equivalent to <tt class="docutils literal"><span class="pre">datetime.fromtimestamp(time.time())</span></tt>. See also <a class="reference internal" href="#datetime.datetime.now" title="datetime.datetime.now"><tt class="xref py py-meth docutils literal"><span class="pre">now()</span></tt></a>, <a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><tt class="xref py py-meth docutils literal"><span class="pre">fromtimestamp()</span></tt></a>.</p> </dd></dl> <dl class="classmethod"> <dt id="datetime.datetime.now"> <em class="property">classmethod </em><tt class="descclassname">datetime.</tt><tt class="descname">now</tt><big>(</big><span class="optional">[</span><em>tz</em><span class="optional">]</span><big>)</big><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 <tt class="docutils literal"><span class="pre">None</span></tt> or not specified, this is like <a class="reference internal" href="#datetime.datetime.today" title="datetime.datetime.today"><tt class="xref py py-meth docutils literal"><span class="pre">today()</span></tt></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"><tt class="xref py py-func docutils literal"><span class="pre">time.time()</span></tt></a> timestamp (for example, this may be possible on platforms supplying the C <tt class="xref c c-func docutils literal"><span class="pre">gettimeofday()</span></tt> function).</p> <p>Else <em>tz</em> must be an instance of a class <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></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 <tt class="docutils literal"><span class="pre">tz.fromutc(datetime.utcnow().replace(tzinfo=tz))</span></tt>. See also <a class="reference internal" href="#datetime.datetime.today" title="datetime.datetime.today"><tt class="xref py py-meth docutils literal"><span class="pre">today()</span></tt></a>, <a class="reference internal" href="#datetime.datetime.utcnow" title="datetime.datetime.utcnow"><tt class="xref py py-meth docutils literal"><span class="pre">utcnow()</span></tt></a>.</p> </dd></dl> <dl class="classmethod"> <dt id="datetime.datetime.utcnow"> <em class="property">classmethod </em><tt class="descclassname">datetime.</tt><tt class="descname">utcnow</tt><big>(</big><big>)</big><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.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> <tt class="docutils literal"><span class="pre">None</span></tt>. This is like <a class="reference internal" href="#datetime.datetime.now" title="datetime.datetime.now"><tt class="xref py py-meth docutils literal"><span class="pre">now()</span></tt></a>, but returns the current UTC date and time, as a naive <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object. See also <a class="reference internal" href="#datetime.datetime.now" title="datetime.datetime.now"><tt class="xref py py-meth docutils literal"><span class="pre">now()</span></tt></a>.</p> </dd></dl> <dl class="classmethod"> <dt id="datetime.datetime.fromtimestamp"> <em class="property">classmethod </em><tt class="descclassname">datetime.</tt><tt class="descname">fromtimestamp</tt><big>(</big><em>timestamp</em><span class="optional">[</span>, <em>tz</em><span class="optional">]</span><big>)</big><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"><tt class="xref py py-func docutils literal"><span class="pre">time.time()</span></tt></a>. If optional argument <em>tz</em> is <tt class="docutils literal"><span class="pre">None</span></tt> 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"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object is naive.</p> <p>Else <em>tz</em> must be an instance of a class <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclass, and the timestamp is converted to <em>tz</em>‘s time zone. In this case the result is equivalent to <tt class="docutils literal"><span class="pre">tz.fromutc(datetime.utcfromtimestamp(timestamp).replace(tzinfo=tz))</span></tt>.</p> <p><a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><tt class="xref py py-meth docutils literal"><span class="pre">fromtimestamp()</span></tt></a> may raise <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a>, if the timestamp is out of the range of values supported by the platform C <tt class="xref c c-func docutils literal"><span class="pre">localtime()</span></tt> or <tt class="xref c c-func docutils literal"><span class="pre">gmtime()</span></tt> functions. It’s common for this to be restricted to years in 1970 through 2038. Note that on non-POSIX systems that include leap seconds in their notion of a timestamp, leap seconds are ignored by <a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><tt class="xref py py-meth docutils literal"><span class="pre">fromtimestamp()</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> objects. See also <a class="reference internal" href="#datetime.datetime.utcfromtimestamp" title="datetime.datetime.utcfromtimestamp"><tt class="xref py py-meth docutils literal"><span class="pre">utcfromtimestamp()</span></tt></a>.</p> </dd></dl> <dl class="classmethod"> <dt id="datetime.datetime.utcfromtimestamp"> <em class="property">classmethod </em><tt class="descclassname">datetime.</tt><tt class="descname">utcfromtimestamp</tt><big>(</big><em>timestamp</em><big>)</big><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"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> corresponding to the POSIX timestamp, with <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> <tt class="docutils literal"><span class="pre">None</span></tt>. This may raise <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a>, if the timestamp is out of the range of values supported by the platform C <tt class="xref c c-func docutils literal"><span class="pre">gmtime()</span></tt> function. It’s common for this to be restricted to years in 1970 through 2038. See also <a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><tt class="xref py py-meth docutils literal"><span class="pre">fromtimestamp()</span></tt></a>.</p> </dd></dl> <dl class="classmethod"> <dt id="datetime.datetime.fromordinal"> <em class="property">classmethod </em><tt class="descclassname">datetime.</tt><tt class="descname">fromordinal</tt><big>(</big><em>ordinal</em><big>)</big><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"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> corresponding to the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1. <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> is raised unless <tt class="docutils literal"><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></tt>. The hour, minute, second and microsecond of the result are all 0, and <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> is <tt class="docutils literal"><span class="pre">None</span></tt>.</p> </dd></dl> <dl class="classmethod"> <dt id="datetime.datetime.combine"> <em class="property">classmethod </em><tt class="descclassname">datetime.</tt><tt class="descname">combine</tt><big>(</big><em>date</em>, <em>time</em><big>)</big><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"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object whose date components are equal to the given <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> object’s, and whose time components and <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attributes are equal to the given <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> object’s. For any <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object <em>d</em>, <tt class="docutils literal"><span class="pre">d</span> <span class="pre">==</span> <span class="pre">datetime.combine(d.date(),</span> <span class="pre">d.timetz())</span></tt>. If date is a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object, its time components and <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attributes are ignored.</p> </dd></dl> <dl class="classmethod"> <dt id="datetime.datetime.strptime"> <em class="property">classmethod </em><tt class="descclassname">datetime.</tt><tt class="descname">strptime</tt><big>(</big><em>date_string</em>, <em>format</em><big>)</big><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"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> corresponding to <em>date_string</em>, parsed according to <em>format</em>. This is equivalent to <tt class="docutils literal"><span class="pre">datetime(*(time.strptime(date_string,</span> <span class="pre">format)[0:6]))</span></tt>. <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></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"><tt class="xref py py-func docutils literal"><span class="pre">time.strptime()</span></tt></a> or if it returns a value which isn’t a time tuple. See section <a class="reference internal" href="#strftime-strptime-behavior"><em>strftime() and strptime() Behavior</em></a>.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.5.</span></p> </dd></dl> <p>Class attributes:</p> <dl class="attribute"> <dt id="datetime.datetime.min"> <tt class="descclassname">datetime.</tt><tt class="descname">min</tt><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"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a>, <tt class="docutils literal"><span class="pre">datetime(MINYEAR,</span> <span class="pre">1,</span> <span class="pre">1,</span> <span class="pre">tzinfo=None)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.datetime.max"> <tt class="descclassname">datetime.</tt><tt class="descname">max</tt><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"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a>, <tt class="docutils literal"><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></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.datetime.resolution"> <tt class="descclassname">datetime.</tt><tt class="descname">resolution</tt><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"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> objects, <tt class="docutils literal"><span class="pre">timedelta(microseconds=1)</span></tt>.</p> </dd></dl> <p>Instance attributes (read-only):</p> <dl class="attribute"> <dt id="datetime.datetime.year"> <tt class="descclassname">datetime.</tt><tt class="descname">year</tt><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"><tt class="xref py py-const docutils literal"><span class="pre">MINYEAR</span></tt></a> and <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MAXYEAR</span></tt></a> inclusive.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.datetime.month"> <tt class="descclassname">datetime.</tt><tt class="descname">month</tt><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"> <tt class="descclassname">datetime.</tt><tt class="descname">day</tt><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"> <tt class="descclassname">datetime.</tt><tt class="descname">hour</tt><a class="headerlink" href="#datetime.datetime.hour" title="Permalink to this definition">¶</a></dt> <dd><p>In <tt class="docutils literal"><span class="pre">range(24)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.datetime.minute"> <tt class="descclassname">datetime.</tt><tt class="descname">minute</tt><a class="headerlink" href="#datetime.datetime.minute" title="Permalink to this definition">¶</a></dt> <dd><p>In <tt class="docutils literal"><span class="pre">range(60)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.datetime.second"> <tt class="descclassname">datetime.</tt><tt class="descname">second</tt><a class="headerlink" href="#datetime.datetime.second" title="Permalink to this definition">¶</a></dt> <dd><p>In <tt class="docutils literal"><span class="pre">range(60)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.datetime.microsecond"> <tt class="descclassname">datetime.</tt><tt class="descname">microsecond</tt><a class="headerlink" href="#datetime.datetime.microsecond" title="Permalink to this definition">¶</a></dt> <dd><p>In <tt class="docutils literal"><span class="pre">range(1000000)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.datetime.tzinfo"> <tt class="descclassname">datetime.</tt><tt class="descname">tzinfo</tt><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"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> constructor, or <tt class="docutils literal"><span class="pre">None</span></tt> if none was passed.</p> </dd></dl> <p>Supported operations:</p> <table border="1" class="docutils"> <colgroup> <col width="55%" /> <col width="45%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Operation</th> <th class="head">Result</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">datetime2</span> <span class="pre">=</span> <span class="pre">datetime1</span> <span class="pre">+</span> <span class="pre">timedelta</span></tt></td> <td>(1)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">datetime2</span> <span class="pre">=</span> <span class="pre">datetime1</span> <span class="pre">-</span> <span class="pre">timedelta</span></tt></td> <td>(2)</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">timedelta</span> <span class="pre">=</span> <span class="pre">datetime1</span> <span class="pre">-</span> <span class="pre">datetime2</span></tt></td> <td>(3)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">datetime1</span> <span class="pre"><</span> <span class="pre">datetime2</span></tt></td> <td>Compares <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> to <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a>. (4)</td> </tr> </tbody> </table> <ol class="arabic"> <li><p class="first">datetime2 is a duration of timedelta removed from datetime1, moving forward in time if <tt class="docutils literal"><span class="pre">timedelta.days</span></tt> > 0, or backward if <tt class="docutils literal"><span class="pre">timedelta.days</span></tt> < 0. The result has the same <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attribute as the input datetime, and datetime2 - datetime1 == timedelta after. <a class="reference internal" href="exceptions.html#exceptions.OverflowError" title="exceptions.OverflowError"><tt class="xref py py-exc docutils literal"><span class="pre">OverflowError</span></tt></a> is raised if datetime2.year would be smaller than <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MINYEAR</span></tt></a> or larger than <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MAXYEAR</span></tt></a>. Note that no time zone adjustments are done even if the input is an aware object.</p> </li> <li><p class="first">Computes the datetime2 such that datetime2 + timedelta == datetime1. As for addition, the result has the same <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attribute as the input datetime, and no time zone adjustments are done even if the input is aware. This isn’t quite equivalent to datetime1 + (-timedelta), because -timedelta in isolation can overflow in cases where datetime1 - timedelta does not.</p> </li> <li><p class="first">Subtraction of a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> from a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> is defined only if both operands are naive, or if both are aware. If one is aware and the other is naive, <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> is raised.</p> <p>If both are naive, or both are aware and have the same <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attribute, the <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attributes are ignored, and the result is a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object <em>t</em> such that <tt class="docutils literal"><span class="pre">datetime2</span> <span class="pre">+</span> <span class="pre">t</span> <span class="pre">==</span> <span class="pre">datetime1</span></tt>. No time zone adjustments are done in this case.</p> <p>If both are aware and have different <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attributes, <tt class="docutils literal"><span class="pre">a-b</span></tt> acts as if <em>a</em> and <em>b</em> were first converted to naive UTC datetimes first. The result is <tt class="docutils literal"><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></tt> except that the implementation never overflows.</p> </li> <li><p class="first"><em>datetime1</em> is considered less than <em>datetime2</em> when <em>datetime1</em> precedes <em>datetime2</em> in time.</p> <p>If one comparand is naive and the other is aware, <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> is raised. If both comparands are aware, and have the same <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attribute, the common <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attribute is ignored and the base datetimes are compared. If both comparands are aware and have different <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attributes, the comparands are first adjusted by subtracting their UTC offsets (obtained from <tt class="docutils literal"><span class="pre">self.utcoffset()</span></tt>).</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">In order to stop comparison from falling back to the default scheme of comparing object addresses, datetime comparison normally raises <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> if the other comparand isn’t also a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object. However, <tt class="docutils literal"><span class="pre">NotImplemented</span></tt> is returned instead if the other comparand has a <tt class="xref py py-meth docutils literal"><span class="pre">timetuple()</span></tt> 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"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object is compared to an object of a different type, <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> is raised unless the comparison is <tt class="docutils literal"><span class="pre">==</span></tt> or <tt class="docutils literal"><span class="pre">!=</span></tt>. The latter cases return <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const docutils literal"><span class="pre">False</span></tt></a> or <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a>, respectively.</p> </div> </li> </ol> <p><a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> objects can be used as dictionary keys. In Boolean contexts, all <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> objects are considered to be true.</p> <p>Instance methods:</p> <dl class="method"> <dt id="datetime.datetime.date"> <tt class="descclassname">datetime.</tt><tt class="descname">date</tt><big>(</big><big>)</big><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"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> object with same year, month and day.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.time"> <tt class="descclassname">datetime.</tt><tt class="descname">time</tt><big>(</big><big>)</big><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"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> object with same hour, minute, second and microsecond. <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> is <tt class="docutils literal"><span class="pre">None</span></tt>. See also method <a class="reference internal" href="#datetime.datetime.timetz" title="datetime.datetime.timetz"><tt class="xref py py-meth docutils literal"><span class="pre">timetz()</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.timetz"> <tt class="descclassname">datetime.</tt><tt class="descname">timetz</tt><big>(</big><big>)</big><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"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> object with same hour, minute, second, microsecond, and tzinfo attributes. See also method <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><tt class="xref py py-meth docutils literal"><span class="pre">time()</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.replace"> <tt class="descclassname">datetime.</tt><tt class="descname">replace</tt><big>(</big><span class="optional">[</span><em>year</em><span class="optional">[</span>, <em>month</em><span class="optional">[</span>, <em>day</em><span class="optional">[</span>, <em>hour</em><span class="optional">[</span>, <em>minute</em><span class="optional">[</span>, <em>second</em><span class="optional">[</span>, <em>microsecond</em><span class="optional">[</span>, <em>tzinfo</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><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 <tt class="docutils literal"><span class="pre">tzinfo=None</span></tt> can be specified to create a naive datetime from an aware datetime with no conversion of date and time data.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.astimezone"> <tt class="descclassname">datetime.</tt><tt class="descname">astimezone</tt><big>(</big><em>tz</em><big>)</big><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"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object with new <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attribute <em>tz</em>, adjusting the date and time data so the result is the same UTC time as <em>self</em>, but in <em>tz</em>‘s local time.</p> <p><em>tz</em> must be an instance of a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclass, and its <a class="reference internal" href="#datetime.datetime.utcoffset" title="datetime.datetime.utcoffset"><tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt></a> and <a class="reference internal" href="#datetime.datetime.dst" title="datetime.datetime.dst"><tt class="xref py py-meth docutils literal"><span class="pre">dst()</span></tt></a> methods must not return <tt class="docutils literal"><span class="pre">None</span></tt>. <em>self</em> must be aware (<tt class="docutils literal"><span class="pre">self.tzinfo</span></tt> must not be <tt class="docutils literal"><span class="pre">None</span></tt>, and <tt class="docutils literal"><span class="pre">self.utcoffset()</span></tt> must not return <tt class="docutils literal"><span class="pre">None</span></tt>).</p> <p>If <tt class="docutils literal"><span class="pre">self.tzinfo</span></tt> is <em>tz</em>, <tt class="docutils literal"><span class="pre">self.astimezone(tz)</span></tt> is equal to <em>self</em>: no adjustment of date or time data is performed. Else the result is local time in time zone <em>tz</em>, representing the same UTC time as <em>self</em>: after <tt class="docutils literal"><span class="pre">astz</span> <span class="pre">=</span> <span class="pre">dt.astimezone(tz)</span></tt>, <tt class="docutils literal"><span class="pre">astz</span> <span class="pre">-</span> <span class="pre">astz.utcoffset()</span></tt> will usually have the same date and time data as <tt class="docutils literal"><span class="pre">dt</span> <span class="pre">-</span> <span class="pre">dt.utcoffset()</span></tt>. The discussion of class <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> explains the cases at Daylight Saving Time transition boundaries where this cannot be achieved (an issue only if <em>tz</em> models both standard and daylight time).</p> <p>If you merely want to attach a time zone object <em>tz</em> to a datetime <em>dt</em> without adjustment of date and time data, use <tt class="docutils literal"><span class="pre">dt.replace(tzinfo=tz)</span></tt>. 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 <tt class="docutils literal"><span class="pre">dt.replace(tzinfo=None)</span></tt>.</p> <p>Note that the default <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><tt class="xref py py-meth docutils literal"><span class="pre">tzinfo.fromutc()</span></tt></a> method can be overridden in a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclass to affect the result returned by <a class="reference internal" href="#datetime.datetime.astimezone" title="datetime.datetime.astimezone"><tt class="xref py py-meth docutils literal"><span class="pre">astimezone()</span></tt></a>. Ignoring error cases, <a class="reference internal" href="#datetime.datetime.astimezone" title="datetime.datetime.astimezone"><tt class="xref py py-meth docutils literal"><span class="pre">astimezone()</span></tt></a> acts like:</p> <div class="highlight-python"><div class="highlight"><pre><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="c"># 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="c"># Convert from UTC to tz's local time.</span> <span class="k">return</span> <span class="n">tz</span><span class="o">.</span><span class="n">fromutc</span><span class="p">(</span><span class="n">utc</span><span class="p">)</span> </pre></div> </div> </dd></dl> <dl class="method"> <dt id="datetime.datetime.utcoffset"> <tt class="descclassname">datetime.</tt><tt class="descname">utcoffset</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.datetime.utcoffset" title="Permalink to this definition">¶</a></dt> <dd><p>If <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> is <tt class="docutils literal"><span class="pre">None</span></tt>, returns <tt class="docutils literal"><span class="pre">None</span></tt>, else returns <tt class="docutils literal"><span class="pre">self.tzinfo.utcoffset(self)</span></tt>, and raises an exception if the latter doesn’t return <tt class="docutils literal"><span class="pre">None</span></tt>, or a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object representing a whole number of minutes with magnitude less than one day.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.dst"> <tt class="descclassname">datetime.</tt><tt class="descname">dst</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.datetime.dst" title="Permalink to this definition">¶</a></dt> <dd><p>If <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> is <tt class="docutils literal"><span class="pre">None</span></tt>, returns <tt class="docutils literal"><span class="pre">None</span></tt>, else returns <tt class="docutils literal"><span class="pre">self.tzinfo.dst(self)</span></tt>, and raises an exception if the latter doesn’t return <tt class="docutils literal"><span class="pre">None</span></tt>, or a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object representing a whole number of minutes with magnitude less than one day.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.tzname"> <tt class="descclassname">datetime.</tt><tt class="descname">tzname</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.datetime.tzname" title="Permalink to this definition">¶</a></dt> <dd><p>If <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> is <tt class="docutils literal"><span class="pre">None</span></tt>, returns <tt class="docutils literal"><span class="pre">None</span></tt>, else returns <tt class="docutils literal"><span class="pre">self.tzinfo.tzname(self)</span></tt>, raises an exception if the latter doesn’t return <tt class="docutils literal"><span class="pre">None</span></tt> or a string object,</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.timetuple"> <tt class="descclassname">datetime.</tt><tt class="descname">timetuple</tt><big>(</big><big>)</big><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"><tt class="xref py py-class docutils literal"><span class="pre">time.struct_time</span></tt></a> such as returned by <a class="reference internal" href="time.html#time.localtime" title="time.localtime"><tt class="xref py py-func docutils literal"><span class="pre">time.localtime()</span></tt></a>. <tt class="docutils literal"><span class="pre">d.timetuple()</span></tt> is equivalent to <tt class="docutils literal"><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></tt>, where <tt class="docutils literal"><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></tt> is the day number within the current year starting with <tt class="docutils literal"><span class="pre">1</span></tt> for January 1st. The <tt class="xref py py-attr docutils literal"><span class="pre">tm_isdst</span></tt> flag of the result is set according to the <a class="reference internal" href="#datetime.datetime.dst" title="datetime.datetime.dst"><tt class="xref py py-meth docutils literal"><span class="pre">dst()</span></tt></a> method: <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> is <tt class="docutils literal"><span class="pre">None</span></tt> or <a class="reference internal" href="#datetime.datetime.dst" title="datetime.datetime.dst"><tt class="xref py py-meth docutils literal"><span class="pre">dst()</span></tt></a> returns <tt class="docutils literal"><span class="pre">None</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">tm_isdst</span></tt> is set to <tt class="docutils literal"><span class="pre">-1</span></tt>; else if <a class="reference internal" href="#datetime.datetime.dst" title="datetime.datetime.dst"><tt class="xref py py-meth docutils literal"><span class="pre">dst()</span></tt></a> returns a non-zero value, <tt class="xref py py-attr docutils literal"><span class="pre">tm_isdst</span></tt> is set to <tt class="docutils literal"><span class="pre">1</span></tt>; else <tt class="xref py py-attr docutils literal"><span class="pre">tm_isdst</span></tt> is set to <tt class="docutils literal"><span class="pre">0</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.utctimetuple"> <tt class="descclassname">datetime.</tt><tt class="descname">utctimetuple</tt><big>(</big><big>)</big><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"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> instance <em>d</em> is naive, this is the same as <tt class="docutils literal"><span class="pre">d.timetuple()</span></tt> except that <tt class="xref py py-attr docutils literal"><span class="pre">tm_isdst</span></tt> is forced to 0 regardless of what <tt class="docutils literal"><span class="pre">d.dst()</span></tt> 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 <tt class="docutils literal"><span class="pre">d.utcoffset()</span></tt>, and a <a class="reference internal" href="time.html#time.struct_time" title="time.struct_time"><tt class="xref py py-class docutils literal"><span class="pre">time.struct_time</span></tt></a> for the normalized time is returned. <tt class="xref py py-attr docutils literal"><span class="pre">tm_isdst</span></tt> is forced to 0. Note that the result’s <tt class="xref py py-attr docutils literal"><span class="pre">tm_year</span></tt> member may be <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MINYEAR</span></tt></a>-1 or <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MAXYEAR</span></tt></a>+1, if <em>d</em>.year was <tt class="docutils literal"><span class="pre">MINYEAR</span></tt> or <tt class="docutils literal"><span class="pre">MAXYEAR</span></tt> and UTC adjustment spills over a year boundary.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.toordinal"> <tt class="descclassname">datetime.</tt><tt class="descname">toordinal</tt><big>(</big><big>)</big><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 <tt class="docutils literal"><span class="pre">self.date().toordinal()</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.weekday"> <tt class="descclassname">datetime.</tt><tt class="descname">weekday</tt><big>(</big><big>)</big><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 <tt class="docutils literal"><span class="pre">self.date().weekday()</span></tt>. See also <a class="reference internal" href="#datetime.datetime.isoweekday" title="datetime.datetime.isoweekday"><tt class="xref py py-meth docutils literal"><span class="pre">isoweekday()</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.isoweekday"> <tt class="descclassname">datetime.</tt><tt class="descname">isoweekday</tt><big>(</big><big>)</big><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 <tt class="docutils literal"><span class="pre">self.date().isoweekday()</span></tt>. See also <a class="reference internal" href="#datetime.datetime.weekday" title="datetime.datetime.weekday"><tt class="xref py py-meth docutils literal"><span class="pre">weekday()</span></tt></a>, <a class="reference internal" href="#datetime.datetime.isocalendar" title="datetime.datetime.isocalendar"><tt class="xref py py-meth docutils literal"><span class="pre">isocalendar()</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.isocalendar"> <tt class="descclassname">datetime.</tt><tt class="descname">isocalendar</tt><big>(</big><big>)</big><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 <tt class="docutils literal"><span class="pre">self.date().isocalendar()</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.isoformat"> <tt class="descclassname">datetime.</tt><tt class="descname">isoformat</tt><big>(</big><span class="optional">[</span><em>sep</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#datetime.datetime.isoformat" title="Permalink to this definition">¶</a></dt> <dd><p>Return a string representing the date and time in ISO 8601 format, YYYY-MM-DDTHH:MM:SS.mmmmmm or, if <a class="reference internal" href="#datetime.datetime.microsecond" title="datetime.datetime.microsecond"><tt class="xref py py-attr docutils literal"><span class="pre">microsecond</span></tt></a> is 0, YYYY-MM-DDTHH:MM:SS</p> <p>If <a class="reference internal" href="#datetime.datetime.utcoffset" title="datetime.datetime.utcoffset"><tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt></a> does not return <tt class="docutils literal"><span class="pre">None</span></tt>, a 6-character string is appended, giving the UTC offset in (signed) hours and minutes: YYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MM or, if <a class="reference internal" href="#datetime.datetime.microsecond" title="datetime.datetime.microsecond"><tt class="xref py py-attr docutils literal"><span class="pre">microsecond</span></tt></a> is 0 YYYY-MM-DDTHH:MM:SS+HH:MM</p> <p>The optional argument <em>sep</em> (default <tt class="docutils literal"><span class="pre">'T'</span></tt>) is a one-character separator, placed between the date and time portions of the result. For example,</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">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="s">' '</span><span class="p">)</span> <span class="go">'2002-12-25 00:00:00-06:39'</span> </pre></div> </div> </dd></dl> <dl class="method"> <dt id="datetime.datetime.__str__"> <tt class="descclassname">datetime.</tt><tt class="descname">__str__</tt><big>(</big><big>)</big><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"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> instance <em>d</em>, <tt class="docutils literal"><span class="pre">str(d)</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">d.isoformat('</span> <span class="pre">')</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.ctime"> <tt class="descclassname">datetime.</tt><tt class="descname">ctime</tt><big>(</big><big>)</big><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 <tt class="docutils literal"><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></tt>. <tt class="docutils literal"><span class="pre">d.ctime()</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">time.ctime(time.mktime(d.timetuple()))</span></tt> on platforms where the native C <tt class="xref c c-func docutils literal"><span class="pre">ctime()</span></tt> function (which <a class="reference internal" href="time.html#time.ctime" title="time.ctime"><tt class="xref py py-func docutils literal"><span class="pre">time.ctime()</span></tt></a> invokes, but which <a class="reference internal" href="#datetime.datetime.ctime" title="datetime.datetime.ctime"><tt class="xref py py-meth docutils literal"><span class="pre">datetime.ctime()</span></tt></a> does not invoke) conforms to the C standard.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.strftime"> <tt class="descclassname">datetime.</tt><tt class="descname">strftime</tt><big>(</big><em>format</em><big>)</big><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. See section <a class="reference internal" href="#strftime-strptime-behavior"><em>strftime() and strptime() Behavior</em></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.__format__"> <tt class="descclassname">datetime.</tt><tt class="descname">__format__</tt><big>(</big><em>format</em><big>)</big><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"><tt class="xref py py-meth docutils literal"><span class="pre">datetime.strftime()</span></tt></a>. This makes it possible to specify format string for a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object when using <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><tt class="xref py py-meth docutils literal"><span class="pre">str.format()</span></tt></a>. See section <a class="reference internal" href="#strftime-strptime-behavior"><em>strftime() and strptime() Behavior</em></a>.</p> </dd></dl> <p>Examples of working with datetime objects:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span><span class="p">,</span> <span class="n">date</span><span class="p">,</span> <span class="n">time</span> <span class="gp">>>> </span><span class="c"># 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="c"># 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="c"># 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="s">"21/11/06 16:30"</span><span class="p">,</span> <span class="s">"</span><span class="si">%d</span><span class="s">/%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="c"># Using datetime.timetuple() to get tuple of all attributes</span> <span class="gp">>>> </span><span class="n">tt</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">timetuple</span><span class="p">()</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">it</span> <span class="ow">in</span> <span class="n">tt</span><span class="p">:</span> <span class="gp">... </span> <span class="k">print</span> <span class="n">it</span> <span class="gp">...</span> <span class="go">2006 # year</span> <span class="go">11 # month</span> <span class="go">21 # day</span> <span class="go">16 # hour</span> <span class="go">30 # minute</span> <span class="go">0 # second</span> <span class="go">1 # weekday (0 = Monday)</span> <span class="go">325 # number of days since 1st January</span> <span class="go">-1 # dst - method tzinfo.dst() returned None</span> <span class="gp">>>> </span><span class="c"># Date in ISO format</span> <span class="gp">>>> </span><span class="n">ic</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">isocalendar</span><span class="p">()</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">it</span> <span class="ow">in</span> <span class="n">ic</span><span class="p">:</span> <span class="gp">... </span> <span class="k">print</span> <span class="n">it</span> <span class="gp">...</span> <span class="go">2006 # ISO year</span> <span class="go">47 # ISO week</span> <span class="go">2 # ISO weekday</span> <span class="gp">>>> </span><span class="c"># 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="s">"%A, </span><span class="si">%d</span><span class="s">. %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="s">'The {1} is {0:</span><span class="si">%d</span><span class="s">}, the {2} is {0:%B}, the {3} is {0:%I:%M%p}.'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">dt</span><span class="p">,</span> <span class="s">"day"</span><span class="p">,</span> <span class="s">"month"</span><span class="p">,</span> <span class="s">"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-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">timedelta</span><span class="p">,</span> <span class="n">datetime</span><span class="p">,</span> <span class="n">tzinfo</span> <span class="gp">>>> </span><span class="k">class</span> <span class="nc">GMT1</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">dst</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="gp">... </span> <span class="c"># DST starts last Sunday in March</span> <span class="gp">... </span> <span class="n">d</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c"># ends last Sunday in October</span> <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">dston</span> <span class="o">=</span> <span class="n">d</span> <span class="o">-</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">weekday</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="gp">... </span> <span class="n">d</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">dstoff</span> <span class="o">=</span> <span class="n">d</span> <span class="o">-</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">weekday</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="gp">... </span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">dston</span> <span class="o"><=</span> <span class="n">dt</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span> <span class="o"><</span> <span class="bp">self</span><span class="o">.</span><span class="n">dstoff</span><span class="p">:</span> <span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="gp">... </span> <span class="k">else</span><span class="p">:</span> <span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">dt</span><span class="p">):</span> <span class="gp">... </span> <span class="k">return</span> <span class="s">"GMT +1"</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="k">class</span> <span class="nc">GMT2</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">dst</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="gp">... </span> <span class="n">d</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">dston</span> <span class="o">=</span> <span class="n">d</span> <span class="o">-</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">weekday</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="gp">... </span> <span class="n">d</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">dstoff</span> <span class="o">=</span> <span class="n">d</span> <span class="o">-</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">weekday</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="gp">... </span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">dston</span> <span class="o"><=</span> <span class="n">dt</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span> <span class="o"><</span> <span class="bp">self</span><span class="o">.</span><span class="n">dstoff</span><span class="p">:</span> <span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="gp">... </span> <span class="k">else</span><span class="p">:</span> <span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">dt</span><span class="p">):</span> <span class="gp">... </span> <span class="k">return</span> <span class="s">"GMT +2"</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">gmt1</span> <span class="o">=</span> <span class="n">GMT1</span><span class="p">()</span> <span class="gp">>>> </span><span class="c"># Daylight Saving Time</span> <span class="gp">>>> </span><span class="n">dt1</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2006</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">21</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">gmt1</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">dt1</span><span class="o">.</span><span class="n">dst</span><span class="p">()</span> <span class="go">datetime.timedelta(0)</span> <span class="gp">>>> </span><span class="n">dt1</span><span class="o">.</span><span class="n">utcoffset</span><span class="p">()</span> <span class="go">datetime.timedelta(0, 3600)</span> <span class="gp">>>> </span><span class="n">dt2</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2006</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">gmt1</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">dt2</span><span class="o">.</span><span class="n">dst</span><span class="p">()</span> <span class="go">datetime.timedelta(0, 3600)</span> <span class="gp">>>> </span><span class="n">dt2</span><span class="o">.</span><span class="n">utcoffset</span><span class="p">()</span> <span class="go">datetime.timedelta(0, 7200)</span> <span class="gp">>>> </span><span class="c"># Convert datetime to another time zone</span> <span class="gp">>>> </span><span class="n">dt3</span> <span class="o">=</span> <span class="n">dt2</span><span class="o">.</span><span class="n">astimezone</span><span class="p">(</span><span class="n">GMT2</span><span class="p">())</span> <span class="gp">>>> </span><span class="n">dt3</span> <span class="go">datetime.datetime(2006, 6, 14, 14, 0, tzinfo=<GMT2 object at 0x...>)</span> <span class="gp">>>> </span><span class="n">dt2</span> <span class="go">datetime.datetime(2006, 6, 14, 13, 0, tzinfo=<GMT1 object at 0x...>)</span> <span class="gp">>>> </span><span class="n">dt2</span><span class="o">.</span><span class="n">utctimetuple</span><span class="p">()</span> <span class="o">==</span> <span class="n">dt3</span><span class="o">.</span><span class="n">utctimetuple</span><span class="p">()</span> <span class="go">True</span> </pre></div> </div> </div> <div class="section" id="time-objects"> <span id="datetime-time"></span><h2>8.1.5. <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> object.</p> <dl class="class"> <dt id="datetime.time"> <em class="property">class </em><tt class="descclassname">datetime.</tt><tt class="descname">time</tt><big>(</big><span class="optional">[</span><em>hour</em><span class="optional">[</span>, <em>minute</em><span class="optional">[</span>, <em>second</em><span class="optional">[</span>, <em>microsecond</em><span class="optional">[</span>, <em>tzinfo</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#datetime.time" title="Permalink to this definition">¶</a></dt> <dd><p>All arguments are optional. <em>tzinfo</em> may be <tt class="docutils literal"><span class="pre">None</span></tt>, or an instance of a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclass. The remaining arguments may be ints or longs, in the following ranges:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">hour</span> <span class="pre"><</span> <span class="pre">24</span></tt></li> <li><tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">minute</span> <span class="pre"><</span> <span class="pre">60</span></tt></li> <li><tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">second</span> <span class="pre"><</span> <span class="pre">60</span></tt></li> <li><tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">microsecond</span> <span class="pre"><</span> <span class="pre">1000000</span></tt>.</li> </ul> <p>If an argument outside those ranges is given, <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> is raised. All default to <tt class="docutils literal"><span class="pre">0</span></tt> except <em>tzinfo</em>, which defaults to <a class="reference internal" href="constants.html#None" title="None"><tt class="xref py py-const docutils literal"><span class="pre">None</span></tt></a>.</p> </dd></dl> <p>Class attributes:</p> <dl class="attribute"> <dt id="datetime.time.min"> <tt class="descclassname">time.</tt><tt class="descname">min</tt><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"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a>, <tt class="docutils literal"><span class="pre">time(0,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">0)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.time.max"> <tt class="descclassname">time.</tt><tt class="descname">max</tt><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"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a>, <tt class="docutils literal"><span class="pre">time(23,</span> <span class="pre">59,</span> <span class="pre">59,</span> <span class="pre">999999)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.time.resolution"> <tt class="descclassname">time.</tt><tt class="descname">resolution</tt><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"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> objects, <tt class="docutils literal"><span class="pre">timedelta(microseconds=1)</span></tt>, although note that arithmetic on <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> objects is not supported.</p> </dd></dl> <p>Instance attributes (read-only):</p> <dl class="attribute"> <dt id="datetime.time.hour"> <tt class="descclassname">time.</tt><tt class="descname">hour</tt><a class="headerlink" href="#datetime.time.hour" title="Permalink to this definition">¶</a></dt> <dd><p>In <tt class="docutils literal"><span class="pre">range(24)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.time.minute"> <tt class="descclassname">time.</tt><tt class="descname">minute</tt><a class="headerlink" href="#datetime.time.minute" title="Permalink to this definition">¶</a></dt> <dd><p>In <tt class="docutils literal"><span class="pre">range(60)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.time.second"> <tt class="descclassname">time.</tt><tt class="descname">second</tt><a class="headerlink" href="#datetime.time.second" title="Permalink to this definition">¶</a></dt> <dd><p>In <tt class="docutils literal"><span class="pre">range(60)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.time.microsecond"> <tt class="descclassname">time.</tt><tt class="descname">microsecond</tt><a class="headerlink" href="#datetime.time.microsecond" title="Permalink to this definition">¶</a></dt> <dd><p>In <tt class="docutils literal"><span class="pre">range(1000000)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.time.tzinfo"> <tt class="descclassname">time.</tt><tt class="descname">tzinfo</tt><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"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> constructor, or <tt class="docutils literal"><span class="pre">None</span></tt> if none was passed.</p> </dd></dl> <p>Supported operations:</p> <ul class="simple"> <li>comparison of <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> to <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a>, where <em>a</em> is considered less than <em>b</em> when <em>a</em> precedes <em>b</em> in time. If one comparand is naive and the other is aware, <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> is raised. If both comparands are aware, and have the same <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attribute, the common <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attribute is ignored and the base times are compared. If both comparands are aware and have different <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attributes, the comparands are first adjusted by subtracting their UTC offsets (obtained from <tt class="docutils literal"><span class="pre">self.utcoffset()</span></tt>). 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"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> object is compared to an object of a different type, <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> is raised unless the comparison is <tt class="docutils literal"><span class="pre">==</span></tt> or <tt class="docutils literal"><span class="pre">!=</span></tt>. The latter cases return <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const docutils literal"><span class="pre">False</span></tt></a> or <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a>, respectively.</li> <li>hash, use as dict key</li> <li>efficient pickling</li> <li>in Boolean contexts, a <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> object is considered to be true if and only if, after converting it to minutes and subtracting <tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt> (or <tt class="docutils literal"><span class="pre">0</span></tt> if that’s <tt class="docutils literal"><span class="pre">None</span></tt>), the result is non-zero.</li> </ul> <p>Instance methods:</p> <dl class="method"> <dt id="datetime.time.replace"> <tt class="descclassname">time.</tt><tt class="descname">replace</tt><big>(</big><span class="optional">[</span><em>hour</em><span class="optional">[</span>, <em>minute</em><span class="optional">[</span>, <em>second</em><span class="optional">[</span>, <em>microsecond</em><span class="optional">[</span>, <em>tzinfo</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><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"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> with the same value, except for those attributes given new values by whichever keyword arguments are specified. Note that <tt class="docutils literal"><span class="pre">tzinfo=None</span></tt> can be specified to create a naive <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> from an aware <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a>, without conversion of the time data.</p> </dd></dl> <dl class="method"> <dt id="datetime.time.isoformat"> <tt class="descclassname">time.</tt><tt class="descname">isoformat</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.time.isoformat" title="Permalink to this definition">¶</a></dt> <dd><p>Return a string representing the time in ISO 8601 format, HH:MM:SS.mmmmmm or, if self.microsecond is 0, HH:MM:SS If <a class="reference internal" href="#datetime.time.utcoffset" title="datetime.time.utcoffset"><tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt></a> does not return <tt class="docutils literal"><span class="pre">None</span></tt>, a 6-character string is appended, giving the UTC offset in (signed) hours and minutes: HH:MM:SS.mmmmmm+HH:MM or, if self.microsecond is 0, HH:MM:SS+HH:MM</p> </dd></dl> <dl class="method"> <dt id="datetime.time.__str__"> <tt class="descclassname">time.</tt><tt class="descname">__str__</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.time.__str__" title="Permalink to this definition">¶</a></dt> <dd><p>For a time <em>t</em>, <tt class="docutils literal"><span class="pre">str(t)</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">t.isoformat()</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="datetime.time.strftime"> <tt class="descclassname">time.</tt><tt class="descname">strftime</tt><big>(</big><em>format</em><big>)</big><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. See section <a class="reference internal" href="#strftime-strptime-behavior"><em>strftime() and strptime() Behavior</em></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.time.__format__"> <tt class="descclassname">time.</tt><tt class="descname">__format__</tt><big>(</big><em>format</em><big>)</big><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"><tt class="xref py py-meth docutils literal"><span class="pre">time.strftime()</span></tt></a>. This makes it possible to specify format string for a <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> object when using <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><tt class="xref py py-meth docutils literal"><span class="pre">str.format()</span></tt></a>. See section <a class="reference internal" href="#strftime-strptime-behavior"><em>strftime() and strptime() Behavior</em></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.time.utcoffset"> <tt class="descclassname">time.</tt><tt class="descname">utcoffset</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.time.utcoffset" title="Permalink to this definition">¶</a></dt> <dd><p>If <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> is <tt class="docutils literal"><span class="pre">None</span></tt>, returns <tt class="docutils literal"><span class="pre">None</span></tt>, else returns <tt class="docutils literal"><span class="pre">self.tzinfo.utcoffset(None)</span></tt>, and raises an exception if the latter doesn’t return <tt class="docutils literal"><span class="pre">None</span></tt> or a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object representing a whole number of minutes with magnitude less than one day.</p> </dd></dl> <dl class="method"> <dt id="datetime.time.dst"> <tt class="descclassname">time.</tt><tt class="descname">dst</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.time.dst" title="Permalink to this definition">¶</a></dt> <dd><p>If <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> is <tt class="docutils literal"><span class="pre">None</span></tt>, returns <tt class="docutils literal"><span class="pre">None</span></tt>, else returns <tt class="docutils literal"><span class="pre">self.tzinfo.dst(None)</span></tt>, and raises an exception if the latter doesn’t return <tt class="docutils literal"><span class="pre">None</span></tt>, or a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object representing a whole number of minutes with magnitude less than one day.</p> </dd></dl> <dl class="method"> <dt id="datetime.time.tzname"> <tt class="descclassname">time.</tt><tt class="descname">tzname</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.time.tzname" title="Permalink to this definition">¶</a></dt> <dd><p>If <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> is <tt class="docutils literal"><span class="pre">None</span></tt>, returns <tt class="docutils literal"><span class="pre">None</span></tt>, else returns <tt class="docutils literal"><span class="pre">self.tzinfo.tzname(None)</span></tt>, or raises an exception if the latter doesn’t return <tt class="docutils literal"><span class="pre">None</span></tt> or a string object.</p> </dd></dl> <p>Example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">time</span><span class="p">,</span> <span class="n">tzinfo</span> <span class="gp">>>> </span><span class="k">class</span> <span class="nc">GMT1</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="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="s">"Europe/Prague"</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">t</span> <span class="o">=</span> <span class="n">time</span><span class="p">(</span><span class="mi">12</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">GMT1</span><span class="p">())</span> <span class="gp">>>> </span><span class="n">t</span> <span class="go">datetime.time(12, 10, 30, tzinfo=<GMT1 object at 0x...>)</span> <span class="gp">>>> </span><span class="n">gmt</span> <span class="o">=</span> <span class="n">GMT1</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">t</span><span class="o">.</span><span class="n">isoformat</span><span class="p">()</span> <span class="go">'12:10:30+01:00'</span> <span class="gp">>>> </span><span class="n">t</span><span class="o">.</span><span class="n">dst</span><span class="p">()</span> <span class="go">datetime.timedelta(0)</span> <span class="gp">>>> </span><span class="n">t</span><span class="o">.</span><span class="n">tzname</span><span class="p">()</span> <span class="go">'Europe/Prague'</span> <span class="gp">>>> </span><span class="n">t</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s">"%H:%M:%S %Z"</span><span class="p">)</span> <span class="go">'12:10:30 Europe/Prague'</span> <span class="gp">>>> </span><span class="s">'The {} is {:%H:%M}.'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s">"time"</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span> <span class="go">'The time is 12:10.'</span> </pre></div> </div> </div> <div class="section" id="tzinfo-objects"> <span id="datetime-tzinfo"></span><h2>8.1.6. <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> Objects<a class="headerlink" href="#tzinfo-objects" title="Permalink to this headline">¶</a></h2> <p><a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> 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"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> methods needed by the <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> methods you use. The <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><tt class="xref py py-mod docutils literal"><span class="pre">datetime</span></tt></a> module does not supply any concrete subclasses of <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a>.</p> <p>An instance of (a concrete subclass of) <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> can be passed to the constructors for <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> and <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclass must have an <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">__init__()</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></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."><tt class="xref py py-mod docutils literal"><span class="pre">datetime</span></tt></a> objects. If in doubt, simply implement all of them.</p> <dl class="method"> <dt id="datetime.tzinfo.utcoffset"> <tt class="descclassname">tzinfo.</tt><tt class="descname">utcoffset</tt><big>(</big><em>self</em>, <em>dt</em><big>)</big><a class="headerlink" href="#datetime.tzinfo.utcoffset" title="Permalink to this definition">¶</a></dt> <dd><p>Return offset of local time from UTC, in minutes east of UTC. If local time is west of UTC, this should be negative. Note that this is intended to be the total offset from UTC; for example, if a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> object represents both time zone and DST adjustments, <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt></a> should return their sum. If the UTC offset isn’t known, return <tt class="docutils literal"><span class="pre">None</span></tt>. Else the value returned must be a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object specifying a whole number of minutes in the range -1439 to 1439 inclusive (1440 = 24*60; the magnitude of the offset must be less than one day). Most implementations of <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt></a> will probably look like one of these two:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">return</span> <span class="n">CONSTANT</span> <span class="c"># 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="c"># daylight-aware class</span> </pre></div> </div> <p>If <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt></a> does not return <tt class="docutils literal"><span class="pre">None</span></tt>, <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><tt class="xref py py-meth docutils literal"><span class="pre">dst()</span></tt></a> should not return <tt class="docutils literal"><span class="pre">None</span></tt> either.</p> <p>The default implementation of <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt></a> raises <a class="reference internal" href="exceptions.html#exceptions.NotImplementedError" title="exceptions.NotImplementedError"><tt class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.tzinfo.dst"> <tt class="descclassname">tzinfo.</tt><tt class="descname">dst</tt><big>(</big><em>self</em>, <em>dt</em><big>)</big><a class="headerlink" href="#datetime.tzinfo.dst" title="Permalink to this definition">¶</a></dt> <dd><p>Return the daylight saving time (DST) adjustment, in minutes east of UTC, or <tt class="docutils literal"><span class="pre">None</span></tt> if DST information isn’t known. Return <tt class="docutils literal"><span class="pre">timedelta(0)</span></tt> 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"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object (see <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt></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"><tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt></a>, so there’s no need to consult <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><tt class="xref py py-meth docutils literal"><span class="pre">dst()</span></tt></a> unless you’re interested in obtaining DST info separately. For example, <a class="reference internal" href="#datetime.datetime.timetuple" title="datetime.datetime.timetuple"><tt class="xref py py-meth docutils literal"><span class="pre">datetime.timetuple()</span></tt></a> calls its <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attribute’s <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><tt class="xref py py-meth docutils literal"><span class="pre">dst()</span></tt></a> method to determine how the <tt class="xref py py-attr docutils literal"><span class="pre">tm_isdst</span></tt> flag should be set, and <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><tt class="xref py py-meth docutils literal"><span class="pre">tzinfo.fromutc()</span></tt></a> calls <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><tt class="xref py py-meth docutils literal"><span class="pre">dst()</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclass that models both standard and daylight times must be consistent in this sense:</p> <p><tt class="docutils literal"><span class="pre">tz.utcoffset(dt)</span> <span class="pre">-</span> <span class="pre">tz.dst(dt)</span></tt></p> <p>must return the same result for every <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> <em>dt</em> with <tt class="docutils literal"><span class="pre">dt.tzinfo</span> <span class="pre">==</span> <span class="pre">tz</span></tt> For sane <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></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"><tt class="xref py py-meth docutils literal"><span class="pre">datetime.astimezone()</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></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"><tt class="xref py py-meth docutils literal"><span class="pre">tzinfo.fromutc()</span></tt></a> to work correctly with <tt class="xref py py-meth docutils literal"><span class="pre">astimezone()</span></tt> regardless.</p> <p>Most implementations of <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><tt class="xref py py-meth docutils literal"><span class="pre">dst()</span></tt></a> will probably look like one of these two:</p> <div class="highlight-python"><div class="highlight"><pre><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="c"># 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-python"><div class="highlight"><pre><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="c"># Code to set dston and dstoff to the time zone's DST</span> <span class="c"># transition times based on the input dt.year, and expressed</span> <span class="c"># 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="bp">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"><tt class="xref py py-meth docutils literal"><span class="pre">dst()</span></tt></a> raises <a class="reference internal" href="exceptions.html#exceptions.NotImplementedError" title="exceptions.NotImplementedError"><tt class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.tzinfo.tzname"> <tt class="descclassname">tzinfo.</tt><tt class="descname">tzname</tt><big>(</big><em>self</em>, <em>dt</em><big>)</big><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"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></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."><tt class="xref py py-mod docutils literal"><span class="pre">datetime</span></tt></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 <tt class="docutils literal"><span class="pre">None</span></tt> 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"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></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"><tt class="xref py py-meth docutils literal"><span class="pre">tzname()</span></tt></a> raises <a class="reference internal" href="exceptions.html#exceptions.NotImplementedError" title="exceptions.NotImplementedError"><tt class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></tt></a>.</p> </dd></dl> <p>These methods are called by a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> or <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> object, in response to their methods of the same names. A <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object passes itself as the argument, and a <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> object passes <tt class="docutils literal"><span class="pre">None</span></tt> as the argument. A <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclass’s methods should therefore be prepared to accept a <em>dt</em> argument of <tt class="docutils literal"><span class="pre">None</span></tt>, or of class <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a>.</p> <p>When <tt class="docutils literal"><span class="pre">None</span></tt> is passed, it’s up to the class designer to decide the best response. For example, returning <tt class="docutils literal"><span class="pre">None</span></tt> 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"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> protocols. It may be more useful for <tt class="docutils literal"><span class="pre">utcoffset(None)</span></tt> 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"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object is passed in response to a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> method, <tt class="docutils literal"><span class="pre">dt.tzinfo</span></tt> is the same object as <em>self</em>. <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> methods can rely on this, unless user code calls <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> methods directly. The intent is that the <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> method that a subclass may wish to override:</p> <dl class="method"> <dt id="datetime.tzinfo.fromutc"> <tt class="descclassname">tzinfo.</tt><tt class="descname">fromutc</tt><big>(</big><em>self</em>, <em>dt</em><big>)</big><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"><tt class="xref py py-class docutils literal"><span class="pre">datetime.astimezone()</span></tt></a> implementation. When called from that, <tt class="docutils literal"><span class="pre">dt.tzinfo</span></tt> 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"><tt class="xref py py-meth docutils literal"><span class="pre">fromutc()</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclasses should be able to inherit the default <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><tt class="xref py py-meth docutils literal"><span class="pre">fromutc()</span></tt></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"><tt class="xref py py-meth docutils literal"><span class="pre">fromutc()</span></tt></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 <tt class="xref py py-meth docutils literal"><span class="pre">astimezone()</span></tt> and <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><tt class="xref py py-meth docutils literal"><span class="pre">fromutc()</span></tt></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"><tt class="xref py py-meth docutils literal"><span class="pre">fromutc()</span></tt></a> implementation acts like:</p> <div class="highlight-python"><div class="highlight"><pre><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="c"># 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="c"># 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="c"># 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="c"># 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="c"># raise ValueError if dtdst is None</span> <span class="k">if</span> <span class="n">dtdst</span><span class="p">:</span> <span class="k">return</span> <span class="n">dt</span> <span class="o">+</span> <span class="n">dtdst</span> <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="n">dt</span> </pre></div> </div> </dd></dl> <p>Example <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> classes:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">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="c"># A UTC class.</span> <span class="k">class</span> <span class="nc">UTC</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span> <span class="sd">"""UTC"""</span> <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="n">ZERO</span> <span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="s">"UTC"</span> <span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="n">ZERO</span> <span class="n">utc</span> <span class="o">=</span> <span class="n">UTC</span><span class="p">()</span> <span class="c"># A class building tzinfo objects for fixed-offset time zones.</span> <span class="c"># Note that FixedOffset(0, "UTC") is a different way to build a</span> <span class="c"># UTC tzinfo object.</span> <span class="k">class</span> <span class="nc">FixedOffset</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span> <span class="sd">"""Fixed offset in minutes east from UTC."""</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">offset</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">__offset</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">minutes</span> <span class="o">=</span> <span class="n">offset</span><span class="p">)</span> <span class="bp">self</span><span class="o">.</span><span class="n">__name</span> <span class="o">=</span> <span class="n">name</span> <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__offset</span> <span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__name</span> <span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="n">ZERO</span> <span class="c"># A class capturing the platform's idea of local time.</span> <span class="kn">import</span> <span class="nn">time</span> <span class="kn">as</span> <span class="nn">_time</span> <span class="n">STDOFFSET</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">seconds</span> <span class="o">=</span> <span class="o">-</span><span class="n">_time</span><span class="o">.</span><span class="n">timezone</span><span class="p">)</span> <span class="k">if</span> <span class="n">_time</span><span class="o">.</span><span class="n">daylight</span><span class="p">:</span> <span class="n">DSTOFFSET</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">seconds</span> <span class="o">=</span> <span class="o">-</span><span class="n">_time</span><span class="o">.</span><span class="n">altzone</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="n">DSTOFFSET</span> <span class="o">=</span> <span class="n">STDOFFSET</span> <span class="n">DSTDIFF</span> <span class="o">=</span> <span class="n">DSTOFFSET</span> <span class="o">-</span> <span class="n">STDOFFSET</span> <span class="k">class</span> <span class="nc">LocalTimezone</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span> <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_isdst</span><span class="p">(</span><span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="n">DSTOFFSET</span> <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="n">STDOFFSET</span> <span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_isdst</span><span class="p">(</span><span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="n">DSTDIFF</span> <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="n">ZERO</span> <span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="n">_time</span><span class="o">.</span><span class="n">tzname</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_isdst</span><span class="p">(</span><span class="n">dt</span><span class="p">)]</span> <span class="k">def</span> <span class="nf">_isdst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="n">tt</span> <span class="o">=</span> <span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="n">dt</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="n">dt</span><span class="o">.</span><span class="n">day</span><span class="p">,</span> <span class="n">dt</span><span class="o">.</span><span class="n">hour</span><span class="p">,</span> <span class="n">dt</span><span class="o">.</span><span class="n">minute</span><span class="p">,</span> <span class="n">dt</span><span class="o">.</span><span class="n">second</span><span class="p">,</span> <span class="n">dt</span><span class="o">.</span><span class="n">weekday</span><span class="p">(),</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="n">stamp</span> <span class="o">=</span> <span class="n">_time</span><span class="o">.</span><span class="n">mktime</span><span class="p">(</span><span class="n">tt</span><span class="p">)</span> <span class="n">tt</span> <span class="o">=</span> <span class="n">_time</span><span class="o">.</span><span class="n">localtime</span><span class="p">(</span><span class="n">stamp</span><span class="p">)</span> <span class="k">return</span> <span class="n">tt</span><span class="o">.</span><span class="n">tm_isdst</span> <span class="o">></span> <span class="mi">0</span> <span class="n">Local</span> <span class="o">=</span> <span class="n">LocalTimezone</span><span class="p">()</span> <span class="c"># 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="c"># US DST Rules</span> <span class="c">#</span> <span class="c"># This is a simplified (i.e., wrong for a few cases) set of rules for US</span> <span class="c"># DST start and end times. For a complete and up-to-date set of DST rules</span> <span class="c"># and timezone definitions, visit the Olson Database (or try pytz):</span> <span class="c"># http://www.twinsun.com/tz/tz-link.htm</span> <span class="c"># http://sourceforge.net/projects/pytz/ (might not be up-to-date)</span> <span class="c">#</span> <span class="c"># In the US, since 2007, DST starts at 2am (standard time) on the second</span> <span class="c"># 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="c"># and ends at 2am (DST time; 1am standard time) on the first Sunday of Nov.</span> <span class="n">DSTEND_2007</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c"># From 1987 to 2006, DST used to start at 2am (standard time) on the first</span> <span class="c"># Sunday in April and to end at 2am (DST time; 1am standard time) on the last</span> <span class="c"># Sunday of October, which is the first Sunday on or after Oct 25.</span> <span class="n">DSTSTART_1987_2006</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="n">DSTEND_1987_2006</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">25</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c"># From 1967 to 1986, DST used to start at 2am (standard time) on the last</span> <span class="c"># Sunday in April (the one on or after April 24) and to end at 2am (DST time;</span> <span class="c"># 1am standard time) on the last Sunday of October, which is the first Sunday</span> <span class="c"># on or after Oct 25.</span> <span class="n">DSTSTART_1967_1986</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">24</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="n">DSTEND_1967_1986</span> <span class="o">=</span> <span class="n">DSTEND_1987_2006</span> <span class="k">class</span> <span class="nc">USTimeZone</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">hours</span><span class="p">,</span> <span class="n">reprname</span><span class="p">,</span> <span class="n">stdname</span><span class="p">,</span> <span class="n">dstname</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">stdoffset</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="n">hours</span><span class="p">)</span> <span class="bp">self</span><span class="o">.</span><span class="n">reprname</span> <span class="o">=</span> <span class="n">reprname</span> <span class="bp">self</span><span class="o">.</span><span class="n">stdname</span> <span class="o">=</span> <span class="n">stdname</span> <span class="bp">self</span><span class="o">.</span><span class="n">dstname</span> <span class="o">=</span> <span class="n">dstname</span> <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">reprname</span> <span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">dst</span><span class="p">(</span><span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">dstname</span> <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">stdname</span> <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">stdoffset</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">dst</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span> <span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">if</span> <span class="n">dt</span> <span class="ow">is</span> <span class="bp">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="bp">None</span><span class="p">:</span> <span class="c"># An exception may be sensible here, in one or both cases.</span> <span class="c"># It depends on how you want to treat them. The default</span> <span class="c"># fromutc() implementation (called by the default astimezone()</span> <span class="c"># 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="c"># Find start and end times for US DST. For years before 1967, return</span> <span class="c"># ZERO for no DST.</span> <span class="k">if</span> <span class="mi">2006</span> <span class="o"><</span> <span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">:</span> <span class="n">dststart</span><span class="p">,</span> <span class="n">dstend</span> <span class="o">=</span> <span class="n">DSTSTART_2007</span><span class="p">,</span> <span class="n">DSTEND_2007</span> <span class="k">elif</span> <span class="mi">1986</span> <span class="o"><</span> <span class="n">dt</span><span class="o">.</span><span class="n">year</span> <span class="o"><</span> <span class="mi">2007</span><span class="p">:</span> <span class="n">dststart</span><span class="p">,</span> <span class="n">dstend</span> <span class="o">=</span> <span class="n">DSTSTART_1987_2006</span><span class="p">,</span> <span class="n">DSTEND_1987_2006</span> <span class="k">elif</span> <span class="mi">1966</span> <span class="o"><</span> <span class="n">dt</span><span class="o">.</span><span class="n">year</span> <span class="o"><</span> <span class="mi">1987</span><span class="p">:</span> <span class="n">dststart</span><span class="p">,</span> <span class="n">dstend</span> <span class="o">=</span> <span class="n">DSTSTART_1967_1986</span><span class="p">,</span> <span class="n">DSTEND_1967_1986</span> <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="n">ZERO</span> <span class="n">start</span> <span class="o">=</span> <span class="n">first_sunday_on_or_after</span><span class="p">(</span><span class="n">dststart</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">year</span><span class="o">=</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">))</span> <span class="n">end</span> <span class="o">=</span> <span class="n">first_sunday_on_or_after</span><span class="p">(</span><span class="n">dstend</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">year</span><span class="o">=</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">))</span> <span class="c"># Can't compare naive to aware objects, so strip the timezone from</span> <span class="c"># dt first.</span> <span class="k">if</span> <span class="n">start</span> <span class="o"><=</span> <span class="n">dt</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span> <span class="o"><</span> <span class="n">end</span><span class="p">:</span> <span class="k">return</span> <span class="n">HOUR</span> <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="n">ZERO</span> <span class="n">Eastern</span> <span class="o">=</span> <span class="n">USTimeZone</span><span class="p">(</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span> <span class="s">"Eastern"</span><span class="p">,</span> <span class="s">"EST"</span><span class="p">,</span> <span class="s">"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="s">"Central"</span><span class="p">,</span> <span class="s">"CST"</span><span class="p">,</span> <span class="s">"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="s">"Mountain"</span><span class="p">,</span> <span class="s">"MST"</span><span class="p">,</span> <span class="s">"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="s">"Pacific"</span><span class="p">,</span> <span class="s">"PST"</span><span class="p">,</span> <span class="s">"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"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></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-python"><pre> UTC 3:MM 4:MM 5:MM 6:MM 7:MM 8:MM EST 22:MM 23:MM 0:MM 1:MM 2:MM 3:MM EDT 23:MM 0:MM 1:MM 2:MM 3:MM 4:MM start 22:MM 23:MM 0:MM 1:MM 3:MM 4:MM end 23:MM 0:MM 1:MM 1:MM 2:MM 3:MM</pre> </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 <tt class="docutils literal"><span class="pre">astimezone(Eastern)</span></tt> won’t deliver a result with <tt class="docutils literal"><span class="pre">hour</span> <span class="pre">==</span> <span class="pre">2</span></tt> on the day DST begins. In order for <tt class="xref py py-meth docutils literal"><span class="pre">astimezone()</span></tt> to make this guarantee, the <tt class="xref py py-meth docutils literal"><span class="pre">rzinfo.dst()</span></tt> method must consider times in the “missing hour” (2:MM for Eastern) to be in daylight time.</p> <p>When DST ends (the “end” line), there’s a potentially worse problem: there’s an hour that can’t be spelled unambiguously in local wall time: the last hour of daylight time. In Eastern, that’s times of the form 5:MM UTC on the day daylight time ends. The local wall clock leaps from 1:59 (daylight time) back to 1:00 (standard time) again. Local times of the form 1:MM are ambiguous. <tt class="xref py py-meth docutils literal"><span class="pre">astimezone()</span></tt> mimics the local clock’s behavior by mapping two adjacent UTC hours into the same local hour then. In the Eastern example, UTC times of the form 5:MM and 6:MM both map to 1:MM when converted to Eastern. In order for <tt class="xref py py-meth docutils literal"><span class="pre">astimezone()</span></tt> to make this guarantee, the <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><tt class="xref py py-meth docutils literal"><span class="pre">tzinfo.dst()</span></tt></a> method must consider times in the “repeated hour” to be in standard time. This is easily arranged, as in the example, by expressing DST switch times in the time zone’s standard local time.</p> <p>Applications that can’t bear such ambiguities should avoid using hybrid <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclasses; there are no ambiguities when using UTC, or any other fixed-offset <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclass (such as a class representing only EST (fixed offset -5 hours), or only EDT (fixed offset -4 hours)).</p> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt><a class="reference external" href="http://pypi.python.org/pypi/pytz/">pytz</a></dt> <dd><p class="first">The standard library has no <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> instances, but there exists a third-party library which brings the <em>IANA timezone database</em> (also known as the Olson database) to Python: <em>pytz</em>.</p> <p class="last"><em>pytz</em> contains up-to-date information and its usage is recommended.</p> </dd> <dt><a class="reference external" href="http://www.iana.org/time-zones">IANA timezone database</a></dt> <dd>The Time Zone Database (often called tz or zoneinfo) contains code and data that represent the history of local time for many representative locations around the globe. It is updated periodically to reflect changes made by political bodies to time zone boundaries, UTC offsets, and daylight-saving rules.</dd> </dl> </div> </div> <div class="section" id="strftime-and-strptime-behavior"> <span id="strftime-strptime-behavior"></span><h2>8.1.7. <tt class="xref py py-meth docutils literal"><span class="pre">strftime()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">strptime()</span></tt> 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"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a>, <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a>, and <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> objects all support a <tt class="docutils literal"><span class="pre">strftime(format)</span></tt> method, to create a string representing the time under the control of an explicit format string. Broadly speaking, <tt class="docutils literal"><span class="pre">d.strftime(fmt)</span></tt> acts like the <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><tt class="xref py py-mod docutils literal"><span class="pre">time</span></tt></a> module’s <tt class="docutils literal"><span class="pre">time.strftime(fmt,</span> <span class="pre">d.timetuple())</span></tt> although not all objects support a <tt class="xref py py-meth docutils literal"><span class="pre">timetuple()</span></tt> method.</p> <p>Conversely, the <a class="reference internal" href="#datetime.datetime.strptime" title="datetime.datetime.strptime"><tt class="xref py py-meth docutils literal"><span class="pre">datetime.strptime()</span></tt></a> class method creates a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object from a string representing a date and time and a corresponding format string. <tt class="docutils literal"><span class="pre">datetime.strptime(date_string,</span> <span class="pre">format)</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">datetime(*(time.strptime(date_string,</span> <span class="pre">format)[0:6]))</span></tt>.</p> <p>For <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></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, <tt class="docutils literal"><span class="pre">1900</span></tt> is substituted for the year, and <tt class="docutils literal"><span class="pre">1</span></tt> for the month and day.</p> <p>For <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> objects have no such values. If they’re used anyway, <tt class="docutils literal"><span class="pre">0</span></tt> is substituted for them.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6: </span><a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> and <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> objects support a <tt class="docutils literal"><span class="pre">%f</span></tt> format code which expands to the number of microseconds in the object, zero-padded on the left to six places.</p> <p>For a naive object, the <tt class="docutils literal"><span class="pre">%z</span></tt> and <tt class="docutils literal"><span class="pre">%Z</span></tt> format codes are replaced by empty strings.</p> <p>For an aware object:</p> <dl class="docutils"> <dt><tt class="docutils literal"><span class="pre">%z</span></tt></dt> <dd><tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt> is transformed into a 5-character string of the form +HHMM or -HHMM, where HH is a 2-digit string giving the number of UTC offset hours, and MM is a 2-digit string giving the number of UTC offset minutes. For example, if <tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt> returns <tt class="docutils literal"><span class="pre">timedelta(hours=-3,</span> <span class="pre">minutes=-30)</span></tt>, <tt class="docutils literal"><span class="pre">%z</span></tt> is replaced with the string <tt class="docutils literal"><span class="pre">'-0330'</span></tt>.</dd> <dt><tt class="docutils literal"><span class="pre">%Z</span></tt></dt> <dd>If <tt class="xref py py-meth docutils literal"><span class="pre">tzname()</span></tt> returns <tt class="docutils literal"><span class="pre">None</span></tt>, <tt class="docutils literal"><span class="pre">%Z</span></tt> is replaced by an empty string. Otherwise <tt class="docutils literal"><span class="pre">%Z</span></tt> is replaced by the returned value, which must be a string.</dd> </dl> <p>The full set of format codes supported varies across platforms, because Python calls the platform C library’s <tt class="xref py py-func docutils literal"><span class="pre">strftime()</span></tt> function, and platform variations are common.</p> <p>The following is a list of all the format codes that the C standard (1989 version) requires, and these work on all platforms with a standard C implementation. Note that the 1999 version of the C standard added additional format codes.</p> <p>The exact range of years for which <tt class="xref py py-meth docutils literal"><span class="pre">strftime()</span></tt> works also varies across platforms. Regardless of platform, years before 1900 cannot be used.</p> <table border="1" class="docutils"> <colgroup> <col width="22%" /> <col width="64%" /> <col width="14%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Directive</th> <th class="head">Meaning</th> <th class="head">Notes</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%a</span></tt></td> <td>Locale’s abbreviated weekday name.</td> <td> </td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%A</span></tt></td> <td>Locale’s full weekday name.</td> <td> </td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%b</span></tt></td> <td>Locale’s abbreviated month name.</td> <td> </td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%B</span></tt></td> <td>Locale’s full month name.</td> <td> </td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%c</span></tt></td> <td>Locale’s appropriate date and time representation.</td> <td> </td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%d</span></tt></td> <td>Day of the month as a decimal number [01,31].</td> <td> </td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%f</span></tt></td> <td>Microsecond as a decimal number [0,999999], zero-padded on the left</td> <td>(1)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%H</span></tt></td> <td>Hour (24-hour clock) as a decimal number [00,23].</td> <td> </td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%I</span></tt></td> <td>Hour (12-hour clock) as a decimal number [01,12].</td> <td> </td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%j</span></tt></td> <td>Day of the year as a decimal number [001,366].</td> <td> </td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%m</span></tt></td> <td>Month as a decimal number [01,12].</td> <td> </td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%M</span></tt></td> <td>Minute as a decimal number [00,59].</td> <td> </td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%p</span></tt></td> <td>Locale’s equivalent of either AM or PM.</td> <td>(2)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%S</span></tt></td> <td>Second as a decimal number [00,61].</td> <td>(3)</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%U</span></tt></td> <td>Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.</td> <td>(4)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%w</span></tt></td> <td>Weekday as a decimal number [0(Sunday),6].</td> <td> </td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%W</span></tt></td> <td>Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.</td> <td>(4)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%x</span></tt></td> <td>Locale’s appropriate date representation.</td> <td> </td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%X</span></tt></td> <td>Locale’s appropriate time representation.</td> <td> </td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%y</span></tt></td> <td>Year without century as a decimal number [00,99].</td> <td> </td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%Y</span></tt></td> <td>Year with century as a decimal number.</td> <td> </td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%z</span></tt></td> <td>UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).</td> <td>(5)</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%Z</span></tt></td> <td>Time zone name (empty string if the object is naive).</td> <td> </td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%%</span></tt></td> <td>A literal <tt class="docutils literal"><span class="pre">'%'</span></tt> character.</td> <td> </td> </tr> </tbody> </table> <p>Notes:</p> <ol class="arabic simple"> <li>When used with the <tt class="xref py py-meth docutils literal"><span class="pre">strptime()</span></tt> method, the <tt class="docutils literal"><span class="pre">%f</span></tt> directive accepts from one to six digits and zero pads on the right. <tt class="docutils literal"><span class="pre">%f</span></tt> is an extension to the set of format characters in the C standard (but implemented separately in datetime objects, and therefore always available).</li> <li>When used with the <tt class="xref py py-meth docutils literal"><span class="pre">strptime()</span></tt> method, the <tt class="docutils literal"><span class="pre">%p</span></tt> directive only affects the output hour field if the <tt class="docutils literal"><span class="pre">%I</span></tt> directive is used to parse the hour.</li> <li>The range really is <tt class="docutils literal"><span class="pre">0</span></tt> to <tt class="docutils literal"><span class="pre">61</span></tt>; according to the Posix standard this accounts for leap seconds and the (very rare) double leap seconds. The <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><tt class="xref py py-mod docutils literal"><span class="pre">time</span></tt></a> module may produce and does accept leap seconds since it is based on the Posix standard, but the <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><tt class="xref py py-mod docutils literal"><span class="pre">datetime</span></tt></a> module does not accept leap seconds in <tt class="xref py py-meth docutils literal"><span class="pre">strptime()</span></tt> input nor will it produce them in <tt class="xref py py-func docutils literal"><span class="pre">strftime()</span></tt> output.</li> <li>When used with the <tt class="xref py py-meth docutils literal"><span class="pre">strptime()</span></tt> method, <tt class="docutils literal"><span class="pre">%U</span></tt> and <tt class="docutils literal"><span class="pre">%W</span></tt> are only used in calculations when the day of the week and the year are specified.</li> <li>For example, if <tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt> returns <tt class="docutils literal"><span class="pre">timedelta(hours=-3,</span> <span class="pre">minutes=-30)</span></tt>, <tt class="docutils literal"><span class="pre">%z</span></tt> is replaced with the string <tt class="docutils literal"><span class="pre">'-0330'</span></tt>.</li> </ol> <p class="rubric">Footnotes</p> <table class="docutils footnote" frame="void" id="id2" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>If, that is, we ignore the effects of Relativity</td></tr> </tbody> </table> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">8.1. <tt class="docutils literal"><span class="pre">datetime</span></tt> — Basic date and time types</a><ul> <li><a class="reference internal" href="#available-types">8.1.1. Available Types</a></li> <li><a class="reference internal" href="#timedelta-objects">8.1.2. <tt class="docutils literal"><span class="pre">timedelta</span></tt> Objects</a></li> <li><a class="reference internal" href="#date-objects">8.1.3. <tt class="docutils literal"><span class="pre">date</span></tt> Objects</a></li> <li><a class="reference internal" href="#datetime-objects">8.1.4. <tt class="docutils literal"><span class="pre">datetime</span></tt> Objects</a></li> <li><a class="reference internal" href="#time-objects">8.1.5. <tt class="docutils literal"><span class="pre">time</span></tt> Objects</a></li> <li><a class="reference internal" href="#tzinfo-objects">8.1.6. <tt class="docutils literal"><span class="pre">tzinfo</span></tt> Objects</a></li> <li><a class="reference internal" href="#strftime-and-strptime-behavior">8.1.7. <tt class="docutils literal"><span class="pre">strftime()</span></tt> and <tt class="docutils literal"><span class="pre">strptime()</span></tt> Behavior</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="datatypes.html" title="previous chapter">8. Data Types</a></p> <h4>Next topic</h4> <p class="topless"><a href="calendar.html" title="next chapter">8.2. <tt class="docutils literal"><span class="pre">calendar</span></tt> — General calendar-related functions</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/library/datetime.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="calendar.html" title="8.2. calendar — General calendar-related functions" >next</a> |</li> <li class="right" > <a href="datatypes.html" title="8. Data Types" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="datatypes.html" >8. Data Types</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2020, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Oct 13, 2020. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>