Skip to content

Use source date epoch - #14770

Open
Darlokt wants to merge 9 commits into
sphinx-doc:masterfrom
Darlokt:use_SOURCE_DATE_EPOCH
Open

Darlokt wants to merge 9 commits into
sphinx-doc:masterfrom
Darlokt:use_SOURCE_DATE_EPOCH

Conversation

@Darlokt

@Darlokt Darlokt commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

Hej,

this extends Sphinx’s SOURCE_DATE_EPOCH support to the Docutils date directive on older Docutils releases (#14769) and makes copyright %Y placeholders use the epoch directly, as proposed in #13526
The Docutils 1.0 development version already includes support for the date directive. With older versions, Sphinx’s |today| can use the supplied timestamp while the date directive still uses the system clock. The compatibility directive now uses the timestamp in UTC. Docutils 1.0 keeps its own implementation, and extensions can still replace the directive.

There are also a few changes to Sphinx’s existing support:

  • Timestamp parsing is shared between consumers. Epoch zero is handled consistently, and values must use ASCII integer syntax, following the SOURCE_DATE_EPOCH specification. The accepted syntax is -?[0-9]+; unset and empty values are treated as absent.
  • Gettext calculates its timestamp when the builder is initialized. Previously, the timestamp was fixed when the module was imported.
  • Copyright %Y placeholders use the epoch’s UTC year, capped at the current local year. Substitution works regardless of surrounding punctuation, including strings such as 2000–%Y, Alice. Static copyright correction keeps its existing pattern restrictions.

Setting, changing, or unsetting the epoch now invalidates cached doctrees. When a loaded environment detects an epoch change, a read-affecting configuration change, or an extension-list change, selected-file builds take the full-build path. Previously, they could save updated doctrees but leave other HTML files with the old dates. If none of the requested filenames are valid, the existing warnings are retained and the pending rebuild is left intact.

The undocumented module-level sphinx.builders.gettext.timestamp and ctime values are removed instead of deprecated, because they were computed once at import time. GitHub code search found no external users.

Tests cover the directive, copyright substitution, input validation, incremental builds, serial and parallel builds, rendered output, and recovery after a failed build.

References

AI Disclosure

OpenAI Codex assisted with reviewing the implementation, analyzing compatibility with released and development Docutils versions and previous issue/discussion context. All AI-assisted work was reviewed and understood by me.

Add a shared parser for SOURCE_DATE_EPOCH and use it across Sphinx timestamp consumers.
Backport support to the Docutils date directive before Docutils 1.0, preserve epoch zero, reject invalid values, and calculate gettext timestamps during builder initialization.
Persist the normalized epoch in the build environment and reread all documents when its effective value changes between incremental builds.

Document the incremental-build behavior and reproducibility limitations.
Require ASCII integer syntax as specified by the SOURCE_DATE_EPOCH standard, with a translatable validation error.

Use the epoch's UTC year directly for copyright %Y placeholders, capped at the current local year, while preserving legacy correction.

Rebuild all output when global read inputs change, even when filenames are specified. Preserve pending invalidation for invalid selections.

Add regression coverage for rendered output, parallel builds, recovery, copyright substitution, and directive overrides.
Older Docutils releases generate configured footer dates from the system clock, leaving clock-based dates in cached doctrees and XML output.

Adapt the Decorations transform before Docutils 1.0 using the shared epoch parser and UTC conversion.
Preserve footer formatting, literal percent signs, source links, and generator credits. Restore the configured format after generation and retain Docutils 1.0's native implementation.

Add regression coverage for formatting, boundary values, failure recovery, and serial and parallel incremental XML builds. Update documentation and changelog references.
@Darlokt

Darlokt commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

The follow-up in cb6ad54 also covers Docutils datestamp setting. When enabled in docutils.conf, older Docutils releases still generated the footer date from the system clock. That date remained in cached doctrees and appeared in XML and pseudo-XML output, even though the date substitutions used SOURCE_DATE_EPOCH.
The compatibility transform now uses the shared epoch parser and supplies the date in UTC. Docutils still constructs the footer, preserving custom formatting, literal percent signs, source links, and generator credits. The original format setting is restored afterward, including on failure. Unset and empty epochs retain the current-time behavior, and disabled datestamps stay disabled. Docutils 1.0 keeps its native implementation as with the other changes.

@AA-Turner

Copy link
Copy Markdown
Member

this is a 1000 line PR, i can't reasonably review this

@Darlokt

Darlokt commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

Sure, should I restructure the commits, most of it is docs changes/ test additions, so split the commits up semantically to make it easier to read?

@Darlokt

Darlokt commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor Author

The core of this is mostly around the shared parser in _get_source_date_epoch sphinx/util/_timestamps.py and implementing it in all the downstream consumers, so they all share the shared parser.

The second part is the %Y substitution in sphinx/config.py.

The third the two adapters in sphinx/directives/patches.py and sphinx/transforms/init.py to cover the date directive and configured footer timestamps.

Then around that some invalidation changes, so the cache gets properly invalidated and moving some timestamp calculations around to make it reproducible/stable (Gettext now calculates it no longer on import of the module, but when the builder initializes, so separate builds in the same process can use different SOURCE_DATE_EPOCH values. Input parsing, consumers now share one parser. 0 is valid, while unset and empty mean “use the current time” and fractions, white space, plus signs, and other invalid syntax are rejected compared to previously where different consumers accepted different inputs.)

Those are roughly the main code changes, the rest are Doc updates to specify this behavior clearly as there seemed to be some issues previously opened about whether something is real or false behavior, and test additions to codify the 0 handling changes and test proper cache invalidation, compatibility across versions etc.

What would be the preferred way to make this PR reviewable for you?

@Darlokt

Darlokt commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

@AA-Turner , I split up the PR into 3 more reviewable parts, they are under 1. #14776, 2. #14777 and 3. #14778.

@Darlokt

Darlokt commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

In the later ones, as they depend on the first etc. there are in the References direct diffs between them. Most of the lines are docs/test additions, the actual code changes are little.

This branch has not been deployed

No deployments
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.

Make Docutils date substitutions honor SOURCE_DATE_EPOCH across supported versions

2 participants