Class: Exempi::XmpDateTime

Inherits:
FFI::Struct
  • Object
show all
Defined in:
lib/exempi/consts.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_datetime(source) ⇒ Exempi::XmpDateTime

Creates an XmpDateTime struct using a Ruby DateTime object, or a date string which is parseable by DateTime

Parameters:

  • source (DateTime, String)

    the date to convert

Returns:



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/exempi/consts.rb', line 255

def self.from_datetime source
  if source.is_a? String
    source = DateTime.parse source
  end

  struct = self.new
  [:year, :month, :day, :hour, :minute, :second].each do |field|
    struct[field] = source.send field
  end
  struct[:nanoSecond] = source.to_time.nsec

  match = source.zone.match(/(?<sign>[-+]){1}(?<hour>\d\d){1}:(?<minute>\d\d){1}/)
  if match
    if match[:sign] == '-' then struct[:tzSign] = -1
    elsif match[:hour] == '00' && match[:minute] == '00' then struct[:tzSign] = 0
    else struct[:tzSign] = 1
    end

    struct[:tzHour] = match[:hour].to_i
    struct[:tzMinute] = match[:minute].to_i
  end

  struct
end

Instance Method Details

#to_datetimeDateTime

Converts a XmpDateTime struct into a Ruby object

Returns:

  • (DateTime)

    A new Ruby DateTime object



241
242
243
244
245
246
247
248
249
# File 'lib/exempi/consts.rb', line 241

def to_datetime
  sign = self[:tzSign] == :XMP_TZ_WEST ? '-' : '+'
  zone = "%s%02d:%02d" % [sign, self[:tzHour], self[:tzMinute]]

  second = self[:second] + Rational(self[:nanoSecond],1000000000)

  DateTime.new self[:year], self[:month], self[:day], self[:hour],
    self[:minute], second, zone
end