]> git-server-git.apps.pok.os.sepia.ceph.com Git - teuthology.git/commitdiff
util.time: Add parse_timestamp
authorZack Cerza <zack@redhat.com>
Wed, 31 Jul 2024 20:11:52 +0000 (14:11 -0600)
committerZack Cerza <zack@redhat.com>
Thu, 8 Aug 2024 00:05:15 +0000 (18:05 -0600)
And move the format string to the time module.

Signed-off-by: Zack Cerza <zack@redhat.com>
teuthology/suite/run.py
teuthology/suite/test/test_run_.py
teuthology/util/test/test_time.py
teuthology/util/time.py

index 8fa45b5b4596aea833e204aa8b01ea4e509f7b23..600f1568feab17910f724d958cf0deb03bf859a4 100644 (file)
@@ -24,6 +24,7 @@ from teuthology.suite import util
 from teuthology.suite.merge import config_merge
 from teuthology.suite.build_matrix import build_matrix
 from teuthology.suite.placeholder import substitute_placeholders, dict_templ
+from teuthology.util.time import TIMESTAMP_FMT
 
 log = logging.getLogger(__name__)
 
@@ -43,7 +44,7 @@ class Run(object):
         self.args = args
         # We assume timestamp is a datetime.datetime object
         self.timestamp = self.args.timestamp or \
-            datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
+            datetime.datetime.now().strftime(TIMESTAMP_FMT)
         self.user = self.args.user or pwd.getpwuid(os.getuid()).pw_name
 
         self.name = self.make_run_name()
index 1dc23e20d71a29ce6d512c05034b067d4e1a35f6..abe78a38630783fa946e36821e46f973fbe25c29 100644 (file)
@@ -12,6 +12,7 @@ from io import BytesIO
 from teuthology.config import config, YamlConfig
 from teuthology.exceptions import ScheduleFailError
 from teuthology.suite import run
+from teuthology.util.time import TIMESTAMP_FMT
 
 
 class TestRun(object):
@@ -52,7 +53,7 @@ class TestRun(object):
 
     @patch('teuthology.suite.run.util.fetch_repos')
     def test_name(self, m_fetch_repos):
-        stamp = datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
+        stamp = datetime.now().strftime(TIMESTAMP_FMT)
         with patch.object(run.Run, 'create_initial_config',
                           return_value=run.JobConfig()):
             name = run.Run(self.args).name
index 33958506b9669746848efd7294a1e131ead7a020..c37b5f2f53b72122339ba9a2aad38044e904503a 100644 (file)
@@ -1,11 +1,35 @@
 import pytest
 
-from datetime import timedelta
+from datetime import datetime, timedelta, timezone
 from typing import Type
 
 from teuthology.util import time
 
 
+@pytest.mark.parametrize(
+    ["timestamp", "result"],
+    [
+        ["1999-12-31_23:59:59", datetime(1999, 12, 31, 23, 59, 59, tzinfo=timezone.utc)],
+        ["1999-12-31_23:59", datetime(1999, 12, 31, 23, 59, 0, tzinfo=timezone.utc)],
+        ["1999-12-31T23:59:59", datetime(1999, 12, 31, 23, 59, 59, tzinfo=timezone.utc)],
+        ["1999-12-31T23:59:59+00:00", datetime(1999, 12, 31, 23, 59, 59, tzinfo=timezone.utc)],
+        ["1999-12-31T17:59:59-06:00", datetime(1999, 12, 31, 23, 59, 59, tzinfo=timezone.utc)],
+        ["2024-01-01", datetime(2024, 1, 1, 0, 0, tzinfo=timezone.utc)],
+        ["tomorrow", ValueError],
+        ["1d", ValueError],
+        ["", ValueError],
+        ["2024", ValueError],
+
+    ]
+)
+def test_parse_timestamp(timestamp: str, result: datetime | Type[Exception]):
+    if isinstance(result, datetime):
+        assert time.parse_timestamp(timestamp) == result
+    else:
+        with pytest.raises(result):
+            time.parse_timestamp(timestamp)
+
+
 @pytest.mark.parametrize(
     ["offset", "result"],
     [
index 4fbafe84d671841a3f48a549b09f78fe4b94f1c8..8e0525fcc3649f685cab6dcdc5d1b25fe040d175 100644 (file)
@@ -1,6 +1,24 @@
 import re
 
-from datetime import timedelta
+from datetime import datetime, timedelta, timezone
+
+# When we're not using ISO format, we're using this
+TIMESTAMP_FMT = "%Y-%m-%d_%H:%M:%S"
+
+def parse_timestamp(timestamp: str) -> datetime:
+    """
+    timestamp: A string either in ISO 8601 format or TIMESTAMP_FMT.
+               If no timezone is specified, UTC is assumed.
+
+    :returns: a datetime object
+    """
+    try:
+        dt = datetime.fromisoformat(timestamp)
+    except ValueError:
+        dt = datetime.strptime(timestamp, TIMESTAMP_FMT)
+    if dt.tzinfo is None:
+        dt = dt.replace(tzinfo=timezone.utc)
+    return dt
 
 def parse_offset(offset: str) -> timedelta:
     """