Method: Microformats::TimePropertyParser#parse_dt

Defined in:
lib/microformats/time_property_parser.rb

#parse_dt(data, normalize: false) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/microformats/time_property_parser.rb', line 82

def parse_dt(data, normalize: false)
  # currently the value-class-pattern page lists to normalize  and remove :'s but not regular parsing, seems very odd
  # https://github.com/microformats/tests/issues/29
  #
  # TODO: this still allows a lot of non correct values such as 39th day of the month, etc
  case data.strip
  when /^P\d*W$/
    @duration_value = data if @duration_value.nil?

  when /^P(\d+Y)?(\d+M)?(\d+D)?(T(\d+H)?(\d+M)?(\d+S)?)?$/
    @duration_value = data if @duration_value.nil?

  when /^(\d{4}-[01]\d-[0-3]\d)[tT ]([0-2]\d:[0-5]\d(:[0-5]\d)?)?([zZ]|[-+][01]?\d:?[0-5]\d)?$/
    @date_value = Regexp.last_match(1) if @date_value.nil?
    @time_value = Regexp.last_match(2) if @time_value.nil?
    @tz_value = Regexp.last_match(4).tr('z', 'Z') if @tz_value.nil?

  when /^(\d{4}-[01]\d-[0-3]\d)[tT ]([0-2]\d:[0-5]\d(:[0-5]\d)?)( ?[-+]\d\d:?(\d\d)?)$/
    @date_value = Regexp.last_match(1) if @date_value.nil?
    @time_value = Regexp.last_match(2) if @time_value.nil?

    if normalize
      @tz_value = Regexp.last_match(4).tr('z', 'Z').delete(':') if @tz_value.nil?
    else
      @tz_value = Regexp.last_match(4).tr('z', 'Z') if @tz_value.nil?
    end

  when /^(\d{4}-[0-3]\d\d)[tT ]([0-2]\d:[0-5]\d(:[0-5]\d)?)?([zZ]|[-+][01]?\d:?[0-5]\d)?$/
    @date_value = Regexp.last_match(1) if @date_value.nil?
    @time_value = Regexp.last_match(2) if @time_value.nil?
    @tz_value = Regexp.last_match(4).tr('z', 'Z') if @tz_value.nil?

  when /^(\d{4}-[0-3]\d\d)[tT ]([0-2]\d:[0-5]\d(:[0-5]\d)?)( ?[-+]\d\d:?(\d\d)?)$/
    @date_value = Regexp.last_match(1) if @date_value.nil?
    @time_value = Regexp.last_match(2) if @time_value.nil?

    if normalize
      @tz_value = Regexp.last_match(4).tr('z', 'Z').delete(':') if @tz_value.nil?
    else
      @tz_value = Regexp.last_match(4).tr('z', 'Z') if @tz_value.nil?
    end

  when /^(\d{4})-([01]?\d)-([0-3]?\d)$/
    @date_value = DateTime.new(Regexp.last_match(1).to_i, Regexp.last_match(2).to_i, Regexp.last_match(3).to_i).strftime('%F') if @date_value.nil?

  when /^(\d{4})-([0-3]\d{2})$/
    @date_value = data if @date_value.nil?

  when /^(\d{4})-([01]?\d)$/
    @date_value = data if @date_value.nil?

  when /^([zZ]|[-+][01]?\d:?[0-5]\d)$/
    if normalize
      @tz_value = Regexp.last_match(1).tr('z', 'Z').delete(':') if @tz_value.nil?
    else
      @tz_value = Regexp.last_match(1).tr('z', 'Z') if @tz_value.nil?
    end

  when /^([0-2]\d:[0-5]\d(:[0-5]\d)?)([zZ]|[-+][01]\d:?\d\d)?$/
    @time_value = Regexp.last_match(1) if @time_value.nil?

    if normalize
      @tz_value = Regexp.last_match(3).tr('z', 'Z').delete(':') if @tz_value.nil?
    else
      @tz_value = Regexp.last_match(3).tr('z', 'Z') if @tz_value.nil?
    end

  when /^[0-2]\d:[0-5]\d[zZ]?$/
    @time_value = Time.parse(data).strftime('%H:%M') if @time_value.nil?
    @tz_value = 'Z'

  when /^([0-2]\d:[0-5]\d:[0-5]\d)([-+][01]\d:?[0-5]\d)$/
    Time.parse(data).strftime('%T') # to make sure this time doesn't throw an error

    @time_value = Regexp.last_match(1) if @time_value.nil?
    @tz_value = Regexp.last_match(2) if @tz_value.nil?

  when /^([0-2][0-0]:[0-5]\d)([-+][01]\d:?[0-5]\d)$/
    Time.parse(data).strftime('%H:%M') # to make sure this time doesn't throw an error

    @time_value = Regexp.last_match(1) if @time_value.nil?
    @tz_value = Regexp.last_match(2) if @tz_value.nil?

  when /^([01]?\d):?([0-5]\d)?p\.?m\.?$/i
    @time_value = (Regexp.last_match(1).to_i + 12).to_s + ':' + Regexp.last_match(2).to_s.rjust(2, '0') if @time_value.nil?

  when /^([01]?\d):?([0-5]\d)?a\.?m\.?$/i
    @time_value = Regexp.last_match(1).to_s.rjust(2, '0') + ':' + Regexp.last_match(2).to_s.rjust(2, '0') if @time_value.nil?

  when /^([01]?\d):([0-5]\d):([0-5]\d)?p\.?m\.?$/i
    @time_value = (Regexp.last_match(1).to_i + 12).to_s + ':' + Regexp.last_match(2).to_s.rjust(2, '0') + ':' + Regexp.last_match(3).to_s.rjust(2, '0') if @time_value.nil?

  when /^([01]?\d):([0-5]\d):([0-5]\d)?a\.?m\.?$/i
    @time_value = Regexp.last_match(1).to_s.rjust(2, '0') + ':' + Regexp.last_match(2).to_s.rjust(2, '0') + ':' + Regexp.last_match(3).to_s.rjust(2, '0') if @time_value.nil?

  else
    t = Time.parse(data)

    @time_value = t.strftime('%T') if @time_value.nil?
    @date_value = t.strftime('%F') if @date_value.nil?
  end
rescue
  nil
end