Fixed creation date loss on rename, added timezone option - #33
Open
liWanr wants to merge 2 commits into
Open
Conversation
- The pre-commit hook now migrates the cached creation date from the old path to the new one, based on git's rename detection (-M), so both `git mv` and a manual `mv` + `git add` are covered - Only renames that stay inside the docs directory are migrated; a file moved in from outside has no creation date to inherit - Not detected when a file is renamed and largely rewritten in the same commit, since git's similarity detection no longer pairs the two
- Added the `timezone` option: git timestamps are resolved into calendar days in it, and Front Matter values without a timezone are interpreted as being in it, so CI and local builds stay consistent - Front Matter values with an explicit timezone are respected as-is - Fixed the displayed text being formatted in UTC while the `<time datetime>` attribute used the local timezone, which rendered dates one day off for commits whose UTC time fell on a different local day - Defaults to the build machine's local timezone, the previous behavior
There was a problem hiding this comment.
Pull request overview
This PR addresses two date-related correctness issues in the mkdocs-document-dates plugin: preserving cached creation dates across renames/moves, and ensuring date rendering is consistent with a configurable timezone.
Changes:
- Add rename/move detection in the pre-commit cache updater and migrate cached entries so creation dates aren’t reset on rename.
- Introduce a
timezoneplugin option and apply it when rendering dates (including recently-updated lists and<time datetime>attributes). - Document the new
timezoneconfiguration option in the JSON schema.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| mkdocs_document_dates/utils.py | Adds a timezone parameter for “recently updated” date formatting. |
| mkdocs_document_dates/plugin.py | Adds timezone config + applies it across rendering, meta parsing, and exported cached date values. |
| mkdocs_document_dates/cache_manager.py | Detects staged renames via git diff --cached -M and migrates cache keys before updating. |
| docs/schema.json | Documents the new timezone option and expected values. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+346
to
350
| # 没写时区就按配置的时区解释(即作者所在时区),再转 UTC; | ||
| # 显式写了时区的以它自己的为准 | ||
| if dt.tzinfo is None: | ||
| local_tz = datetime.now().astimezone().tzinfo | ||
| dt = dt.replace(tzinfo=local_tz) | ||
| dt = dt.replace(tzinfo=self.tz or datetime.now().astimezone().tzinfo) | ||
| return dt.astimezone(timezone.utc) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Both changes are about dates rendering incorrectly. I found them in the same investigation, so they're split into two independent commits.
git log doesn't detect renames, and the keys in .dates_cache.jsonl are paths — so after a rename the new path isn't in the cache, the file is treated as new, and its date becomes the moment of the rename.
Added get_renamed_files() and migrate_renamed_entries() in cache_manager.py, which move created from the old key to the new one in update_cache() before the main loop runs.
Not using --follow: it only works on a single file, and calling it per file would turn O(1) into O(n). This is one git diff --cached -M, and it runs only in the pre-commit hook, so build time is unaffected. It also diffs from the repository root — with --relative, git only sees inside the docs directory and will mispair a file moved out with a similar one moved in.
_formatting_date was formatting in UTC while the attribute used local time, so the two could disagree by a day.
The new timezone option resolves git timestamps into calendar days and interprets Front Matter values that carry no timezone. Left empty, it falls back to the build machine's local timezone — the previous behavior.
Limitations