Skip to content

Fixed creation date loss on rename, added timezone option - #33

Open
liWanr wants to merge 2 commits into
jaywhj:mainfrom
liWanr:fix/date-rename-and-timezone
Open

Fixed creation date loss on rename, added timezone option#33
liWanr wants to merge 2 commits into
jaywhj:mainfrom
liWanr:fix/date-rename-and-timezone

Conversation

@liWanr

@liWanr liWanr commented Aug 4, 2026

Copy link
Copy Markdown

Both changes are about dates rendering incorrectly. I found them in the same investigation, so they're split into two independent commits.

  1. Creation date lost after a rename or move

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.

  1. Added a timezone option

_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

  • When a file is renamed and substantially rewritten in the same commit, git's similarity detection no longer pairs the two and the date is still lost
  • The timezone fix changes existing output: commits whose UTC time falls in the local early-morning window shift forward by a day (correctly) — worth noting in the changelog on release

liWanr added 2 commits August 5, 2026 00:42
- 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
Copilot AI lite review requested due to automatic review settings August 4, 2026 19:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 timezone plugin option and apply it when rendering dates (including recently-updated lists and <time datetime> attributes).
  • Document the new timezone configuration 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants