Skip to content

Commit

Permalink
BUG: Dates conversion not working with Z00'00' (#1946)
Browse files Browse the repository at this point in the history
Closes #1943
  • Loading branch information
pubpub-zz authored Jul 6, 2023
1 parent 29c79fc commit c239073
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
17 changes: 12 additions & 5 deletions pypdf/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,16 @@ def creation_date(self) -> Optional[datetime]:
text = self._get_text(DI.CREATION_DATE)
if text is None:
return None
return datetime.strptime(text.replace("'", ""), "D:%Y%m%d%H%M%S%z")
return datetime.strptime(
text.replace("Z", "+").replace("'", ""), "D:%Y%m%d%H%M%S%z"
)

@property
def creation_date_raw(self) -> Optional[str]:
"""
The "raw" version of creation date; can return a ``ByteStringObject``.
Typically in the format ``D:YYYYMMDDhhmmss[+-]hh'mm`` where the suffix
Typically in the format ``D:YYYYMMDDhhmmss[+Z-]hh'mm`` where the suffix
is the offset from UTC.
"""
return self.get(DI.CREATION_DATE)
Expand All @@ -264,15 +266,17 @@ def modification_date(self) -> Optional[datetime]:
text = self._get_text(DI.MOD_DATE)
if text is None:
return None
return datetime.strptime(text.replace("'", ""), "D:%Y%m%d%H%M%S%z")
return datetime.strptime(
text.replace("Z", "+").replace("'", ""), "D:%Y%m%d%H%M%S%z"
)

@property
def modification_date_raw(self) -> Optional[str]:
"""
The "raw" version of modification date; can return a
``ByteStringObject``.
Typically in the format ``D:YYYYMMDDhhmmss[+-]hh'mm`` where the suffix
Typically in the format ``D:YYYYMMDDhhmmss[+Z-]hh'mm`` where the suffix
is the offset from UTC.
"""
return self.get(DI.MOD_DATE)
Expand Down Expand Up @@ -644,7 +648,10 @@ def _build_field(
if s not in states:
states.append(s)
retval[key][NameObject("/_States_")] = ArrayObject(states)
if obj.get(FA.Ff, 0) & FA.FfBits.NoToggleToOff != 0 and "/Off" in retval[key]["/_States_"]:
if (
obj.get(FA.Ff, 0) & FA.FfBits.NoToggleToOff != 0
and "/Off" in retval[key]["/_States_"]
):
del retval[key]["/_States_"][retval[key]["/_States_"].index("/Off")]

def _check_kids(
Expand Down
17 changes: 17 additions & 0 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ def test_read_metadata(pdf_path, expected):
assert metadict["/Title"] == docinfo.title


def test_iss1943():
reader = PdfReader(RESOURCE_ROOT / "crazyones.pdf")
docinfo = reader.metadata
docinfo.update(
{
NameObject("/CreationDate"): TextStringObject("D:20230705005151Z00'00'"),
NameObject("/ModDate"): TextStringObject("D:20230705005151Z00'00'"),
}
)
docinfo.creation_date
docinfo.creation_date_raw
docinfo.modification_date
docinfo.modification_date_raw
docinfo.update({NameObject("/CreationDate"): NumberObject(1)})
assert docinfo.creation_date is None


@pytest.mark.samples()
@pytest.mark.parametrize(
"pdf_path", [SAMPLE_ROOT / "017-unreadable-meta-data/unreadablemetadata.pdf"]
Expand Down

0 comments on commit c239073

Please sign in to comment.