|
54 | 54 |
|
55 | 55 | <body>
|
56 | 56 | <header>
|
57 |
| - <aside>August 31, 2023</aside> |
| 57 | + <aside>September 5, 2023</aside> |
58 | 58 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
59 | 59 | </header>
|
60 | 60 |
|
|
517 | 517 | </code></pre>
|
518 | 518 | <div><h2 id="datetime"><a href="#datetime" name="datetime">#</a>Datetime</h2><p><strong>Provides 'date', 'time', 'datetime' and 'timedelta' classes. All are immutable and hashable.</strong></p><pre><code class="python language-python hljs"><span class="hljs-comment"># pip3 install python-dateutil</span>
|
519 | 519 | <span class="hljs-keyword">from</span> datetime <span class="hljs-keyword">import</span> date, time, datetime, timedelta, timezone
|
520 |
| -<span class="hljs-keyword">from</span> dateutil.tz <span class="hljs-keyword">import</span> tzlocal, gettz, datetime_exists, resolve_imaginary |
| 520 | +<span class="hljs-keyword">from</span> dateutil.tz <span class="hljs-keyword">import</span> tzlocal, gettz |
521 | 521 | </code></pre></div>
|
522 | 522 |
|
523 | 523 |
|
|
531 | 531 | <li><strong><code class="python hljs"><span class="hljs-string">'fold=1'</span></code> means the second pass in case of time jumping back for one hour.</strong></li>
|
532 | 532 | <li><strong>Timedelta normalizes arguments to ±days, seconds (< 86 400) and microseconds (< 1M).</strong></li>
|
533 | 533 | <li><strong>Use <code class="python hljs"><span class="hljs-string">'<D/DT>.weekday()'</span></code> to get the day of the week as an int, with Monday being 0.</strong></li>
|
534 |
| -<li><strong><code class="python hljs"><span class="hljs-string">'<DTa> = resolve_imaginary(<DTa>)'</span></code> fixes DTs that fall into the missing hour.</strong></li> |
535 | 534 | </ul>
|
536 |
| -<div><h3 id="now">Now</h3><pre><code class="python language-python hljs"><D/DTn> = D/DT.today() <span class="hljs-comment"># Current local date or naive datetime.</span> |
537 |
| -<DTn> = DT.utcnow() <span class="hljs-comment"># Naive datetime from current UTC time.</span> |
538 |
| -<DTa> = DT.now(<tzinfo>) <span class="hljs-comment"># Aware datetime from current tz time.</span> |
| 535 | +<div><h3 id="now">Now</h3><pre><code class="python language-python hljs"><D/DTn> = D/DT.today() <span class="hljs-comment"># Current local date or naive DT. Also DT.now().</span> |
| 536 | +<DTa> = DT.now(<tzinfo>) <span class="hljs-comment"># Aware DT from current time in passed timezone.</span> |
539 | 537 | </code></pre></div>
|
540 | 538 |
|
541 | 539 | <ul>
|
542 | 540 | <li><strong>To extract time use <code class="python hljs"><span class="hljs-string">'<DTn>.time()'</span></code>, <code class="python hljs"><span class="hljs-string">'<DTa>.time()'</span></code> or <code class="python hljs"><span class="hljs-string">'<DTa>.timetz()'</span></code>.</strong></li>
|
543 | 541 | </ul>
|
544 |
| -<div><h3 id="timezone">Timezone</h3><pre><code class="python language-python apache hljs"><tzinfo> = timezone.utc <span class="hljs-comment"># London without daylight saving time.</span> |
| 542 | +<div><h3 id="timezone">Timezone</h3><pre><code class="python language-python apache hljs"><tzinfo> = timezone.utc <span class="hljs-comment"># London without daylight saving time (DST).</span> |
545 | 543 | <tzinfo> = timezone(<timedelta>) <span class="hljs-comment"># Timezone with fixed offset from UTC.</span>
|
546 | 544 | <tzinfo> = tzlocal() <span class="hljs-comment"># Local timezone. Also gettz().</span>
|
547 | 545 | <tzinfo> = gettz(<span class="hljs-string">'<Continent>/<City>'</span>) <span class="hljs-comment"># 'Continent/City_Name' timezone or None.</span>
|
548 |
| -<DTa> = <DT>.astimezone([<tzinfo>]) <span class="hljs-comment"># Converts DT to the passed or local timezone.</span> |
| 546 | +<DTa> = <DT>.astimezone([<tzinfo>]) <span class="hljs-comment"># Converts DT to the passed or local fixed zone.</span> |
549 | 547 | <Ta/DTa> = <T/DT>.replace(tzinfo=<tzinfo>) <span class="hljs-comment"># Changes object's timezone without conversion.</span>
|
550 | 548 | </code></pre></div>
|
551 | 549 |
|
552 | 550 | <ul>
|
| 551 | +<li><strong>Timezones returned by gettz(), tzlocal(), and implicit local timezone of naive objects have offsets that vary through time due to DST and historical changes of the zone's base offset.</strong></li> |
553 | 552 | <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>
|
554 | 553 | </ul>
|
555 | 554 | <div><h3 id="encode">Encode</h3><pre><code class="python language-python apache hljs"><D/T/DT> = D/T/DT.fromisoformat(<span class="hljs-string">'<iso>'</span>) <span class="hljs-comment"># Object from ISO string. Raises ValueError.</span>
|
|
564 | 563 | <li><strong>Python uses the Unix Epoch: <code class="python hljs"><span class="hljs-string">'1970-01-01 00:00 UTC'</span></code>, <code class="python hljs"><span class="hljs-string">'1970-01-01 01:00 CET'</span></code>, …</strong></li>
|
565 | 564 | </ul>
|
566 | 565 | <div><h3 id="decode">Decode</h3><pre><code class="python language-python hljs"><str> = <D/T/DT>.isoformat(sep=<span class="hljs-string">'T'</span>) <span class="hljs-comment"># Also `timespec='auto/hours/minutes/seconds/…'`.</span>
|
567 |
| -<str> = <D/T/DT>.strftime(<span class="hljs-string">'<format>'</span>) <span class="hljs-comment"># Custom string representation.</span> |
| 566 | +<str> = <D/T/DT>.strftime(<span class="hljs-string">'<format>'</span>) <span class="hljs-comment"># Custom string representation of the object.</span> |
568 | 567 | <int> = <D/DT>.toordinal() <span class="hljs-comment"># Days since Gregorian NYE 1, ignoring time and tz.</span>
|
569 | 568 | <float> = <DTn>.timestamp() <span class="hljs-comment"># Seconds since the Epoch, from DTn in local tz.</span>
|
570 | 569 | <float> = <DTa>.timestamp() <span class="hljs-comment"># Seconds since the Epoch, from aware datetime.</span>
|
571 | 570 | </code></pre></div>
|
572 | 571 |
|
573 |
| -<div><h3 id="format-1">Format</h3><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>dt = datetime.strptime(<span class="hljs-string">'2015-05-14 23:39:00.00 +0200'</span>, <span class="hljs-string">'%Y-%m-%d %H:%M:%S.%f %z'</span>) |
574 |
| -<span class="hljs-meta">>>> </span>dt.strftime(<span class="hljs-string">"%A, %dth of %B '%y, %I:%M%p %Z"</span>) |
575 |
| -<span class="hljs-string">"Thursday, 14th of May '15, 11:39PM UTC+02:00"</span> |
| 572 | +<div><h3 id="format-1">Format</h3><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>dt = datetime.strptime(<span class="hljs-string">'2025-08-14 23:39:00.00 +0200'</span>, <span class="hljs-string">'%Y-%m-%d %H:%M:%S.%f %z'</span>) |
| 573 | +<span class="hljs-meta">>>> </span>dt.strftime(<span class="hljs-string">"%dth of %B '%y (%a), %I:%M%p %Z"</span>) |
| 574 | +<span class="hljs-string">"14th of August '25 (Thu), 11:39PM UTC+02:00"</span> |
576 | 575 | </code></pre></div>
|
577 | 576 |
|
578 | 577 | <ul>
|
579 | 578 | <li><strong><code class="python hljs"><span class="hljs-string">'%z'</span></code> accepts <code class="python hljs"><span class="hljs-string">'±HH[:]MM'</span></code> and returns <code class="python hljs"><span class="hljs-string">'±HHMM'</span></code> or empty string if datetime is naive.</strong></li>
|
580 | 579 | <li><strong><code class="python hljs"><span class="hljs-string">'%Z'</span></code> accepts <code class="python hljs"><span class="hljs-string">'UTC/GMT'</span></code> and local timezone's code and returns timezone's name, <code class="python hljs"><span class="hljs-string">'UTC[±HH:MM]'</span></code> if timezone is nameless, or an empty string if datetime is naive.</strong></li>
|
581 |
| -<li><strong>For abbreviated weekday and month use <code class="python hljs"><span class="hljs-string">'%a'</span></code> and <code class="python hljs"><span class="hljs-string">'%b'</span></code>.</strong></li> |
582 | 580 | </ul>
|
583 |
| -<div><h3 id="arithmetics">Arithmetics</h3><pre><code class="python language-python apache hljs"><D/DT> = <D/DT> ± <TD> <span class="hljs-comment"># Returned datetime can fall into missing hour.</span> |
584 |
| -<TD> = <D/DTn> - <D/DTn> <span class="hljs-comment"># Returns the difference. Ignores time jumps.</span> |
585 |
| -<TD> = <DTa> - <DTa> <span class="hljs-comment"># Ignores time jumps if they share tzinfo object.</span> |
586 |
| -<TD> = <TD> * <int/float> <span class="hljs-comment"># Also: <TD> = abs(<TD>) and <TD> = <TD> ±% <TD>.</span> |
587 |
| -<float> = <TD> / <TD> <span class="hljs-comment"># How many weeks/years there are in TD. Also //.</span> |
| 581 | +<div><h3 id="arithmetics">Arithmetics</h3><pre><code class="python language-python apache hljs"><bool> = <D/T/DTn> > <D/T/DTn> <span class="hljs-comment"># Ignores time jumps (fold attribute). Also ==.</span> |
| 582 | +<bool> = <DTa> > <DTa> <span class="hljs-comment"># Ignores time jumps if they share tzinfo object.</span> |
| 583 | +<TD> = <D/DTn> - <D/DTn> <span class="hljs-comment"># Returns the difference. Ignores time jumps.</span> |
| 584 | +<TD> = <DTa> - <DTa> <span class="hljs-comment"># Ignores time jumps if they share tzinfo object.</span> |
| 585 | +<D/DT> = <D/DT> ± <TD> <span class="hljs-comment"># Returned datetime can fall into missing hour.</span> |
| 586 | +<TD> = <TD> * <int/float> <span class="hljs-comment"># Also: <TD> = abs(<TD>) and <TD> = <TD> ±% <TD>.</span> |
| 587 | +<float> = <TD> / <TD> <span class="hljs-comment"># How many weeks/years there are in TD. Also //.</span> |
588 | 588 | </code></pre></div>
|
589 | 589 |
|
590 | 590 | <div><h2 id="arguments"><a href="#arguments" name="arguments">#</a>Arguments</h2><div><h3 id="insidefunctioncall">Inside Function Call</h3><pre><code class="python language-python hljs">func(<positional_args>) <span class="hljs-comment"># func(0, 0)</span>
|
@@ -2928,7 +2928,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
|
2928 | 2928 |
|
2929 | 2929 |
|
2930 | 2930 | <footer>
|
2931 |
| - <aside>August 31, 2023</aside> |
| 2931 | + <aside>September 5, 2023</aside> |
2932 | 2932 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
2933 | 2933 | </footer>
|
2934 | 2934 |
|
|
0 commit comments