generated from canonical/starbase
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cli and platform utils (#599)
Signed-off-by: Dariusz Duda <[email protected]> Co-authored-by: Alex Lowe <[email protected]>
- Loading branch information
1 parent
44b1a94
commit 8e9893d
Showing
7 changed files
with
179 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# This file is part of craft-application. | ||
# | ||
# Copyright 2024 Canonical Ltd. | ||
# | ||
# This program is free software: you can redistribute it and/or modify it | ||
# under the terms of the GNU Lesser General Public License version 3, as | ||
# published by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, but WITHOUT | ||
# ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, | ||
# SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. | ||
# See the GNU Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
"""CLI-related utilities.""" | ||
|
||
import datetime | ||
import warnings | ||
|
||
|
||
def format_timestamp(dt: datetime.datetime) -> str: | ||
"""Convert a datetime object (with or without timezone) to a string. | ||
The format is an ISO 8601-compliant UTC date and time stamp formatted as: | ||
<DATE>T<TIME>Z | ||
Timezone-aware datetime objects are converted to UTC. Timezone-naive ones | ||
are assumed to be UTC. | ||
""" | ||
if dt.tzinfo is not None and dt.tzinfo.utcoffset(None) is not None: | ||
# timezone aware | ||
dtz = dt.astimezone(datetime.timezone.utc) | ||
else: | ||
# timezone naive, assume it's UTC | ||
dtz = dt | ||
warnings.warn( | ||
"Timezone-naive datetime used. Replace with a timezone-aware one if possible.", | ||
category=UserWarning, | ||
stacklevel=2, | ||
) | ||
return dtz.strftime("%Y-%m-%dT%H:%M:%SZ") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# This file is part of craft-application. | ||
# | ||
# Copyright 2024 Canonical Ltd. | ||
# | ||
# This program is free software: you can redistribute it and/or modify it | ||
# under the terms of the GNU Lesser General Public License version 3, as | ||
# published by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, but WITHOUT | ||
# ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, | ||
# SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. | ||
# See the GNU Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
from datetime import datetime, timedelta, timezone | ||
|
||
import pytest | ||
|
||
from craft_application.util import format_timestamp | ||
|
||
pytestmark = [ | ||
pytest.mark.filterwarnings( | ||
"ignore:Timezone-naive datetime used. Replace with a timezone-aware one if possible.", | ||
) | ||
] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("dt_obj", "expected"), | ||
[ | ||
( | ||
datetime(2024, 5, 23, 13, 24, 0), | ||
"2024-05-23T13:24:00Z", | ||
), | ||
( | ||
datetime(2024, 5, 23, 13, 24, 0, tzinfo=timezone.utc), | ||
"2024-05-23T13:24:00Z", | ||
), | ||
( | ||
datetime(2024, 5, 23, 13, 24, 0, tzinfo=timezone(timedelta(hours=-5))), | ||
"2024-05-23T18:24:00Z", | ||
), | ||
], | ||
) | ||
def test_timezone_parsing(dt_obj: datetime, expected: str) -> None: | ||
assert format_timestamp(dt=dt_obj) == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters