module ONIX::DateHelper

Attributes

date[RW]

@return [Date]

date_format[RW]

@return [DateFormat]

datetime[RW]

@return [Time]

Public Instance Methods

format_from_code(code) click to toggle source

@param [String] code @return [String]

# File lib/onix/date.rb, line 49
def format_from_code(code)
  case code
  when "00"
    "%Y%m%d"
  when "01"
    "%Y%m"
  when "02"
    "%Y%%W"
  when "05"
    "%Y"
  when "13"
    "%Y%m%dT%H%M%S"
  when "14"
    "%Y%m%dT%H%M%S%z"
  else
    nil
  end
end
format_from_string(str) click to toggle source

@param [String] str

# File lib/onix/date.rb, line 69
def format_from_string(str)
  case str
  when /^\d{4}\d{2}\d{2}T\d{2}\d{2}\d{2}/
    "%Y%m%dT%H%M%S%z"
  when /^\d{4}\-\d{2}\-\d{2}$/
    "%Y-%m-%d"
  when /^\d{4}\d{2}\d{2}$/
    "%Y%m%d"
  when /^\d{4}\d{2}$/
    "%Y%m"
  when /^\d{4}$/
    "%Y"
  else
    nil
  end
end
parse_date() click to toggle source
# File lib/onix/date.rb, line 10
def parse_date
  if @date_format
    @deprecated_date_format = true
  else
    if @date.is_a?(TextWithAttributes)
      @date_format = @date.attributes["dateformat"]
    end
  end

  @datetime = strpdate!(@date, @date_format)
  @date = @datetime ? @datetime.to_date : nil
end
strpdate!(date_txt, date_format) click to toggle source

@param [String] date_txt @param [DateFormat] date_format @return [Time]

# File lib/onix/date.rb, line 26
def strpdate!(date_txt, date_format)
  date_format ||= DateFormat.from_code("00")
  code_format = format_from_code(date_format.code)
  text_format = format_from_string(date_txt)

  format = code_format

  if code_format != text_format
    puts "WARN incorrect date format #{text_format} != #{code_format}"
    format = text_format
  end

  begin
    datetime = Time.strptime(date_txt, format) if format && %w[00 01 02 05 13 14].include?(date_format.code)
  rescue => e
    # invalid date
  end

  datetime
end
time() click to toggle source

@return [Time]

# File lib/onix/date.rb, line 87
def time
  @datetime
end