Skip to content

Commit

Permalink
BUG: TypeError in xmp._converter_date (py-pdf#813)
Browse files Browse the repository at this point in the history
Fix: Convert decimal to int before passing it to datetime

Closes py-pdf#774
  • Loading branch information
MartinThoma authored and VictorCarlquist committed Apr 29, 2022
1 parent 68d424a commit 4f448b7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions PyPDF2/xmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ def _converter_date(value):
second = decimal.Decimal(m.group("second") or "0")
seconds = second.to_integral(decimal.ROUND_FLOOR)
milliseconds = (second - seconds) * 1000000

seconds = int(seconds)
milliseconds = int(milliseconds)

tzd = m.group("tzd") or "Z"
dt = datetime.datetime(year, month, day, hour, minute, seconds, milliseconds)
if tzd != "Z":
Expand Down
6 changes: 6 additions & 0 deletions Tests/test_xmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ def get_all_tiff(xmp):
contents.append(content.data)
data[tag.tagName] = contents
return data


def test_regression_issue774():
cls = PyPDF2.xmp.XmpInformation
date = cls._converter_date("2021-04-28T12:23:34.123Z")
assert date.year == 2021

0 comments on commit 4f448b7

Please sign in to comment.