Module: HexaPDF::DictionaryFields::DateConverter

Defined in:
lib/hexapdf/dictionary_fields.rb

Overview

Converter module for handling PDF date fields since they are stored as strings.

The ISO PDF specification differs from Adobe’s specification in respect to the supported date format. When converting from a date string to a Time object, this is taken into account.

See: PDF1.7 s7.9.4, ADB1.7 3.8.3

Constant Summary collapse

DATE_RE =

:nodoc:

/\AD:(\d{4})(\d\d)?(\d\d)?(\d\d)?(\d\d)?(\d\d)?([Z+-])?(?:(\d\d)(?:'|'(\d\d)'?|\z)?)?\z/n

Class Method Summary collapse

Class Method Details

.additional_typesObject

A date field may contain a string in PDF format, or a Time, Date or DateTime object.



301
302
303
# File 'lib/hexapdf/dictionary_fields.rb', line 301

def self.additional_types
  [String, Time, Date, DateTime]
end

.convert(str, _type, _document) ⇒ Object

Converts the string into a Time object.



314
315
316
317
318
319
# File 'lib/hexapdf/dictionary_fields.rb', line 314

def self.convert(str, _type, _document)
  m = DATE_RE.match(str)
  utc_offset = (m[7].nil? || m[7] == 'Z' ? 0 : "#{m[7]}#{m[8]}:#{m[9] || '00'}")
  Time.new(m[1].to_i, (m[2] ? m[2].to_i : 1), (m[3] ? m[3].to_i : 1),
           m[4].to_i, m[5].to_i, m[6].to_i, utc_offset)
end

.convert?(data, _type) ⇒ Boolean

Returns true if the given data should be converted to a Time object.

Returns:



309
310
311
# File 'lib/hexapdf/dictionary_fields.rb', line 309

def self.convert?(data, _type)
  data.kind_of?(String) && data =~ DATE_RE
end

.usable_for?(type) ⇒ Boolean

This converter is usable if the type is PDFDate.

Returns:



296
297
298
# File 'lib/hexapdf/dictionary_fields.rb', line 296

def self.usable_for?(type)
  type == PDFDate
end