From ef18e64f77dbdd1e561e2321957baf9949764d5e Mon Sep 17 00:00:00 2001 From: Patrick McCarty Date: Mon, 29 May 2023 13:55:54 -0700 Subject: [PATCH] Treat default creation/mod dates as UTC (fixes #155) Without passing a tzinfo object to `datetime.now()`, a "naive" datetime object is created, which is not timezone-aware. To fix the default date/time detection for non-UTC local timezones, pass `datetime.timezone.utc` to convert the value to UTC and make the datetime object "aware". Also, adjust the strftime() wrappers to use the UTC offsets instead of a literal `Z`; using the literal `Z` at the end appears to be valid ISO-8601, but for some reason it does not successfully convert, whereas the `%z` placeholder substitutes the UTC offset and successfully converts. --- src/img2pdf.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/img2pdf.py b/src/img2pdf.py index 47ed960..4a1cd37 100755 --- a/src/img2pdf.py +++ b/src/img2pdf.py @@ -37,6 +37,7 @@ if hasattr(GifImagePlugin, "LoadingStrategy"): # TiffImagePlugin.DEBUG = True from PIL.ExifTags import TAGS from datetime import datetime +from datetime import timezone from jp2 import parsejp2 from enum import Enum from io import BytesIO @@ -721,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.strftime("%Y%m%d%H%M%S%z") for k in ["Title", "Author", "Creator", "Producer", "Subject"]: v = locals()[k.lower()] @@ -731,7 +732,7 @@ class pdfdoc(object): v = PdfString.encode(v) self.writer.docinfo[getattr(PdfName, k)] = v - now = datetime.now() + now = datetime.now(tz=timezone.utc) for k in ["CreationDate", "ModDate"]: v = locals()[k.lower()] if v is None and nodate: @@ -751,7 +752,7 @@ class pdfdoc(object): ) def datetime_to_xmpdate(dt): - return dt.strftime("%Y-%m-%dT%H:%M:%SZ") + return dt.strftime("%Y-%m-%dT%H:%M:%S%z") self.xmp = b"""