Skip to content

Commit 4d55756

Browse files
authored
Merge pull request #122 from timvink/utctimestamp
Support Utctimestamp in python 3.12
2 parents 6150fad + 4bf713a commit 4d55756

File tree

11 files changed

+148
-305
lines changed

11 files changed

+148
-305
lines changed

.github/workflows/pythonpublish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ jobs:
88
deploy:
99
runs-on: ubuntu-latest
1010
steps:
11-
- uses: actions/checkout@v2
11+
- uses: actions/checkout@v4
1212
with:
1313
fetch-depth: '0'
1414
- name: Set up Python
15-
uses: actions/setup-python@v2
15+
uses: actions/setup-python@v4
1616
with:
1717
python-version: '3.x'
1818
- name: Install dependencies

.github/workflows/scheduled_unittests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- name: Setup Python
2121
uses: actions/setup-python@master
2222
with:
23-
python-version: 3.9
23+
python-version: 3.10
2424

2525
- name: Install dependencies
2626
run: |

.github/workflows/unittests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ jobs:
55
name: Run unit tests with codecov upload
66
runs-on: ${{ matrix.os }}
77
env:
8-
USING_COVERAGE: '3.7'
8+
USING_COVERAGE: '3.10'
99
strategy:
1010
matrix:
1111
os: [ubuntu-latest, macos-latest, windows-latest]
12-
python-version: [3.7, 3.8, 3.9, "3.10"]
12+
python-version: [3.8, 3.9, "3.10", "3.11", "3.12"]
1313
steps:
1414
- uses: actions/checkout@master
1515

.github/workflows/unittests_codecov.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ jobs:
1313
name: Run unit tests with codecov upload
1414
runs-on: ${{ matrix.os }}
1515
env:
16-
USING_COVERAGE: '3.7'
16+
USING_COVERAGE: '3.10'
1717
strategy:
1818
matrix:
1919
os: [ubuntu-latest, macos-latest, windows-latest]
20-
python-version: [3.7, 3.8, 3.9, "3.10"]
20+
python-version: [3.8, 3.9, "3.10", "3.11", "3.12"]
2121
steps:
2222
- uses: actions/checkout@master
2323

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
from babel.dates import format_date, get_timezone
3+
4+
from datetime import datetime, timezone
5+
from typing import Any, Dict
6+
7+
8+
def get_date_formats(
9+
unix_timestamp: float,
10+
locale: str = "en",
11+
time_zone: str = "UTC",
12+
custom_format: str = "%d. %B %Y"
13+
) -> Dict[str, Any]:
14+
"""
15+
Calculate different date formats / types.
16+
17+
Args:
18+
unix_timestamp (float): A timestamp in seconds since 1970. Assumes UTC.
19+
locale (str): Locale code of language to use. Defaults to 'en'.
20+
time_zone (str): Timezone database name (https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).
21+
custom_format (str): strftime format specifier for the 'custom' type
22+
23+
Returns:
24+
dict: Different date formats.
25+
"""
26+
assert time_zone is not None
27+
assert locale is not None
28+
29+
utc_revision_date = datetime.fromtimestamp(int(unix_timestamp), tz=timezone.utc)
30+
loc_revision_date = utc_revision_date.replace(
31+
tzinfo=get_timezone("UTC")
32+
).astimezone(get_timezone(time_zone))
33+
34+
return {
35+
"date": format_date(loc_revision_date, format="long", locale=locale),
36+
"datetime": " ".join(
37+
[
38+
format_date(loc_revision_date, format="long", locale=locale),
39+
loc_revision_date.strftime("%H:%M:%S"),
40+
]
41+
),
42+
"iso_date": loc_revision_date.strftime("%Y-%m-%d"),
43+
"iso_datetime": loc_revision_date.strftime("%Y-%m-%d %H:%M:%S"),
44+
"timeago": '<span class="timeago" datetime="%s" locale="%s"></span>' % (loc_revision_date.isoformat(), locale),
45+
"custom": loc_revision_date.strftime(custom_format),
46+
}
47+

mkdocs_git_revision_date_localized_plugin/util.py

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
import logging
33
import os
44
import time
5-
from datetime import datetime
65

76
from mkdocs_git_revision_date_localized_plugin.ci import raise_ci_warnings
7+
from mkdocs_git_revision_date_localized_plugin.dates import get_date_formats
8+
89

9-
from babel.dates import format_date, get_timezone
1010
from git import (
1111
Repo,
1212
Git,
@@ -16,7 +16,7 @@
1616
NoSuchPathError,
1717
)
1818

19-
from typing import Any, Dict
19+
from typing import Dict
2020

2121
logger = logging.getLogger("mkdocs.plugins")
2222

@@ -44,43 +44,6 @@ def _get_repo(self, path: str) -> Git:
4444

4545
return self.repo_cache[path]
4646

47-
@staticmethod
48-
def _date_formats(
49-
unix_timestamp: float, locale: str = "en", time_zone: str = "UTC", custom_format: str = "%d. %B %Y"
50-
) -> Dict[str, Any]:
51-
"""
52-
Calculate different date formats / types.
53-
54-
Args:
55-
unix_timestamp (float): A timestamp in seconds since 1970.
56-
locale (str): Locale code of language to use. Defaults to 'en'.
57-
time_zone (str): Timezone database name (https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).
58-
custom_format (str): strftime format specifier for the 'custom' type
59-
60-
Returns:
61-
dict: Different date formats.
62-
"""
63-
assert time_zone is not None
64-
assert locale is not None
65-
66-
utc_revision_date = datetime.utcfromtimestamp(int(unix_timestamp))
67-
loc_revision_date = utc_revision_date.replace(
68-
tzinfo=get_timezone("UTC")
69-
).astimezone(get_timezone(time_zone))
70-
71-
return {
72-
"date": format_date(loc_revision_date, format="long", locale=locale),
73-
"datetime": " ".join(
74-
[
75-
format_date(loc_revision_date, format="long", locale=locale),
76-
loc_revision_date.strftime("%H:%M:%S"),
77-
]
78-
),
79-
"iso_date": loc_revision_date.strftime("%Y-%m-%d"),
80-
"iso_datetime": loc_revision_date.strftime("%Y-%m-%d %H:%M:%S"),
81-
"timeago": '<span class="timeago" datetime="%s" locale="%s"></span>' % (loc_revision_date.isoformat(), locale),
82-
"custom": loc_revision_date.strftime(custom_format),
83-
}
8447

8548
def get_git_commit_timestamp(
8649
self,
@@ -201,7 +164,7 @@ def get_date_formats_for_timestamp(
201164
Returns:
202165
dict: Localized date variants.
203166
"""
204-
date_formats = self._date_formats(
167+
date_formats = get_date_formats(
205168
unix_timestamp=commit_timestamp,
206169
time_zone=self.config.get("timezone"),
207170
locale=locale,

tests/fixtures/i18n/mkdocs.yml

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,13 @@ markdown_extensions:
2222
plugins:
2323
- search
2424
- i18n:
25-
default_language: !ENV [DEFAULT_LANGUAGE, "en"]
26-
default_language_only: !ENV [DEFAULT_LANGUAGE_ONLY, false]
2725
languages:
28-
default:
29-
name: Default (en)
30-
build: true
31-
en:
26+
- locale: en
3227
name: English
3328
build: true
34-
site_name: "MkDocs static i18n plugin demo (en)"
35-
fr:
29+
default: true
30+
- locale: fr
3631
name: Français
3732
build: true
38-
site_name: "Démo du plugin MkDocs static i18n (fr)"
39-
nav_translations:
40-
fr:
41-
Topic1: Sujet1
42-
Topic2: Sujet2
33+
4334
- git-revision-date-localized

tests/fixtures/i18n/mkdocs_wrong_order.yml

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,13 @@ plugins:
2727
- search
2828
- git-revision-date-localized
2929
- i18n:
30-
default_language: !ENV [DEFAULT_LANGUAGE, "en"]
31-
default_language_only: !ENV [DEFAULT_LANGUAGE_ONLY, false]
3230
languages:
33-
default:
34-
name: Default (en)
35-
build: true
36-
en:
31+
- locale: en
3732
name: English
3833
build: true
39-
site_name: "MkDocs static i18n plugin demo (en)"
40-
fr:
34+
default: true
35+
- locale: fr
4136
name: Français
4237
build: true
43-
site_name: "Démo du plugin MkDocs static i18n (fr)"
44-
nav_translations:
45-
fr:
46-
Topic1: Sujet1
47-
Topic2: Sujet2
38+
39+

tests/test_builds.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
# package module
3131
from mkdocs_git_revision_date_localized_plugin.util import Util
3232
from mkdocs_git_revision_date_localized_plugin.ci import commit_count
33+
from mkdocs_git_revision_date_localized_plugin.dates import get_date_formats
3334

3435
# ##################################
3536
# ######## Globals #################
@@ -147,6 +148,7 @@ def setup_commit_history(testproject_path):
147148
testproject_path = str(testproject_path)
148149

149150
repo = git.Repo.init(testproject_path, bare=False)
151+
repo.git.checkout("-b", "master")
150152
author = "Test Person <[email protected]>"
151153

152154

@@ -342,18 +344,6 @@ def validate_mkdocs_file(temp_path: str, mkdocs_yml_file: str):
342344
# ##################################
343345

344346

345-
def test_date_formats():
346-
u = Util()
347-
assert u._date_formats(1582397529) == {
348-
"date": "February 22, 2020",
349-
"datetime": "February 22, 2020 18:52:09",
350-
"iso_date": "2020-02-22",
351-
"iso_datetime": "2020-02-22 18:52:09",
352-
"timeago": '<span class="timeago" datetime="2020-02-22T18:52:09+00:00" locale="en"></span>',
353-
"custom": '22. February 2020',
354-
}
355-
356-
357347
@pytest.mark.parametrize("mkdocs_file", MKDOCS_FILES, ids=lambda x: f"mkdocs file: {x}")
358348
def test_tags_are_replaced(tmp_path, mkdocs_file):
359349
"""
@@ -390,7 +380,7 @@ def test_tags_are_replaced(tmp_path, mkdocs_file):
390380

391381
# the revision date was in 'setup_commit_history' was set to 1642911026 (Sun Jan 23 2022 04:10:26 GMT+0000)
392382
# Assert {{ git_revision_date_localized }} is replaced
393-
date_formats_revision_date = Util()._date_formats(1642911026,
383+
date_formats_revision_date = get_date_formats(1642911026,
394384
locale=plugin_config.get("locale"),
395385
time_zone=plugin_config.get("timezone"),
396386
custom_format=plugin_config.get("custom_format")
@@ -403,7 +393,7 @@ def test_tags_are_replaced(tmp_path, mkdocs_file):
403393

404394
# The last site revision was set in setup_commit_history to 1643911026 (Thu Feb 03 2022 17:57:06 GMT+0000)
405395
# Assert {{ git_site_revision_date_localized }} is replaced
406-
date_formats_revision_date = Util()._date_formats(1643911026,
396+
date_formats_revision_date = get_date_formats(1643911026,
407397
locale=plugin_config.get("locale"),
408398
time_zone=plugin_config.get("timezone"),
409399
custom_format=plugin_config.get("custom_format")
@@ -416,7 +406,7 @@ def test_tags_are_replaced(tmp_path, mkdocs_file):
416406
# Note {{ git_creation_date_localized }} is only replaced when configured in the config
417407
if plugin_config.get("enable_creation_date"):
418408
# The creation of page_with_tag.md was set in setup_commit_history to 1500854705 ( Mon Jul 24 2017 00:05:05 GMT+0000 )
419-
date_formats_revision_date = Util()._date_formats(1500854705,
409+
date_formats_revision_date = get_date_formats(1500854705,
420410
locale=plugin_config.get("locale"),
421411
time_zone=plugin_config.get("timezone"),
422412
custom_format=plugin_config.get("custom_format")
@@ -628,6 +618,10 @@ def test_low_fetch_depth(tmp_path, caplog):
628618

629619
# Clone the local repo with fetch depth of 1
630620
repo = git.Repo.init(cloned_folder, bare=False)
621+
try:
622+
repo.heads.main.rename("master", force=True)
623+
except:
624+
pass
631625
origin = repo.create_remote("origin", str(testproject_path))
632626
origin.fetch(depth=1, prune=True)
633627
repo.create_head(

tests/test_dates.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import pytest
2+
from datetime import datetime, timezone
3+
from babel.dates import get_timezone
4+
5+
from mkdocs_git_revision_date_localized_plugin.dates import get_date_formats
6+
7+
8+
9+
def test_get_dates():
10+
# Test with default arguments
11+
expected_output = {
12+
"date": "January 1, 1970",
13+
"datetime": "January 1, 1970 00:00:00",
14+
"iso_date": "1970-01-01",
15+
"iso_datetime": "1970-01-01 00:00:00",
16+
"timeago": '<span class="timeago" datetime="1970-01-01T00:00:00+00:00" locale="en"></span>',
17+
"custom": "01. January 1970"
18+
}
19+
assert get_date_formats(0) == expected_output
20+
21+
# Test with custom arguments
22+
expected_output = {
23+
"date": "January 1, 1970",
24+
"datetime": "January 1, 1970 00:00:00",
25+
"iso_date": "1970-01-01",
26+
"iso_datetime": "1970-01-01 00:00:00",
27+
"timeago": '<span class="timeago" datetime="1970-01-01T00:00:00+00:00" locale="en"></span>',
28+
"custom": "01. Jan 1970"
29+
}
30+
assert get_date_formats(0, locale="en", time_zone="UTC", custom_format="%d. %b %Y") == expected_output
31+
32+
# Test with non-UTC timezone
33+
expected_output = {
34+
"date": "January 1, 1970",
35+
"datetime": "January 1, 1970 02:00:00",
36+
"iso_date": "1970-01-01",
37+
"iso_datetime": "1970-01-01 02:00:00",
38+
"timeago": '<span class="timeago" datetime="1970-01-01T02:00:00+01:00" locale="en"></span>',
39+
"custom": "01. January 1970"
40+
}
41+
loc_dt = datetime(1970, 1, 1, 1, 0, 0, tzinfo=get_timezone("Europe/Berlin"))
42+
unix_timestamp = loc_dt.replace(tzinfo=timezone.utc).timestamp()
43+
assert get_date_formats(unix_timestamp, time_zone="Europe/Berlin") == expected_output
44+
45+
# Test with missing arguments
46+
with pytest.raises(TypeError):
47+
get_date_formats() # noqa
48+
49+
# Test with invalid timezone
50+
with pytest.raises(LookupError):
51+
get_date_formats(0, time_zone="Invalid/Timezone")
52+
53+
# Test with more recent date
54+
expected_output = {
55+
'date': 'October 15, 2023',
56+
'datetime': 'October 15, 2023 13:32:04',
57+
'iso_date': '2023-10-15',
58+
'iso_datetime': '2023-10-15 13:32:04',
59+
'timeago': '<span class="timeago" datetime="2023-10-15T13:32:04+02:00" locale="en"></span>',
60+
'custom': '15. October 2023'
61+
}
62+
assert get_date_formats(1697369524, time_zone="Europe/Amsterdam") == expected_output
63+
64+
65+
assert get_date_formats(1582397529) == {
66+
"date": "February 22, 2020",
67+
"datetime": "February 22, 2020 18:52:09",
68+
"iso_date": "2020-02-22",
69+
"iso_datetime": "2020-02-22 18:52:09",
70+
"timeago": '<span class="timeago" datetime="2020-02-22T18:52:09+00:00" locale="en"></span>',
71+
"custom": '22. February 2020',
72+
}

0 commit comments

Comments
 (0)