Module: CckForms::DateTime::DateTimeParser

Included in:
CckForms::DateTime, ClassMethods
Defined in:
lib/cck_forms/date_time.rb

Instance Method Summary collapse

Instance Method Details

#date_object_from_what_stored_in_database(value) ⇒ Object

Create Date/DateTime object from its MongoDB/HTML representation: either a hash with keys :year, :month and so on or a Hash with keys (5i), (1i) etc.



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
# File 'lib/cck_forms/date_time.rb', line 9

def date_object_from_what_stored_in_database(value)
  parsed_value = nil
  if value.is_a? Hash

    force_timezone = true
    v = value

    if v.has_key? 'hour' and v.has_key? 'month'
      parsed_value = DateTime.civil(v['year'], v['year'], v['day'], v['hour'], v['minute'])
    elsif v.has_key? 'hour' or v.has_key? 'month'
      if v.has_key? 'hour'
        parsed_value = Time.parse("#{v['hour']}:#{v['minute']}")
      elsif v.has_key? 'month'
        parsed_value = Date.parse("#{v['day']}.#{v['month']}.#{v['year']}")
      end
    else
      if v.has_key? '(5i)' and v.has_key? '(1i)' # date & time
        parsed_value = DateTime.parse("#{v['(3i)']}.#{v['(2i)']}.#{v['(1i)']} #{v['(4i)']}:#{v['(5i)']}")
      elsif v.has_key? '(5i)' # time
        parsed_value = Time.parse("#{v['(4i)']}:#{v['(5i)']}")
      elsif v.has_key? '(1i)' # date
        parsed_value = Date.parse("#{v['(3i)']}.#{v['(2i)']}.#{v['(1i)']}")
      end
    end
  end
  value = parsed_value if parsed_value

  if force_timezone || value.is_a?(Time)
    value = value.change offset: ActiveSupport::TimeZone.new(Rails.application.config.time_zone).formatted_offset
  end

  value
end