Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6ae442d

Browse files
committed
Updated Datetime
1 parent 622e6ab commit 6ae442d

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ Datetime
589589
```python
590590
# pip3 install python-dateutil
591591
from datetime import date, time, datetime, timedelta, timezone
592-
from dateutil.tz import tzlocal, gettz, datetime_exists, resolve_imaginary
592+
from dateutil.tz import tzlocal, gettz
593593
```
594594

595595
```python
@@ -602,25 +602,24 @@ from dateutil.tz import tzlocal, gettz, datetime_exists, resolve_imaginary
602602
* **`'fold=1'` means the second pass in case of time jumping back for one hour.**
603603
* **Timedelta normalizes arguments to ±days, seconds (< 86 400) and microseconds (< 1M).**
604604
* **Use `'<D/DT>.weekday()'` to get the day of the week as an int, with Monday being 0.**
605-
* **`'<DTa> = resolve_imaginary(<DTa>)'` fixes DTs that fall into the missing hour.**
606605

607606
### Now
608607
```python
609-
<D/DTn> = D/DT.today() # Current local date or naive datetime.
610-
<DTn> = DT.utcnow() # Naive datetime from current UTC time.
611-
<DTa> = DT.now(<tzinfo>) # Aware datetime from current tz time.
608+
<D/DTn> = D/DT.today() # Current local date or naive DT. Also DT.now().
609+
<DTa> = DT.now(<tzinfo>) # Aware DT from current time in passed timezone.
612610
```
613611
* **To extract time use `'<DTn>.time()'`, `'<DTa>.time()'` or `'<DTa>.timetz()'`.**
614612

615613
### Timezone
616614
```python
617-
<tzinfo> = timezone.utc # London without daylight saving time.
615+
<tzinfo> = timezone.utc # London without daylight saving time (DST).
618616
<tzinfo> = timezone(<timedelta>) # Timezone with fixed offset from UTC.
619617
<tzinfo> = tzlocal() # Local timezone. Also gettz().
620618
<tzinfo> = gettz('<Continent>/<City>') # 'Continent/City_Name' timezone or None.
621-
<DTa> = <DT>.astimezone([<tzinfo>]) # Converts DT to the passed or local timezone.
619+
<DTa> = <DT>.astimezone([<tzinfo>]) # Converts DT to the passed or local fixed zone.
622620
<Ta/DTa> = <T/DT>.replace(tzinfo=<tzinfo>) # Changes object's timezone without conversion.
623621
```
622+
* **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.**
624623
* **Standard library's zoneinfo.ZoneInfo() can be used instead of gettz() on Python 3.9 and later. It requires 'tzdata' package on Windows.**
625624

626625
### Encode
@@ -637,29 +636,30 @@ from dateutil.tz import tzlocal, gettz, datetime_exists, resolve_imaginary
637636
### Decode
638637
```python
639638
<str> = <D/T/DT>.isoformat(sep='T') # Also `timespec='auto/hours/minutes/seconds/…'`.
640-
<str> = <D/T/DT>.strftime('<format>') # Custom string representation.
639+
<str> = <D/T/DT>.strftime('<format>') # Custom string representation of the object.
641640
<int> = <D/DT>.toordinal() # Days since Gregorian NYE 1, ignoring time and tz.
642641
<float> = <DTn>.timestamp() # Seconds since the Epoch, from DTn in local tz.
643642
<float> = <DTa>.timestamp() # Seconds since the Epoch, from aware datetime.
644643
```
645644

646645
### Format
647646
```python
648-
>>> dt = datetime.strptime('2015-05-14 23:39:00.00 +0200', '%Y-%m-%d %H:%M:%S.%f %z')
649-
>>> dt.strftime("%A, %dth of %B '%y, %I:%M%p %Z")
650-
"Thursday, 14th of May '15, 11:39PM UTC+02:00"
647+
>>> dt = datetime.strptime('2025-08-14 23:39:00.00 +0200', '%Y-%m-%d %H:%M:%S.%f %z')
648+
>>> dt.strftime("%dth of %B '%y (%a), %I:%M%p %Z")
649+
"14th of August '25 (Thu), 11:39PM UTC+02:00"
651650
```
652651
* **`'%z'` accepts `'±HH[:]MM'` and returns `'±HHMM'` or empty string if datetime is naive.**
653652
* **`'%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.**
654-
* **For abbreviated weekday and month use `'%a'` and `'%b'`.**
655653

656654
### Arithmetics
657655
```python
658-
<D/DT> = <D/DT> ± <TD> # Returned datetime can fall into missing hour.
659-
<TD> = <D/DTn> - <D/DTn> # Returns the difference. Ignores time jumps.
660-
<TD> = <DTa> - <DTa> # Ignores time jumps if they share tzinfo object.
661-
<TD> = <TD> * <int/float> # Also: <TD> = abs(<TD>) and <TD> = <TD> ±% <TD>.
662-
<float> = <TD> / <TD> # How many weeks/years there are in TD. Also //.
656+
<bool> = <D/T/DTn> > <D/T/DTn> # Ignores time jumps (fold attribute). Also ==.
657+
<bool> = <DTa> > <DTa> # Ignores time jumps if they share tzinfo object.
658+
<TD> = <D/DTn> - <D/DTn> # Returns the difference. Ignores time jumps.
659+
<TD> = <DTa> - <DTa> # Ignores time jumps if they share tzinfo object.
660+
<D/DT> = <D/DT> ± <TD> # Returned datetime can fall into missing hour.
661+
<TD> = <TD> * <int/float> # Also: <TD> = abs(<TD>) and <TD> = <TD> ±% <TD>.
662+
<float> = <TD> / <TD> # How many weeks/years there are in TD. Also //.
663663
```
664664

665665

index.html

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
<body>
5656
<header>
57-
<aside>August 31, 2023</aside>
57+
<aside>September 5, 2023</aside>
5858
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
5959
</header>
6060

@@ -517,7 +517,7 @@
517517
</code></pre>
518518
<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>
519519
<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
521521
</code></pre></div>
522522

523523

@@ -531,25 +531,24 @@
531531
<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>
532532
<li><strong>Timedelta normalizes arguments to ±days, seconds (&lt; 86 400) and microseconds (&lt; 1M).</strong></li>
533533
<li><strong>Use <code class="python hljs"><span class="hljs-string">'&lt;D/DT&gt;.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">'&lt;DTa&gt; = resolve_imaginary(&lt;DTa&gt;)'</span></code> fixes DTs that fall into the missing hour.</strong></li>
535534
</ul>
536-
<div><h3 id="now">Now</h3><pre><code class="python language-python hljs">&lt;D/DTn&gt; = D/DT.today() <span class="hljs-comment"># Current local date or naive datetime.</span>
537-
&lt;DTn&gt; = DT.utcnow() <span class="hljs-comment"># Naive datetime from current UTC time.</span>
538-
&lt;DTa&gt; = DT.now(&lt;tzinfo&gt;) <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">&lt;D/DTn&gt; = D/DT.today() <span class="hljs-comment"># Current local date or naive DT. Also DT.now().</span>
536+
&lt;DTa&gt; = DT.now(&lt;tzinfo&gt;) <span class="hljs-comment"># Aware DT from current time in passed timezone.</span>
539537
</code></pre></div>
540538

541539
<ul>
542540
<li><strong>To extract time use <code class="python hljs"><span class="hljs-string">'&lt;DTn&gt;.time()'</span></code>, <code class="python hljs"><span class="hljs-string">'&lt;DTa&gt;.time()'</span></code> or <code class="python hljs"><span class="hljs-string">'&lt;DTa&gt;.timetz()'</span></code>.</strong></li>
543541
</ul>
544-
<div><h3 id="timezone">Timezone</h3><pre><code class="python language-python apache hljs">&lt;tzinfo&gt; = 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">&lt;tzinfo&gt; = timezone.utc <span class="hljs-comment"># London without daylight saving time (DST).</span>
545543
&lt;tzinfo&gt; = timezone(&lt;timedelta&gt;) <span class="hljs-comment"># Timezone with fixed offset from UTC.</span>
546544
&lt;tzinfo&gt; = tzlocal() <span class="hljs-comment"># Local timezone. Also gettz().</span>
547545
&lt;tzinfo&gt; = gettz(<span class="hljs-string">'&lt;Continent&gt;/&lt;City&gt;'</span>) <span class="hljs-comment"># 'Continent/City_Name' timezone or None.</span>
548-
&lt;DTa&gt; = &lt;DT&gt;.astimezone([&lt;tzinfo&gt;]) <span class="hljs-comment"># Converts DT to the passed or local timezone.</span>
546+
&lt;DTa&gt; = &lt;DT&gt;.astimezone([&lt;tzinfo&gt;]) <span class="hljs-comment"># Converts DT to the passed or local fixed zone.</span>
549547
&lt;Ta/DTa&gt; = &lt;T/DT&gt;.replace(tzinfo=&lt;tzinfo&gt;) <span class="hljs-comment"># Changes object's timezone without conversion.</span>
550548
</code></pre></div>
551549

552550
<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>
553552
<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>
554553
</ul>
555554
<div><h3 id="encode">Encode</h3><pre><code class="python language-python apache hljs">&lt;D/T/DT&gt; = D/T/DT.fromisoformat(<span class="hljs-string">'&lt;iso&gt;'</span>) <span class="hljs-comment"># Object from ISO string. Raises ValueError.</span>
@@ -564,27 +563,28 @@
564563
<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>
565564
</ul>
566565
<div><h3 id="decode">Decode</h3><pre><code class="python language-python hljs">&lt;str&gt; = &lt;D/T/DT&gt;.isoformat(sep=<span class="hljs-string">'T'</span>) <span class="hljs-comment"># Also `timespec='auto/hours/minutes/seconds/…'`.</span>
567-
&lt;str&gt; = &lt;D/T/DT&gt;.strftime(<span class="hljs-string">'&lt;format&gt;'</span>) <span class="hljs-comment"># Custom string representation.</span>
566+
&lt;str&gt; = &lt;D/T/DT&gt;.strftime(<span class="hljs-string">'&lt;format&gt;'</span>) <span class="hljs-comment"># Custom string representation of the object.</span>
568567
&lt;int&gt; = &lt;D/DT&gt;.toordinal() <span class="hljs-comment"># Days since Gregorian NYE 1, ignoring time and tz.</span>
569568
&lt;float&gt; = &lt;DTn&gt;.timestamp() <span class="hljs-comment"># Seconds since the Epoch, from DTn in local tz.</span>
570569
&lt;float&gt; = &lt;DTa&gt;.timestamp() <span class="hljs-comment"># Seconds since the Epoch, from aware datetime.</span>
571570
</code></pre></div>
572571

573-
<div><h3 id="format-1">Format</h3><pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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>
576575
</code></pre></div>
577576

578577
<ul>
579578
<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>
580579
<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>
582580
</ul>
583-
<div><h3 id="arithmetics">Arithmetics</h3><pre><code class="python language-python apache hljs">&lt;D/DT&gt; = &lt;D/DT&gt; ± &lt;TD&gt; <span class="hljs-comment"># Returned datetime can fall into missing hour.</span>
584-
&lt;TD&gt; = &lt;D/DTn&gt; - &lt;D/DTn&gt; <span class="hljs-comment"># Returns the difference. Ignores time jumps.</span>
585-
&lt;TD&gt; = &lt;DTa&gt; - &lt;DTa&gt; <span class="hljs-comment"># Ignores time jumps if they share tzinfo object.</span>
586-
&lt;TD&gt; = &lt;TD&gt; * &lt;int/float&gt; <span class="hljs-comment"># Also: &lt;TD&gt; = abs(&lt;TD&gt;) and &lt;TD&gt; = &lt;TD&gt; ±% &lt;TD&gt;.</span>
587-
&lt;float&gt; = &lt;TD&gt; / &lt;TD&gt; <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">&lt;bool&gt; = &lt;D/T/DTn&gt; &gt; &lt;D/T/DTn&gt; <span class="hljs-comment"># Ignores time jumps (fold attribute). Also ==.</span>
582+
&lt;bool&gt; = &lt;DTa&gt; &gt; &lt;DTa&gt; <span class="hljs-comment"># Ignores time jumps if they share tzinfo object.</span>
583+
&lt;TD&gt; = &lt;D/DTn&gt; - &lt;D/DTn&gt; <span class="hljs-comment"># Returns the difference. Ignores time jumps.</span>
584+
&lt;TD&gt; = &lt;DTa&gt; - &lt;DTa&gt; <span class="hljs-comment"># Ignores time jumps if they share tzinfo object.</span>
585+
&lt;D/DT&gt; = &lt;D/DT&gt; ± &lt;TD&gt; <span class="hljs-comment"># Returned datetime can fall into missing hour.</span>
586+
&lt;TD&gt; = &lt;TD&gt; * &lt;int/float&gt; <span class="hljs-comment"># Also: &lt;TD&gt; = abs(&lt;TD&gt;) and &lt;TD&gt; = &lt;TD&gt; ±% &lt;TD&gt;.</span>
587+
&lt;float&gt; = &lt;TD&gt; / &lt;TD&gt; <span class="hljs-comment"># How many weeks/years there are in TD. Also //.</span>
588588
</code></pre></div>
589589

590590
<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(&lt;positional_args&gt;) <span class="hljs-comment"># func(0, 0)</span>
@@ -2928,7 +2928,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
29282928

29292929

29302930
<footer>
2931-
<aside>August 31, 2023</aside>
2931+
<aside>September 5, 2023</aside>
29322932
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
29332933
</footer>
29342934

0 commit comments

Comments
 (0)