Some fixes to the datetime_utils.
authorScott Gasch <[email protected]>
Mon, 25 Oct 2021 01:06:20 +0000 (18:06 -0700)
committerScott Gasch <[email protected]>
Mon, 25 Oct 2021 01:06:20 +0000 (18:06 -0700)
datetime_utils.py
site_config.py

index e31edc29428ad57a0d02282e9ee4ccdc8c55c546..310ffe154d116348fa65e4c707a421c3c6721de0 100644 (file)
@@ -16,18 +16,69 @@ import constants
 logger = logging.getLogger(__name__)
 
 
+def is_timezone_aware(dt: datetime.datetime) -> bool:
+    """See: https://docs.python.org/3/library/datetime.html
+                               #determining-if-an-object-is-aware-or-naive
+
+    >>> is_timezone_aware(datetime.datetime.now())
+    False
+
+    >>> is_timezone_aware(now_pacific())
+    True
+
+    """
+    return (
+        dt.tzinfo is not None and
+        dt.tzinfo.utcoffset(dt) is not None
+    )
+
+
+def is_timezone_naive(dt: datetime.datetime) -> bool:
+    return not is_timezone_aware(dt)
+
+
 def replace_timezone(dt: datetime.datetime,
                      tz: datetime.tzinfo) -> datetime.datetime:
     """
-    Replaces the timezone on a datetime object.
+    Replaces the timezone on a datetime object directly (leaving
+    the year, month, day, hour, minute, second, micro, etc... alone).
+    Note: this changes the instant to which this dt refers.
 
     >>> from pytz import UTC
     >>> d = now_pacific()
     >>> d.tzinfo.tzname(d)[0]     # Note: could be PST or PDT
     'P'
+    >>> h = d.hour
     >>> o = replace_timezone(d, UTC)
     >>> o.tzinfo.tzname(o)
     'UTC'
+    >>> o.hour == h
+    True
+
+    """
+    return datetime.datetime(
+        dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.microsecond,
+        tzinfo=tz
+    )
+
+
+def translate_timezone(dt: datetime.datetime,
+                       tz: datetime.tzinfo) -> datetime.datetime:
+    """
+    Translates dt into a different timezone by adjusting the year, month,
+    day, hour, minute, second, micro, etc... appropriately.  The returned
+    dt is the same instant in another timezone.
+
+    >>> from pytz import UTC
+    >>> d = now_pacific()
+    >>> d.tzinfo.tzname(d)[0]     # Note: could be PST or PDT
+    'P'
+    >>> h = d.hour
+    >>> o = translate_timezone(d, UTC)
+    >>> o.tzinfo.tzname(o)
+    'UTC'
+    >>> o.hour == h
+    False
 
     """
     return dt.replace(tzinfo=None).astimezone(tz=tz)
@@ -44,7 +95,7 @@ def now_pacific() -> datetime.datetime:
     """
     What time is it?  Result in US/Pacific time (PST/PDT)
     """
-    return replace_timezone(now(), pytz.timezone("US/Pacific"))
+    return datetime.datetime.now(pytz.timezone("US/Pacific"))
 
 
 def date_to_datetime(date: datetime.date) -> datetime.datetime:
index 81185bf046d1004579679d082aa9d3aaa4841284..95ff5d403dcd900861c3a2c2ff8b37f3c5672c0a 100644 (file)
@@ -19,7 +19,7 @@ args.add_argument(
     const='AUTO',
     nargs='?',
     choices=('HOUSE', 'CABIN', 'AUTO'),
-    help='Where are we, HOUSE or CABIN?'
+    help='Where are we, HOUSE, CABIN or AUTO?',
 )