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.



262
263
264
# File 'lib/hexapdf/dictionary_fields.rb', line 262

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

.convert(str, _type, _document) ⇒ Object

Checks if the given object is a string and converts into a Time object if possible. Otherwise returns nil.



271
272
273
274
275
276
277
# File 'lib/hexapdf/dictionary_fields.rb', line 271

def self.convert(str, _type, _document)
  return unless str.kind_of?(String) && (m = str.match(DATE_RE))

  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

.usable_for?(type) ⇒ Boolean

This converter is usable if the type is PDFDate.

Returns:



257
258
259
# File 'lib/hexapdf/dictionary_fields.rb', line 257

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