Class: ActiveLdap::Schema::Syntaxes::GeneralizedTime

Inherits:
Base
  • Object
show all
Defined in:
lib/active_ldap/schema/syntaxes.rb

Constant Summary collapse

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

Constants inherited from Base

Base::PRINTABLE_CHARACTER, Base::SYNTAXES, Base::UNPRINTABLE_CHARACTER

Instance Method Summary collapse

Methods inherited from Base

#binary?, #valid?, #validate

Methods included from GetTextSupport

included

Instance Method Details

#normalize_value(value) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/active_ldap/schema/syntaxes.rb', line 219

def normalize_value(value)
  if value.is_a?(Time)
    normalized_value = value.strftime("%Y%m%d%H%M%S")
    if value.gmt?
      normalized_value + "Z"
    else
      # for timezones with non-zero minutes, such as IST which is +0530,
      # divmod(3600) will give wrong value of 1800

      offset = value.gmtoff / 60 # in minutes
      normalized_value + ("%+03d%02d" % offset.divmod(60))
    end
  else
    value
  end
end

#type_cast(value) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/active_ldap/schema/syntaxes.rb', line 183

def type_cast(value)
  return value if value.nil? or value.is_a?(Time)
  match_data = FORMAT.match(value)
  if match_data
    required_components = match_data.to_a[1, 5]
    return value if required_components.any?(&:nil?)
    year, month, day, hour, minute = required_components.collect(&:to_i)
    second = match_data[-3].to_i
    fraction = match_data[-2]
    fraction = fraction.to_f if fraction
    time_zone = match_data[-1]
    arguments = [
      value, year, month, day, hour, minute, second, fraction, time_zone,
      Time.now,
    ]
    if Time.method(:make_time).arity == 11
      arguments[2, 0] = nil
    end
    begin
      Time.send(:make_time, *arguments)
    rescue ArgumentError
      raise if year >= 1700
      out_of_range_messages = ["argument out of range",
                               "time out of range"]
      raise unless out_of_range_messages.include?($!.message)
      Time.at(0)
    rescue RangeError
      raise if year >= 1700
      raise if $!.message != "bignum too big to convert into `long'"
      Time.at(0)
    end
  else
    value
  end
end