From 81502f21af6503449098a9537ab96a42b40d5b69 Mon Sep 17 00:00:00 2001 From: Patrick McCarty Date: Sat, 10 Jun 2023 17:53:03 -0700 Subject: [PATCH] Convert creation/modification dates to UTC (fixes #155) Ensure that timezones are correctly interpreted in the input by calling `.astimezone()` as appropriate on datetime objects, and store the resulting date fields as UTC. One could argue that datetimes in the local timezone be stored in the PDF, but then the date string handling becomes more complicated; the PDF and XMP date specs both use the `Z` suffix to indicate UTC time, but other +/- offsets require different syntax between the two specs. --- src/img2pdf.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/img2pdf.py b/src/img2pdf.py index 35141d0..47ebd1e 100755 --- a/src/img2pdf.py +++ b/src/img2pdf.py @@ -36,7 +36,7 @@ if hasattr(GifImagePlugin, "LoadingStrategy"): # TiffImagePlugin.DEBUG = True from PIL.ExifTags import TAGS -from datetime import datetime +from datetime import datetime, timezone from jp2 import parsejp2 from enum import Enum from io import BytesIO @@ -722,7 +722,7 @@ class pdfdoc(object): self.writer.docinfo = PdfDict(indirect=True) def datetime_to_pdfdate(dt): - return dt.strftime("%Y%m%d%H%M%SZ") + return dt.astimezone(tz=timezone.utc).strftime("%Y%m%d%H%M%SZ") for k in ["Title", "Author", "Creator", "Producer", "Subject"]: v = locals()[k.lower()] @@ -732,7 +732,7 @@ class pdfdoc(object): v = PdfString.encode(v) self.writer.docinfo[getattr(PdfName, k)] = v - now = datetime.now() + now = datetime.now().astimezone() for k in ["CreationDate", "ModDate"]: v = locals()[k.lower()] if v is None and nodate: @@ -752,7 +752,7 @@ class pdfdoc(object): ) def datetime_to_xmpdate(dt): - return dt.strftime("%Y-%m-%dT%H:%M:%SZ") + return dt.astimezone(tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") self.xmp = b""" -- 2.39.5