PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Bugs and suggestions for timestamp parsing
https://forum.pdfsharp.net/viewtopic.php?f=3&t=3190
Page 1 of 1

Author:  btriffles [ Sun Sep 20, 2015 8:08 am ]
Post subject:  Bugs and suggestions for timestamp parsing

I found a couple bugs in the parsing of creation/modification timestamps in PDFsharp 1.32 (and presumably 1.50).

In PdfSharp.Pdf.IO\Parser.cs :: ParseDateTime:
1. The added/subtracted time zone offset (ts) needs to be assigned back into the variable "datetime".
2. The time zone offset is added when it should be subtracted and vice versa.

Here is some corrected code:
Code:
TimeSpan ts = new TimeSpan(hh, mm, 0);
if (o == '-')  // Changed '+' to '-'
  datetime = datetime.Add(ts);  // Added "datetime = "
else
  datetime = datetime.Subtract(ts);  // Added "datetime = "


Here are some further suggestions to improve the code:

A. In PdfSharp.Pdf\PdfDocumentInformation.cs :: CreationDate/ModificationDate :: get, the second parameter to Elements.GetDateTime should be DateTime.MinValue instead of DateTime.Now so a constant known value is returned when either timestamp cannot be parsed/read. (DateTime.Now constantly changes and therefore cannot be easily compared.)

B. In PdfSharp.Pdf.IO\Parser.cs :: ParseDateTime, there should be some kind of warning or error when the date doesn't start with "D:" because this violates the PDF standard and I have no idea if DateTime.Parse will return the correct UTC time. I'm pretty sure the current call to DateTime.Parse will vary based on the your computer's cultural settings, so its output isn't trustworthy. If there are some PDF writing libraries that use a "plain English format", their precise format should be parsed instead of lazily calling DateTime.Parse with no options.

Thanks for the great library!

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/