| Class | ActiveLdap::Schema::Syntaxes::GeneralizedTime |
| In: |
lib/active_ldap/schema/syntaxes.rb
|
| Parent: | Base |
| FORMAT | = | /\A (\d{4,4})? (\d{2,2})? (\d{2,2})? (\d{2,2})? (\d{2,2})? (\d{2,2})? ([,.]\d+)? ([+-]\d{4,4}|Z)? \z/x |
# File lib/active_ldap/schema/syntaxes.rb, line 198
198: def normalize_value(value)
199: if value.is_a?(Time)
200: normalized_value = value.strftime("%Y%m%d%H%M%S")
201: if value.gmt?
202: normalized_value + "Z"
203: else
204: normalized_value + ("%+03d%02d" % value.gmtoff.divmod(3600))
205: end
206: else
207: value
208: end
209: end
# File lib/active_ldap/schema/syntaxes.rb, line 179
179: def type_cast(value)
180: return value if value.nil? or value.is_a?(Time)
181: match_data = FORMAT.match(value)
182: if match_data
183: required_components = match_data.to_a[1, 6]
184: return value if required_components.any?(&:nil?)
185: year, month, day, hour, minute, second =
186: required_components.collect(&:to_i)
187: fraction = match_data[-2]
188: fraction = fraction.to_f if fraction
189: time_zone = match_data[-1]
190: Time.send(:make_time,
191: year, month, day, hour, minute, second, fraction,
192: time_zone, Time.now)
193: else
194: value
195: end
196: end
# File lib/active_ldap/schema/syntaxes.rb, line 212
212: def validate_normalized_value(value, original_value)
213: match_data = FORMAT.match(value)
214: if match_data
215: year, month, day, hour, minute, second, fraction, time_zone =
216: match_data.to_a[1..-1]
217: missing_components = []
218: %w(year month day hour minute second).each do |component|
219: missing_components << component unless eval(component)
220: end
221: if missing_components.empty?
222: nil
223: else
224: params = [original_value.inspect, missing_components.join(", ")]
225: _("%s has missing components: %s") % params
226: end
227: else
228: _("%s is invalid time format") % original_value.inspect
229: end
230: end