Module: ONIX::DateHelper

Included in:
ContentDate, MarketDate, PriceDate, PublishingDate, SupplyDate
Defined in:
lib/onix/date.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dateObject

Returns the value of attribute date.



4
5
6
# File 'lib/onix/date.rb', line 4

def date
  @date
end

#date_formatObject

Returns the value of attribute date_format.



4
5
6
# File 'lib/onix/date.rb', line 4

def date_format
  @date_format
end

Instance Method Details

#format_from_code(code) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/onix/date.rb', line 53

def format_from_code(code)
  case code
    when "00"
      "%Y%m%d"
    when "01"
      "%Y%m"
    when "05"
      "%Y"
    when "14"
      "%Y%m%dT%H%M%S%z"
    else
      nil
  end
end

#format_from_string(str) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/onix/date.rb', line 68

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(n) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/onix/date.rb', line 6

def parse_date(n)
  @date_format=DateFormat.from_code("00")
  date_txt=nil
  @date=nil
  n.elements.each do |t|
    case t
      when tag_match("DateFormat")
        @date_format=DateFormat.parse(t)
      when tag_match("Date")
        date_txt=t.text
    end

    if t["dateformat"]
      @date_format = DateFormat.from_code(t["dateformat"])
    end
  end

  code_format=format_from_code(@date_format.code)
  text_format=format_from_string(date_txt)

  format=code_format

  if code_format!=text_format
#        puts "EEE date #{n.text} (#{text_format}) do not match code #{@format.code} (#{code_format})"
    format=text_format
  end

  begin
    if format
      case @date_format.code
        when "00"
          @date=Date.strptime(date_txt, format)
        when "01"
          @date=Date.strptime(date_txt, format)
        when "05"
          @date=Date.strptime(date_txt, format)
        when "14"
          @date=Time.strptime(date_txt, format)
        else
          @date=nil
      end
    end
  rescue
    # invalid date
  end
end

#timeObject



85
86
87
# File 'lib/onix/date.rb', line 85

def time
  @date.to_time
end