From 6ae442dbda10cd3d85e08df31e8accdef35247eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Tue, 5 Sep 2023 15:04:06 +0200 Subject: [PATCH] Updated Datetime --- README.md | 34 +++++++++++++++++----------------- index.html | 38 +++++++++++++++++++------------------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index cb224c8..c591e9a 100644 --- a/README.md +++ b/README.md @@ -589,7 +589,7 @@ Datetime ```python # pip3 install python-dateutil from datetime import date, time, datetime, timedelta, timezone -from dateutil.tz import tzlocal, gettz, datetime_exists, resolve_imaginary +from dateutil.tz import tzlocal, gettz ``` ```python @@ -602,25 +602,24 @@ from dateutil.tz import tzlocal, gettz, datetime_exists, resolve_imaginary * **`'fold=1'` means the second pass in case of time jumping back for one hour.** * **Timedelta normalizes arguments to ±days, seconds (< 86 400) and microseconds (< 1M).** * **Use `'.weekday()'` to get the day of the week as an int, with Monday being 0.** -* **`' = resolve_imaginary()'` fixes DTs that fall into the missing hour.** ### Now ```python - = D/DT.today() # Current local date or naive datetime. - = DT.utcnow() # Naive datetime from current UTC time. - = DT.now() # Aware datetime from current tz time. + = D/DT.today() # Current local date or naive DT. Also DT.now(). + = DT.now() # Aware DT from current time in passed timezone. ``` * **To extract time use `'.time()'`, `'.time()'` or `'.timetz()'`.** ### Timezone ```python - = timezone.utc # London without daylight saving time. + = timezone.utc # London without daylight saving time (DST). = timezone() # Timezone with fixed offset from UTC. = tzlocal() # Local timezone. Also gettz(). = gettz('/') # 'Continent/City_Name' timezone or None. - =
.astimezone([]) # Converts DT to the passed or local timezone. + =
.astimezone([]) # Converts DT to the passed or local fixed zone. = .replace(tzinfo=) # Changes object's timezone without conversion. ``` +* **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.** * **Standard library's zoneinfo.ZoneInfo() can be used instead of gettz() on Python 3.9 and later. It requires 'tzdata' package on Windows.** ### Encode @@ -637,7 +636,7 @@ from dateutil.tz import tzlocal, gettz, datetime_exists, resolve_imaginary ### Decode ```python = .isoformat(sep='T') # Also `timespec='auto/hours/minutes/seconds/…'`. - = .strftime('') # Custom string representation. + = .strftime('') # Custom string representation of the object. = .toordinal() # Days since Gregorian NYE 1, ignoring time and tz. = .timestamp() # Seconds since the Epoch, from DTn in local tz. = .timestamp() # Seconds since the Epoch, from aware datetime. @@ -645,21 +644,22 @@ from dateutil.tz import tzlocal, gettz, datetime_exists, resolve_imaginary ### Format ```python ->>> dt = datetime.strptime('2015-05-14 23:39:00.00 +0200', '%Y-%m-%d %H:%M:%S.%f %z') ->>> dt.strftime("%A, %dth of %B '%y, %I:%M%p %Z") -"Thursday, 14th of May '15, 11:39PM UTC+02:00" +>>> dt = datetime.strptime('2025-08-14 23:39:00.00 +0200', '%Y-%m-%d %H:%M:%S.%f %z') +>>> dt.strftime("%dth of %B '%y (%a), %I:%M%p %Z") +"14th of August '25 (Thu), 11:39PM UTC+02:00" ``` * **`'%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'`.** ### Arithmetics ```python - = ± # Returned datetime can fall into missing hour. - = - # Returns the difference. Ignores time jumps. - = - # Ignores time jumps if they share tzinfo object. - = * # Also: = abs() and = ±% . - = / # How many weeks/years there are in TD. Also //. + = > # Ignores time jumps (fold attribute). Also ==. + = > # Ignores time jumps if they share tzinfo object. + = - # Returns the difference. Ignores time jumps. + = - # Ignores time jumps if they share tzinfo object. + = ± # Returned datetime can fall into missing hour. + = * # Also: = abs() and = ±% . + = / # How many weeks/years there are in TD. Also //. ``` diff --git a/index.html b/index.html index 7cf85dd..91acd37 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@
- +
@@ -517,7 +517,7 @@ Point(x=1, y=2

#Datetime

Provides 'date', 'time', 'datetime' and 'timedelta' classes. All are immutable and hashable.

# pip3 install python-dateutil
 from datetime import date, time, datetime, timedelta, timezone
-from dateutil.tz import tzlocal, gettz, datetime_exists, resolve_imaginary
+from dateutil.tz import tzlocal, gettz
 
@@ -531,25 +531,24 @@ Point(x=1, y=2
  • 'fold=1' means the second pass in case of time jumping back for one hour.
  • Timedelta normalizes arguments to ±days, seconds (< 86 400) and microseconds (< 1M).
  • Use '<D/DT>.weekday()' to get the day of the week as an int, with Monday being 0.
  • -
  • '<DTa> = resolve_imaginary(<DTa>)' fixes DTs that fall into the missing hour.
  • -

    Now

    <D/DTn>  = D/DT.today()                     # Current local date or naive datetime.
    -<DTn>    = DT.utcnow()                      # Naive datetime from current UTC time.
    -<DTa>    = DT.now(<tzinfo>)                 # Aware datetime from current tz time.
    +

    Now

    <D/DTn>  = D/DT.today()                     # Current local date or naive DT. Also DT.now().
    +<DTa>    = DT.now(<tzinfo>)                 # Aware DT from current time in passed timezone.
     
    • To extract time use '<DTn>.time()', '<DTa>.time()' or '<DTa>.timetz()'.
    -

    Timezone

    <tzinfo> = timezone.utc                     # London without daylight saving time.
    +

    Timezone

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

    Encode

    <D/T/DT> = D/T/DT.fromisoformat('<iso>')    # Object from ISO string. Raises ValueError.
    @@ -564,27 +563,28 @@ Point(x=1, y=2
     
  • Python uses the Unix Epoch: '1970-01-01 00:00 UTC', '1970-01-01 01:00 CET', …
  • Decode

    <str>    = <D/T/DT>.isoformat(sep='T')      # Also `timespec='auto/hours/minutes/seconds/…'`.
    -<str>    = <D/T/DT>.strftime('<format>')    # Custom string representation.
    +<str>    = <D/T/DT>.strftime('<format>')    # Custom string representation of the object.
     <int>    = <D/DT>.toordinal()               # Days since Gregorian NYE 1, ignoring time and tz.
     <float>  = <DTn>.timestamp()                # Seconds since the Epoch, from DTn in local tz.
     <float>  = <DTa>.timestamp()                # Seconds since the Epoch, from aware datetime.
     
    -

    Format

    >>> dt = datetime.strptime('2015-05-14 23:39:00.00 +0200', '%Y-%m-%d %H:%M:%S.%f %z')
    ->>> dt.strftime("%A, %dth of %B '%y, %I:%M%p %Z")
    -"Thursday, 14th of May '15, 11:39PM UTC+02:00"
    +

    Format

    >>> dt = datetime.strptime('2025-08-14 23:39:00.00 +0200', '%Y-%m-%d %H:%M:%S.%f %z')
    +>>> dt.strftime("%dth of %B '%y (%a), %I:%M%p %Z")
    +"14th of August '25 (Thu), 11:39PM UTC+02:00"
     
    • '%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'.
    -

    Arithmetics

    <D/DT>   = <D/DT>  ± <TD>                   # Returned datetime can fall into missing hour.
    -<TD>     = <D/DTn> - <D/DTn>                # Returns the difference. Ignores time jumps.
    -<TD>     = <DTa>   - <DTa>                  # Ignores time jumps if they share tzinfo object.
    -<TD>     = <TD>    * <int/float>            # Also: <TD> = abs(<TD>) and <TD> = <TD> ±% <TD>.
    -<float>  = <TD>    / <TD>                   # How many weeks/years there are in TD. Also //.
    +

    Arithmetics

    <bool>   = <D/T/DTn> > <D/T/DTn>            # Ignores time jumps (fold attribute). Also ==.
    +<bool>   = <DTa>     > <DTa>                # Ignores time jumps if they share tzinfo object.
    +<TD>     = <D/DTn>   - <D/DTn>              # Returns the difference. Ignores time jumps.
    +<TD>     = <DTa>     - <DTa>                # Ignores time jumps if they share tzinfo object.
    +<D/DT>   = <D/DT>    ± <TD>                 # Returned datetime can fall into missing hour.
    +<TD>     = <TD>      * <int/float>          # Also: <TD> = abs(<TD>) and <TD> = <TD> ±% <TD>.
    +<float>  = <TD>      / <TD>                 # How many weeks/years there are in TD. Also //.
     

    #Arguments

    Inside Function Call

    func(<positional_args>)                           # func(0, 0)
    @@ -2928,7 +2928,7 @@ $ deactivate                  # Deactivates the activ