Skip to main content

Merge pull request #13 from alexwlchan/now

ID
1902bc3
date
2025-12-22 08:30:54+00:00
author
Alex Chan <alex@alexwlchan.net>
parents
8d5fb3a, 291c7bc
message
Merge pull request #13 from alexwlchan/now

Add a method to return the current timestamp
changed files
4 files, 61 additions, 19 deletions

Changed files

CHANGELOG.md (2392) → CHANGELOG.md (2514)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3bdee38..42508ca 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
 # CHANGELOG
 
+## v21 - 2025-12-22
+
+Add a method `dates.now()` to return the current time in the timestamp used by all my static sites.
+
 ## v20 - 2025-12-10
 
 Use concurrency in `test_no_videos_are_av1`, which can make it faster for

src/chives/__init__.py (391) → src/chives/__init__.py (391)

diff --git a/src/chives/__init__.py b/src/chives/__init__.py
index 815e16a..70eb860 100644
--- a/src/chives/__init__.py
+++ b/src/chives/__init__.py
@@ -11,4 +11,4 @@ I share across multiple sites.
 
 """
 
-__version__ = "20"
+__version__ = "21"

src/chives/dates.py (2174) → src/chives/dates.py (2429)

diff --git a/src/chives/dates.py b/src/chives/dates.py
index 1fe4258..e93bc42 100644
--- a/src/chives/dates.py
+++ b/src/chives/dates.py
@@ -11,6 +11,18 @@ from datetime import datetime, timezone
 from typing import Any
 
 
+def now() -> str:
+    """
+    Returns the current time in the standard format used by my static sites.
+    """
+    return (
+        datetime.now(tz=timezone.utc)
+        .replace(microsecond=0)
+        .isoformat()
+        .replace("+00:00", "Z")
+    )
+
+
 def find_all_dates(json_value: Any) -> Iterator[tuple[dict[str, Any], str, str]]:
     """
     Find all the timestamps in a heavily nested JSON object.

tests/test_static_site_tests.py (6784) → tests/test_static_site_tests.py (7599)

diff --git a/tests/test_static_site_tests.py b/tests/test_static_site_tests.py
index c3c41d2..157ea9a 100644
--- a/tests/test_static_site_tests.py
+++ b/tests/test_static_site_tests.py
@@ -6,10 +6,11 @@ from collections.abc import Iterator
 from pathlib import Path
 import shutil
 import subprocess
-from typing import TypeVar
+from typing import Any, TypeVar
 
 import pytest
 
+from chives import dates
 from chives.static_site_tests import StaticSiteTestSuite
 
 
@@ -170,26 +171,51 @@ def test_checks_for_av1_videos(site_root: Path) -> None:
         t.test_no_videos_are_av1(site_root)
 
 
-def test_checks_for_date_formats(site_root: Path) -> None:
+class TestAllTimestampsAreConsistent:
     """
-    The tests check for inconsistent date formats.
+    Tests for the `test_all_timestamps_are_consistent` method.
     """
-    # Check a site with correct metadata
-    metadata1 = {"date_saved": "2025-12-06"}
-    t1 = create_test_suite(site_root, metadata1)
-    t1.test_all_timestamps_are_consistent(metadata1)
 
-    # Check a site with incorrect metadata
-    metadata2 = {"date_saved": "AAAA-BB-CC"}
-    t2 = create_test_suite(site_root, metadata2)
-    with pytest.raises(AssertionError):
-        t2.test_all_timestamps_are_consistent(metadata2)
-
-    # Check we can override the timestamp format
-    metadata3 = {"date_saved": "AAAA-BB-CC"}
-    t3 = create_test_suite(site_root, metadata=metadata3)
-    t3.date_formats.append("AAAA-BB-CC")
-    t3.test_all_timestamps_are_consistent(metadata3)
+    @pytest.mark.parametrize(
+        "metadata",
+        [
+            {"date_saved": "2025-12-06"},
+            {"date_saved": dates.now()},
+        ],
+    )
+    def test_allows_correct_date_formats(self, site_root: Path, metadata: Any) -> None:
+        """
+        The tests pass if all the dates are in the correct format.
+        """
+        t = create_test_suite(site_root, metadata)
+        t.test_all_timestamps_are_consistent(metadata)
+
+    @pytest.mark.parametrize("metadata", [{"date_saved": "AAAA-BB-CC"}])
+    def test_rejects_incorrect_date_formats(
+        self, site_root: Path, metadata: Any
+    ) -> None:
+        """
+        The tests fail if the metadata has inconsistent date formats.
+        """
+        t = create_test_suite(site_root, metadata)
+        with pytest.raises(AssertionError):
+            t.test_all_timestamps_are_consistent(metadata)
+
+    def test_can_override_date_formats(self, site_root: Path) -> None:
+        """
+        A previously-blocked date format is allowed if you add it to
+        the `date_formats` list.
+        """
+        metadata = {"date_saved": "2025"}
+        t = create_test_suite(site_root, metadata)
+
+        # It fails with the default settings
+        with pytest.raises(AssertionError):
+            t.test_all_timestamps_are_consistent(metadata)
+
+        # It passes if we add the format to `date_formats`
+        t.date_formats.append("%Y")
+        t.test_all_timestamps_are_consistent(metadata)
 
 
 def test_checks_for_similar_tags(site_root: Path) -> None: