generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 28
epoch_seconds_to_datetime utility function #132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,13 +1,31 @@ | ||
| # Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"). You | ||
| # may not use this file except in compliance with the License. A copy of | ||
| # the License is located at | ||
| # | ||
| # http://aws.amazon.com/apache2.0/ | ||
| # | ||
| # or in the "license" file accompanying this file. This file is | ||
| # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
| # ANY KIND, either express or implied. See the License for the specific | ||
| # language governing permissions and limitations under the License. | ||
|
|
||
| # mypy: allow-untyped-defs | ||
| # mypy: allow-incomplete-defs | ||
|
Comment on lines
+14
to
+15
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To date, this is the best solution we have for test files that include pytest fixtures. |
||
|
|
||
| from datetime import datetime, timedelta, timezone | ||
| from decimal import Decimal | ||
| from math import isnan | ||
| from typing import Any | ||
| from typing import Any, NamedTuple | ||
| from unittest.mock import Mock | ||
|
|
||
| import pytest | ||
|
|
||
| from smithy_python.exceptions import ExpectationNotMetException | ||
| from smithy_python.utils import ( | ||
| ensure_utc, | ||
| epoch_seconds_to_datetime, | ||
| expect_type, | ||
| limited_parse_float, | ||
| serialize_epoch_seconds, | ||
|
|
@@ -181,29 +199,71 @@ def test_serialize_float(given: float | Decimal, expected: str) -> None: | |
| assert serialize_float(given) == expected | ||
|
|
||
|
|
||
| class DateTimeTestcase(NamedTuple): | ||
| dt_object: datetime | ||
| rfc3339_str: str | ||
| epoch_seconds_num: int | float | ||
| epoch_seconds_str: str | ||
|
|
||
|
|
||
| DATETIME_TEST_CASES: list[DateTimeTestcase] = [ | ||
| DateTimeTestcase( | ||
| dt_object=datetime(2017, 1, 1, tzinfo=timezone.utc), | ||
| rfc3339_str="2017-01-01T00:00:00Z", | ||
| epoch_seconds_num=1483228800, | ||
| epoch_seconds_str="1483228800", | ||
| ), | ||
| DateTimeTestcase( | ||
| dt_object=datetime(2017, 1, 1, microsecond=1, tzinfo=timezone.utc), | ||
| rfc3339_str="2017-01-01T00:00:00.000001Z", | ||
| epoch_seconds_num=1483228800.000001, | ||
| epoch_seconds_str="1483228800.000001", | ||
| ), | ||
| DateTimeTestcase( | ||
| dt_object=datetime(1969, 12, 31, 23, 59, 59, tzinfo=timezone.utc), | ||
| rfc3339_str="1969-12-31T23:59:59Z", | ||
| epoch_seconds_num=-1, | ||
| epoch_seconds_str="-1", | ||
| ), | ||
| # The first second affected by the Year 2038 problem where fromtimestamp raises an | ||
| # OverflowError on 32-bit systems for dates beyond 2038-01-19 03:14:07 UTC. | ||
| DateTimeTestcase( | ||
| dt_object=datetime(2038, 1, 19, 3, 14, 8, tzinfo=timezone.utc), | ||
| rfc3339_str="2038-01-19T03:14:08Z", | ||
| epoch_seconds_num=2147483648, | ||
| epoch_seconds_str="2147483648", | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "given, expected", | ||
| [ | ||
| (datetime(2017, 1, 1, tzinfo=timezone.utc), "2017-01-01T00:00:00Z"), | ||
| ( | ||
| datetime(2017, 1, 1, microsecond=1, tzinfo=timezone.utc), | ||
| "2017-01-01T00:00:00.000001Z", | ||
| ), | ||
| ], | ||
| [(case.dt_object, case.rfc3339_str) for case in DATETIME_TEST_CASES], | ||
| ) | ||
| def test_serialize_rfc3339(given: datetime, expected: str) -> None: | ||
| assert serialize_rfc3339(given) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "given, expected", | ||
| [ | ||
| (datetime(2017, 1, 1, tzinfo=timezone.utc), "1483228800"), | ||
| ( | ||
| datetime(2017, 1, 1, microsecond=1, tzinfo=timezone.utc), | ||
| "1483228800.000001", | ||
| ), | ||
| ], | ||
| [(case.dt_object, case.epoch_seconds_str) for case in DATETIME_TEST_CASES], | ||
| ) | ||
| def test_serialize_epoch_seconds(given: datetime, expected: str) -> None: | ||
| assert serialize_epoch_seconds(given) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "given, expected", | ||
| [(case.epoch_seconds_num, case.dt_object) for case in DATETIME_TEST_CASES], | ||
| ) | ||
| def test_epoch_seconds_to_datetime(given: int | float, expected: datetime) -> None: | ||
| assert epoch_seconds_to_datetime(given) == expected | ||
|
|
||
|
|
||
| def test_epoch_seconds_to_datetime_with_overflow_error(monkeypatch): | ||
| # Emulate the Year 2038 problem by always raising an OverflowError. | ||
| datetime_mock = Mock(wraps=datetime) | ||
| datetime_mock.fromtimestamp = Mock(side_effect=OverflowError()) | ||
| monkeypatch.setattr("smithy_python.utils.datetime", datetime_mock) | ||
| dt_object = datetime(2038, 1, 19, 3, 14, 8, tzinfo=timezone.utc) | ||
| epoch_seconds_to_datetime(2147483648) == dt_object | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To preempt the usual comment: I know, it's 2023. This notice should have been added here in 2022 when the file was first created, so backdating it.