Skip to content

[Bug]: PDF and PGF CreationDate/ModDate carry the wrong UTC offset #32630

In review

Description

@sohailshk

Bug summary

PDF and PGF files generated by Matplotlib can contain incorrect UTC offsets in their CreationDate and ModDate metadata when no explicit dates are provided.

The issue occurs in _datetime_to_pdf in lib/matplotlib/backends/backend_pdf.py, which uses a naive datetime.today().

There are three problems:

  • Naive datetimes use time.altzone or time.timezone, causing daylight-saving offsets to be applied incorrectly throughout the year.
  • The minutes field is calculated from seconds incorrectly.
  • Timezone-aware datetimes have their UTC offset sign reversed.

For example, on the current main branch and Matplotlib 3.11.2:

  • India: +05'1800' instead of +05'30'.
  • New York in winter: -04'00' instead of -05'00'.
  • Amsterdam in winter: +02'00' instead of +01'00'.

Code for reproduction

import io, re, time
import matplotlib
matplotlib.use("pdf")
import matplotlib.pyplot as plt

buf = io.BytesIO()
plt.plot([1])
plt.savefig(buf, format="pdf")
print("matplotlib", matplotlib.__version__, "| TZ:", time.tzname)
print(re.search(rb"/CreationDate \(([^)]*)\)", buf.getvalue()).group(1).decode())
TZ=Asia/Kolkata python pdf_date_repro.py
TZ=America/New_York python pdf_date_repro.py

Actual outcome

The generated PDF and PGF metadata contain incorrect timezone offsets. The problem affects both CreationDate and ModDate.

matplotlib 3.11.2 | TZ: ('IST', 'IST')
D:20261121104550+05'1800'
matplotlib 3.11.2 | TZ: ('EST', 'EDT')
D:20261121001551-04'00'

Expected outcome

Offsets should follow the PDF date format:

D:YYYYMMDDHHmmSSOHH'mm'

Here, O is the sign of the local UTC offset.

The generated metadata should contain the correct local UTC offset, including the correct daylight-saving adjustment and minutes.

SOURCE_DATE_EPOCH output is not affected and continues to use UTC (Z).

Operating system

Confirmed with the reproduction on the current main branch. The issue is timezone-dependent and affects systems using non-UTC timezones.

Matplotlib Version

  • Current main
  • Matplotlib 3.11.2

Additional information

No existing open issue reporting this problem was found.

The issue was initially identified through AI-assisted testing of date handling and confirmed by running the reproduction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions