Convert creation/modification dates to UTC (fixes #155) #168

Merged
josch merged 1 commit from phmccarty/img2pdf:utc-timestamps into main 2023-06-11 05:30:53 +00:00
Contributor

(Tested with Python 3.11.3 on Arch Linux.)

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.

(Tested with Python 3.11.3 on Arch Linux.) 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.
phmccarty added 1 commit 2023-05-29 21:20:09 +00:00
(Tested with Python 3.11.3 on Arch Linux.)

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 for
ISO 8601, but for some reason it does not successfully convert, whereas
the `%z` placeholder substitutes the UTC offset and successfully
converts.
Author
Contributor

After doing a bit more research, I discovered that PDF and XMP have different syntax for timezone offsets... I pushed an experimental change to another branch (atop this branch): https://gitlab.mister-muffin.de/phmccarty/img2pdf/commits/branch/pdf-xmp-timestamps

After doing a bit more research, I discovered that PDF and XMP have different syntax for timezone offsets... I pushed an experimental change to another branch (atop this branch): https://gitlab.mister-muffin.de/phmccarty/img2pdf/commits/branch/pdf-xmp-timestamps
phmccarty added 2 commits 2023-06-11 00:35:14 +00:00
In line with the previous commit for consistency, convert the date
strings from --creationdate and --moddate to UTC as well if a timezone
is specified.
The way to specify timezone offsets differs between the PDF and XMP
specs, so hardcode the UTC offset instead of using `%z`. This also
avoids the possibility of embedding the more more granular timezones
supported by the datetime module but that are unsupported by either PDF
or XMP formats.
Author
Contributor

Updated the MR to reflect the discussion from #155

Updated the MR to reflect the discussion from #155
phmccarty force-pushed utc-timestamps from 85efd9a80a to 512478ac4e 2023-06-11 00:37:01 +00:00 Compare
phmccarty changed title from Treat default creation/mod dates as UTC (fixes #155) to Convert creation/modification dates to UTC (fixes #155) 2023-06-11 00:43:23 +00:00
phmccarty force-pushed utc-timestamps from 512478ac4e to 81502f21af 2023-06-11 01:01:49 +00:00 Compare
Author
Contributor

One more update: I squashed changes into one commit to minimize the churn between commits.

One more update: I squashed changes into one commit to minimize the churn between commits.
josch merged commit 81502f21af into main 2023-06-11 05:30:53 +00:00
phmccarty deleted branch utc-timestamps 2023-06-11 07:14:48 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: josch/img2pdf#168
No description provided.