Module: BugBunny::Helpers

Extended by:
Helpers
Included in:
Helpers
Defined in:
lib/bug_bunny/helpers.rb

Instance Method Summary collapse

Instance Method Details

#datetime_values_to_utc(data) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/bug_bunny/helpers.rb', line 5

def datetime_values_to_utc(data)
  case data
  when Hash
    data.inject({}) {|memo, (k, v)| memo.merge!({k => datetime_values_to_utc(v)}) }
  when Array
    data.map {|e| datetime_values_to_utc(e) }
  when DateTime, Time
    data.to_time.utc.iso8601
  else
    data
  end
end

#utc_values_to_local(data) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bug_bunny/helpers.rb', line 18

def utc_values_to_local(data)
  case data
  when Hash
    data.inject({}) {|memo, (k, v)| memo.merge!({k => utc_values_to_local(v)}) }
  when Array
    data.map {|e| utc_values_to_local(e) }
  when DateTime, Time
    data.to_time.localtime # ensure we always use Time instances
  when /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:.*Z$/
    t = Time.respond_to?(:zone) ? Time.zone.parse(data) : Time.parse(data)
    t.to_time.localtime
  when /^\d{4}-\d{2}-\d{2}$/
    Date.parse(data)
  else
    data
  end
end