]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
dates: refactor `_get_time()` into a helper
authorAarni Koskela <akx@iki.fi>
Sun, 14 Feb 2016 18:04:45 +0000 (20:04 +0200)
committerAarni Koskela <akx@iki.fi>
Sat, 5 Mar 2016 19:23:54 +0000 (21:23 +0200)
babel/dates.py

index 65c59735630100dd39ebdad7c7c5c57a15745b1e..991d11b7f915ee02e7c42f4ac5bd5d40523ea8f9 100644 (file)
@@ -138,6 +138,32 @@ def _ensure_datetime_tzinfo(datetime, tzinfo=None):
     return datetime
 
 
+def _get_time(time, tzinfo=None):
+    """
+    Get a timezoned time from a given instant.
+
+    .. warning:: The return values of this function may depend on the system clock.
+
+    :param time: time, datetime or None
+    :rtype: time
+    """
+    if time is None:
+        time = datetime.utcnow()
+    elif isinstance(time, number_types):
+        time = datetime.utcfromtimestamp(time)
+    if time.tzinfo is None:
+        time = time.replace(tzinfo=UTC)
+    if isinstance(time, datetime):
+        if tzinfo is not None:
+            time = time.astimezone(tzinfo)
+            if hasattr(tzinfo, 'normalize'):  # pytz
+                time = tzinfo.normalize(time)
+        time = time.timetz()
+    elif tzinfo is not None:
+        time = time.replace(tzinfo=tzinfo)
+    return time
+
+
 def get_timezone(zone=None):
     """Looks up a timezone by name and returns it.  The timezone object
     returned comes from ``pytz`` and corresponds to the `tzinfo` interface and
@@ -753,20 +779,7 @@ def format_time(time=None, format='medium', tzinfo=None, locale=LC_TIME):
     :param tzinfo: the time-zone to apply to the time for display
     :param locale: a `Locale` object or a locale identifier
     """
-    if time is None:
-        time = datetime.utcnow()
-    elif isinstance(time, number_types):
-        time = datetime.utcfromtimestamp(time)
-    if time.tzinfo is None:
-        time = time.replace(tzinfo=UTC)
-    if isinstance(time, datetime):
-        if tzinfo is not None:
-            time = time.astimezone(tzinfo)
-            if hasattr(tzinfo, 'normalize'):  # pytz
-                time = tzinfo.normalize(time)
-        time = time.timetz()
-    elif tzinfo is not None:
-        time = time.replace(tzinfo=tzinfo)
+    time = _get_time(time, tzinfo)
 
     locale = Locale.parse(locale)
     if format in ('full', 'long', 'medium', 'short'):