Recently I have discovered an issue with PdfDocumentInformation#getCreationDate() method. Consider situation where you have some PDF file that has been created at 2022-12-10 18:07:43 UTC+1 [Europe/Warsaw] (which is 2022-12-10 17:07:43 UTC) and you want to read its creation date using PdfDocumentInformation#getCreationDate():
* If your local time zone (or rather your local time zone offset) is very same as one used during file creation (Europe/Warsaw in this example), then method getCreationDate() returns correct value (which is 2022-12-10 17:07:43)
* If your local time zone has different offset then getCreationDate() returns incorrect values
Take a look on example code with three unit tests which documents the issue: github.com/kr5ture/spire-pdf-default-timezone-example/blob/master/src/test/java/com/example/ExampleTest.java
- Code: Select all
package com.example;
import com.spire.pdf.PdfDocument;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.time.Instant;
import java.util.TimeZone;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ExampleTest {
@Test
void when_file_created_at_17_07_UTC_then_should_return_same_when_default_timezone_offset_has_plus_8() throws IOException {
// given
TimeZone.setDefault(TimeZone.getTimeZone("Australia/Perth")); // UTC+8
// and - file created at 2022-12-10 18:07:43 UTC+1 [Europe/Warsaw] which is 2022-12-10 17:07:43 UTC
Path filePath = Path.of(getClass().getResource("/example.pdf").getPath());
// and
PdfDocument pdfDocument = new PdfDocument(Files.newInputStream(filePath, StandardOpenOption.READ));
// when
Instant result = pdfDocument.getDocumentInformation()
.getCreationDate()
.toInstant(); // <-- should be 2022-12-10 17:07:43 UTC+0
// then - fails:
// Expected :2022-12-10T17:07:43Z
// Actual :2022-12-10T10:07:43Z
assertEquals("2022-12-10T17:07:43Z", result.toString());
}
@Test
void when_file_created_at_17_07_UTC_then_should_return_same_when_default_timezone_is_UTC() throws IOException {
// given
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
// and - file created at 2022-12-10 18:07:43 UTC+1 [Europe/Warsaw] which is 2022-12-10 17:07:43 UTC
Path filePath = Path.of(getClass().getResource("/example.pdf").getPath());
// and
PdfDocument pdfDocument = new PdfDocument(Files.newInputStream(filePath, StandardOpenOption.READ));
// when
Instant result = pdfDocument.getDocumentInformation()
.getCreationDate()
.toInstant(); // <-- should be 2022-12-10 17:07:43 UTC+0
// then - fails:
// Expected :2022-12-10T17:07:43Z
// Actual :2022-12-10T18:07:43Z
assertEquals("2022-12-10T17:07:43Z", result.toString());
}
@Test
void when_file_created_at_17_07_UTC_then_should_return_same_when_default_timezone_is_same_as_one_from_file_creation() throws IOException {
// given
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Warsaw")); // UTC+1 at 2022-12-10 (winter time)
// and - file created at 2022-12-10 18:07:43 UTC+1 [Europe/Warsaw] which is 2022-12-10 17:07:43 UTC
Path filePath = Path.of(getClass().getResource("/example.pdf").getPath());
// and
PdfDocument pdfDocument = new PdfDocument(Files.newInputStream(filePath, StandardOpenOption.READ));
// when
Instant result = pdfDocument.getDocumentInformation()
.getCreationDate()
.toInstant(); // <-- should be 2022-12-10 17:07:43 UTC+0
// then - passed!
assertEquals("2022-12-10T17:07:43Z", result.toString());
}
}
* when_file_created_at_17_07_UTC_then_should_return_same_when_default_timezone_is_same_as_one_from_file_creation - this test passess
* when_file_created_at_17_07_UTC_then_should_return_same_when_default_timezone_offset_has_plus_8 - this test fails
* when_file_created_at_17_07_UTC_then_should_return_same_when_default_timezone_is_UTC - this test fails
I have tried two sprife.pdf versions (8.10.1 and 8.11.8) and both seems affected.
In addition it seems that creation date attribute contains offset - take a look on pdfinfo output:
- Code: Select all
$ pdfinfo -isodates app/src/test/resources/example.pdf
Creator: Writer
Producer: LibreOffice 7.4
CreationDate: 2022-12-10T18:07:43+01 // <------ here
Custom Metadata: no
Metadata Stream: no
Tagged: no
UserProperties: no
Suspects: no
Form: none
JavaScript: no
Pages: 1
Encrypted: no
Page size: 595.304 x 841.89 pts (A4)
Page rot: 0
File size: 7484 bytes
Optimized: no
PDF version: 1.6