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__)
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()
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):
@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
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"],
[
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:
"""