-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from timvink/utctimestamp
Support Utctimestamp in python 3.12
- Loading branch information
Showing
11 changed files
with
148 additions
and
305 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
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,47 @@ | ||
|
||
from babel.dates import format_date, get_timezone | ||
|
||
from datetime import datetime, timezone | ||
from typing import Any, Dict | ||
|
||
|
||
def get_date_formats( | ||
unix_timestamp: float, | ||
locale: str = "en", | ||
time_zone: str = "UTC", | ||
custom_format: str = "%d. %B %Y" | ||
) -> Dict[str, Any]: | ||
""" | ||
Calculate different date formats / types. | ||
Args: | ||
unix_timestamp (float): A timestamp in seconds since 1970. Assumes UTC. | ||
locale (str): Locale code of language to use. Defaults to 'en'. | ||
time_zone (str): Timezone database name (https://en.wikipedia.org/wiki/List_of_tz_database_time_zones). | ||
custom_format (str): strftime format specifier for the 'custom' type | ||
Returns: | ||
dict: Different date formats. | ||
""" | ||
assert time_zone is not None | ||
assert locale is not None | ||
|
||
utc_revision_date = datetime.fromtimestamp(int(unix_timestamp), tz=timezone.utc) | ||
loc_revision_date = utc_revision_date.replace( | ||
tzinfo=get_timezone("UTC") | ||
).astimezone(get_timezone(time_zone)) | ||
|
||
return { | ||
"date": format_date(loc_revision_date, format="long", locale=locale), | ||
"datetime": " ".join( | ||
[ | ||
format_date(loc_revision_date, format="long", locale=locale), | ||
loc_revision_date.strftime("%H:%M:%S"), | ||
] | ||
), | ||
"iso_date": loc_revision_date.strftime("%Y-%m-%d"), | ||
"iso_datetime": loc_revision_date.strftime("%Y-%m-%d %H:%M:%S"), | ||
"timeago": '<span class="timeago" datetime="%s" locale="%s"></span>' % (loc_revision_date.isoformat(), locale), | ||
"custom": loc_revision_date.strftime(custom_format), | ||
} | ||
|
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
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,72 @@ | ||
import pytest | ||
from datetime import datetime, timezone | ||
from babel.dates import get_timezone | ||
|
||
from mkdocs_git_revision_date_localized_plugin.dates import get_date_formats | ||
|
||
|
||
|
||
def test_get_dates(): | ||
# Test with default arguments | ||
expected_output = { | ||
"date": "January 1, 1970", | ||
"datetime": "January 1, 1970 00:00:00", | ||
"iso_date": "1970-01-01", | ||
"iso_datetime": "1970-01-01 00:00:00", | ||
"timeago": '<span class="timeago" datetime="1970-01-01T00:00:00+00:00" locale="en"></span>', | ||
"custom": "01. January 1970" | ||
} | ||
assert get_date_formats(0) == expected_output | ||
|
||
# Test with custom arguments | ||
expected_output = { | ||
"date": "January 1, 1970", | ||
"datetime": "January 1, 1970 00:00:00", | ||
"iso_date": "1970-01-01", | ||
"iso_datetime": "1970-01-01 00:00:00", | ||
"timeago": '<span class="timeago" datetime="1970-01-01T00:00:00+00:00" locale="en"></span>', | ||
"custom": "01. Jan 1970" | ||
} | ||
assert get_date_formats(0, locale="en", time_zone="UTC", custom_format="%d. %b %Y") == expected_output | ||
|
||
# Test with non-UTC timezone | ||
expected_output = { | ||
"date": "January 1, 1970", | ||
"datetime": "January 1, 1970 02:00:00", | ||
"iso_date": "1970-01-01", | ||
"iso_datetime": "1970-01-01 02:00:00", | ||
"timeago": '<span class="timeago" datetime="1970-01-01T02:00:00+01:00" locale="en"></span>', | ||
"custom": "01. January 1970" | ||
} | ||
loc_dt = datetime(1970, 1, 1, 1, 0, 0, tzinfo=get_timezone("Europe/Berlin")) | ||
unix_timestamp = loc_dt.replace(tzinfo=timezone.utc).timestamp() | ||
assert get_date_formats(unix_timestamp, time_zone="Europe/Berlin") == expected_output | ||
|
||
# Test with missing arguments | ||
with pytest.raises(TypeError): | ||
get_date_formats() # noqa | ||
|
||
# Test with invalid timezone | ||
with pytest.raises(LookupError): | ||
get_date_formats(0, time_zone="Invalid/Timezone") | ||
|
||
# Test with more recent date | ||
expected_output = { | ||
'date': 'October 15, 2023', | ||
'datetime': 'October 15, 2023 13:32:04', | ||
'iso_date': '2023-10-15', | ||
'iso_datetime': '2023-10-15 13:32:04', | ||
'timeago': '<span class="timeago" datetime="2023-10-15T13:32:04+02:00" locale="en"></span>', | ||
'custom': '15. October 2023' | ||
} | ||
assert get_date_formats(1697369524, time_zone="Europe/Amsterdam") == expected_output | ||
|
||
|
||
assert get_date_formats(1582397529) == { | ||
"date": "February 22, 2020", | ||
"datetime": "February 22, 2020 18:52:09", | ||
"iso_date": "2020-02-22", | ||
"iso_datetime": "2020-02-22 18:52:09", | ||
"timeago": '<span class="timeago" datetime="2020-02-22T18:52:09+00:00" locale="en"></span>', | ||
"custom": '22. February 2020', | ||
} |
Oops, something went wrong.