* **Format code `'%z'` accepts `'±HH[:]MM'` and returns `'±HHMM'` (or `''` if datetime is naive).**
* **`'%z'` accepts `'±HH[:]MM'` and returns `'±HHMM'` or empty string if datetime is naive.**
* **`'%Z'` accepts `'UTC/GMT'` and local timezone's code and returns timezone's name, `'UTC[±HH:MM]'` if timezone is nameless, or an empty string if datetime is naive.**
* **For abbreviated weekday and month use `'%a'` and `'%b'`.**
* **For abbreviated weekday and month use `'%a'` and `'%b'`.**
<pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span>it.product(<spanclass="hljs-string">'abc'</span>, <spanclass="hljs-string">'abc'</span>)<spanclass="hljs-comment"># a b c</span>
[(<spanclass="hljs-string">'a'</span>, <spanclass="hljs-string">'a'</span>), (<spanclass="hljs-string">'a'</span>, <spanclass="hljs-string">'b'</span>), (<spanclass="hljs-string">'a'</span>, <spanclass="hljs-string">'c'</span>), <spanclass="hljs-comment"># a x x x</span>
(<spanclass="hljs-string">'b'</span>, <spanclass="hljs-string">'a'</span>), (<spanclass="hljs-string">'b'</span>, <spanclass="hljs-string">'b'</span>), (<spanclass="hljs-string">'b'</span>, <spanclass="hljs-string">'c'</span>), <spanclass="hljs-comment"># b x x x</span>
(<spanclass="hljs-string">'c'</span>, <spanclass="hljs-string">'a'</span>), (<spanclass="hljs-string">'c'</span>, <spanclass="hljs-string">'b'</span>), (<spanclass="hljs-string">'c'</span>, <spanclass="hljs-string">'c'</span>)] <spanclass="hljs-comment"># c x x x</span>
<pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span>list(it.product(<spanclass="hljs-string">'abc'</span>, <spanclass="hljs-string">'abc'</span>))<spanclass="hljs-comment"># a b c</span>
[(<spanclass="hljs-string">'a'</span>, <spanclass="hljs-string">'a'</span>), (<spanclass="hljs-string">'a'</span>, <spanclass="hljs-string">'b'</span>), (<spanclass="hljs-string">'a'</span>, <spanclass="hljs-string">'c'</span>), <spanclass="hljs-comment"># a x x x</span>
(<spanclass="hljs-string">'b'</span>, <spanclass="hljs-string">'a'</span>), (<spanclass="hljs-string">'b'</span>, <spanclass="hljs-string">'b'</span>), (<spanclass="hljs-string">'b'</span>, <spanclass="hljs-string">'c'</span>), <spanclass="hljs-comment"># b x x x</span>
(<spanclass="hljs-string">'c'</span>, <spanclass="hljs-string">'a'</span>), (<spanclass="hljs-string">'c'</span>, <spanclass="hljs-string">'b'</span>), (<spanclass="hljs-string">'c'</span>, <spanclass="hljs-string">'c'</span>)] <spanclass="hljs-comment"># c x x x</span>
</code></pre>
</code></pre>
<pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span>it.combinations(<spanclass="hljs-string">'abc'</span>, <spanclass="hljs-number">2</span>)<spanclass="hljs-comment"># a b c</span>
[(<spanclass="hljs-string">'a'</span>, <spanclass="hljs-string">'b'</span>), (<spanclass="hljs-string">'a'</span>, <spanclass="hljs-string">'c'</span>), <spanclass="hljs-comment"># a . x x</span>
(<spanclass="hljs-string">'b'</span>, <spanclass="hljs-string">'c'</span>)] <spanclass="hljs-comment"># b . . x</span>
<pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span>list(it.combinations(<spanclass="hljs-string">'abc'</span>, <spanclass="hljs-number">2</span>))<spanclass="hljs-comment"># a b c</span>
[(<spanclass="hljs-string">'a'</span>, <spanclass="hljs-string">'b'</span>), (<spanclass="hljs-string">'a'</span>, <spanclass="hljs-string">'c'</span>), <spanclass="hljs-comment"># a . x x</span>
(<spanclass="hljs-string">'b'</span>, <spanclass="hljs-string">'c'</span>)] <spanclass="hljs-comment"># b . . x</span>
</code></pre>
</code></pre>
<pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span>it.combinations_with_replacement(<spanclass="hljs-string">'abc'</span>, <spanclass="hljs-number">2</span>)<spanclass="hljs-comment"># a b c</span>
[(<spanclass="hljs-string">'a'</span>, <spanclass="hljs-string">'a'</span>), (<spanclass="hljs-string">'a'</span>, <spanclass="hljs-string">'b'</span>), (<spanclass="hljs-string">'a'</span>, <spanclass="hljs-string">'c'</span>), <spanclass="hljs-comment"># a x x x</span>
(<spanclass="hljs-string">'b'</span>, <spanclass="hljs-string">'b'</span>), (<spanclass="hljs-string">'b'</span>, <spanclass="hljs-string">'c'</span>), <spanclass="hljs-comment"># b . x x</span>
(<spanclass="hljs-string">'c'</span>, <spanclass="hljs-string">'c'</span>)] <spanclass="hljs-comment"># c . . x</span>
<pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span>list(it.combinations_with_replacement(<spanclass="hljs-string">'abc'</span>, <spanclass="hljs-number">2</span>))<spanclass="hljs-comment"># a b c</span>
[(<spanclass="hljs-string">'a'</span>, <spanclass="hljs-string">'a'</span>), (<spanclass="hljs-string">'a'</span>, <spanclass="hljs-string">'b'</span>), (<spanclass="hljs-string">'a'</span>, <spanclass="hljs-string">'c'</span>), <spanclass="hljs-comment"># a x x x</span>
(<spanclass="hljs-string">'b'</span>, <spanclass="hljs-string">'b'</span>), (<spanclass="hljs-string">'b'</span>, <spanclass="hljs-string">'c'</span>), <spanclass="hljs-comment"># b . x x</span>
(<spanclass="hljs-string">'c'</span>, <spanclass="hljs-string">'c'</span>)] <spanclass="hljs-comment"># c . . x</span>
</code></pre>
</code></pre>
<pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span>it.permutations(<spanclass="hljs-string">'abc'</span>, <spanclass="hljs-number">2</span>)<spanclass="hljs-comment"># a b c</span>
[(<spanclass="hljs-string">'a'</span>, <spanclass="hljs-string">'b'</span>), (<spanclass="hljs-string">'a'</span>, <spanclass="hljs-string">'c'</span>), <spanclass="hljs-comment"># a . x x</span>
(<spanclass="hljs-string">'b'</span>, <spanclass="hljs-string">'a'</span>), (<spanclass="hljs-string">'b'</span>, <spanclass="hljs-string">'c'</span>), <spanclass="hljs-comment"># b x . x</span>
(<spanclass="hljs-string">'c'</span>, <spanclass="hljs-string">'a'</span>), (<spanclass="hljs-string">'c'</span>, <spanclass="hljs-string">'b'</span>)] <spanclass="hljs-comment"># c x x .</span>
<pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span>list(it.permutations(<spanclass="hljs-string">'abc'</span>, <spanclass="hljs-number">2</span>))<spanclass="hljs-comment"># a b c</span>
[(<spanclass="hljs-string">'a'</span>, <spanclass="hljs-string">'b'</span>), (<spanclass="hljs-string">'a'</span>, <spanclass="hljs-string">'c'</span>), <spanclass="hljs-comment"># a . x x</span>
(<spanclass="hljs-string">'b'</span>, <spanclass="hljs-string">'a'</span>), (<spanclass="hljs-string">'b'</span>, <spanclass="hljs-string">'c'</span>), <spanclass="hljs-comment"># b x . x</span>
(<spanclass="hljs-string">'c'</span>, <spanclass="hljs-string">'a'</span>), (<spanclass="hljs-string">'c'</span>, <spanclass="hljs-string">'b'</span>)] <spanclass="hljs-comment"># c x x .</span>
<li><strong>Module 'datetime' provides 'date' <codeclass="apache hljs"><spanclass="hljs-section"><D></span></code>, 'time' <codeclass="apache hljs"><spanclass="hljs-section"><T></span></code>, 'datetime' <codeclass="apache hljs"><spanclass="hljs-section"><DT></span></code> and 'timedelta' <codeclass="apache hljs"><spanclass="hljs-section"><TD></span></code> classes. All are immutable and hashable.</strong></li>
<li><strong>Time and datetime objects can be 'aware' <codeclass="apache hljs"><spanclass="hljs-section"><a></span></code>, meaning they have defined timezone, or 'naive' <codeclass="apache hljs"><spanclass="hljs-section"><n></span></code>, meaning they don't.</strong></li>
<li><strong>If object is naive, it is presumed to be in the system's timezone.</strong></li>
</ul><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">from</span> datetime <spanclass="hljs-keyword">import</span> date, time, datetime, timedelta
<div><h2id="datetime"><ahref="#datetime"name="datetime">#</a>Datetime</h2><p><strong>Provides 'date', 'time', 'datetime' and 'timedelta' classes. All are immutable and hashable.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-comment"># pip3 install python-dateutil</span>
<spanclass="hljs-keyword">from</span> datetime <spanclass="hljs-keyword">import</span> date, time, datetime, timedelta, timezone
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">'<D/DT>.weekday()'</span></code> to get the day of the week as an int, with Monday being 0.</strong></li>
<li><strong>Aware <codeclass="apache hljs"><spanclass="hljs-section"><a></span></code> time and datetime objects have defined timezone, while naive <codeclass="apache hljs"><spanclass="hljs-section"><n></span></code> don't.</strong></li>
<li><strong>If object is naive, it is presumed to be in the system's timezone.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'fold=1'</span></code> means the second pass in case of time jumping back for one hour.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'fold=1'</span></code> means the second pass in case of time jumping back for one hour.</strong></li>
<li><strong>Timedelta normalizes arguments to ±days, seconds (< 86 400) and microseconds (< 1M).</strong></li>
<li><strong>Timedelta normalizes arguments to ±days, seconds (< 86 400) and microseconds (< 1M).</strong></li>
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">'<D/DT>.weekday()'</span></code> to get the day of the week as an int, with Monday being 0.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'<DTa> = resolve_imaginary(<DTa>)'</span></code> fixes DTs that fall into the missing hour.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'<DTa> = resolve_imaginary(<DTa>)'</span></code> fixes DTs that fall into the missing hour.</strong></li>
</ul>
</ul>
<div><h3id="now">Now</h3><pre><codeclass="python language-python hljs"><D/DTn> = D/DT.today() <spanclass="hljs-comment"># Current local date or naive datetime.</span>
<div><h3id="now">Now</h3><pre><codeclass="python language-python hljs"><D/DTn> = D/DT.today() <spanclass="hljs-comment"># Current local date or naive datetime.</span>
<li><strong>To extract time use <codeclass="python hljs"><spanclass="hljs-string">'<DTn>.time()'</span></code>, <codeclass="python hljs"><spanclass="hljs-string">'<DTa>.time()'</span></code> or <codeclass="python hljs"><spanclass="hljs-string">'<DTa>.timetz()'</span></code>.</strong></li>
<li><strong>To extract time use <codeclass="python hljs"><spanclass="hljs-string">'<DTn>.time()'</span></code>, <codeclass="python hljs"><spanclass="hljs-string">'<DTa>.time()'</span></code> or <codeclass="python hljs"><spanclass="hljs-string">'<DTa>.timetz()'</span></code>.</strong></li>
</ul>
</ul>
<div><h3id="timezone">Timezone</h3><pre><codeclass="python language-python apache hljs"><tzinfo> = UTC <spanclass="hljs-comment"># UTC timezone. London without DST.</span>
<div><h3id="timezone">Timezone</h3><pre><codeclass="python language-python apache hljs"><tzinfo> = timezone.utc <spanclass="hljs-comment"># London without daylight saving time.</span>
<tzinfo> = timezone(<timedelta>) <spanclass="hljs-comment"># Timezone with fixed offset from UTC.</span>
<tzinfo> = tzlocal() <spanclass="hljs-comment"># Local timezone. Also gettz().</span>
<tzinfo> = tzlocal() <spanclass="hljs-comment"># Local timezone. Also gettz().</span>
<tzinfo> = gettz(<spanclass="hljs-string">'<Continent>/<City>'</span>) <spanclass="hljs-comment"># 'Continent/City_Name' timezone or None.</span>
<tzinfo> = gettz(<spanclass="hljs-string">'<Continent>/<City>'</span>) <spanclass="hljs-comment"># 'Continent/City_Name' timezone or None.</span>
<DTa> = <DT>.astimezone(<tzinfo>) <spanclass="hljs-comment"># Datetime, converted to the passed timezone.</span>
<DTa> = <DT>.astimezone(<tzinfo>) <spanclass="hljs-comment"># Datetime, converted to the passed timezone.</span>
<Ta/DTa> = <T/DT>.replace(tzinfo=<tzinfo>) <spanclass="hljs-comment"># Unconverted object with a new timezone.</span>
<Ta/DTa> = <T/DT>.replace(tzinfo=<tzinfo>) <spanclass="hljs-comment"># Unconverted object with a new timezone.</span>
</code></pre></div>
</code></pre></div>
<ul>
<li><strong>Standard library's zoneinfo.ZoneInfo() can be used instead of gettz() on Python 3.9 and later. It requires 'tzdata' package on Windows.</strong></li>
</ul>
<div><h3id="encode">Encode</h3><pre><codeclass="python language-python apache hljs"><D/T/DT> = D/T/DT.fromisoformat(<spanclass="hljs-string">'<iso>'</span>) <spanclass="hljs-comment"># Object from ISO string. Raises ValueError.</span>
<div><h3id="encode">Encode</h3><pre><codeclass="python language-python apache hljs"><D/T/DT> = D/T/DT.fromisoformat(<spanclass="hljs-string">'<iso>'</span>) <spanclass="hljs-comment"># Object from ISO string. Raises ValueError.</span>
<DT> = DT.strptime(<str>, <spanclass="hljs-string">'<format>'</span>) <spanclass="hljs-comment"># Datetime from str, according to format.</span>
<DT> = DT.strptime(<str>, <spanclass="hljs-string">'<format>'</span>) <spanclass="hljs-comment"># Datetime from str, according to format.</span>
<D/DTn> = D/DT.fromordinal(<int>) <spanclass="hljs-comment"># D/DTn from days since the Gregorian NYE 1.</span>
<D/DTn> = D/DT.fromordinal(<int>) <spanclass="hljs-comment"># D/DTn from days since the Gregorian NYE 1.</span>
<li><strong>Format code <codeclass="python hljs"><spanclass="hljs-string">'%z'</span></code> accepts <codeclass="python hljs"><spanclass="hljs-string">'±HH[:]MM'</span></code> and returns <codeclass="python hljs"><spanclass="hljs-string">'±HHMM'</span></code> (or <codeclass="python hljs"><spanclass="hljs-string">''</span></code> if datetime is naive).</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'%z'</span></code> accepts <codeclass="python hljs"><spanclass="hljs-string">'±HH[:]MM'</span></code> and returns <codeclass="python hljs"><spanclass="hljs-string">'±HHMM'</span></code> or empty string if datetime is naive.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'%Z'</span></code> accepts <codeclass="python hljs"><spanclass="hljs-string">'UTC/GMT'</span></code> and local timezone's code and returns timezone's name, <codeclass="python hljs"><spanclass="hljs-string">'UTC[±HH:MM]'</span></code> if timezone is nameless, or an empty string if datetime is naive.</strong></li>
<li><strong>For abbreviated weekday and month use <codeclass="python hljs"><spanclass="hljs-string">'%a'</span></code> and <codeclass="python hljs"><spanclass="hljs-string">'%b'</span></code>.</strong></li>
<li><strong>For abbreviated weekday and month use <codeclass="python hljs"><spanclass="hljs-string">'%a'</span></code> and <codeclass="python hljs"><spanclass="hljs-string">'%b'</span></code>.</strong></li>
</ul>
</ul>
<div><h3id="arithmetics">Arithmetics</h3><pre><codeclass="python language-python apache hljs"><D/DT> = <D/DT> ± <TD><spanclass="hljs-comment"># Returned datetime can fall into missing hour.</span>
<div><h3id="arithmetics">Arithmetics</h3><pre><codeclass="python language-python apache hljs"><D/DT> = <D/DT> ± <TD><spanclass="hljs-comment"># Returned datetime can fall into missing hour.</span>
<TD> = <D/DTn> - <D/DTn><spanclass="hljs-comment"># Returns the difference. Ignores time jumps.</span>
<TD> = <D/DTn> - <D/DTn><spanclass="hljs-comment"># Returns the difference. Ignores time jumps.</span>
<TD> = <DTa> - <DTa><spanclass="hljs-comment"># Ignores time jumps if they share tzinfo object.</span>
<TD> = <DTa> - <DTa><spanclass="hljs-comment"># Ignores time jumps if they share tzinfo object.</span>