Module: Toy::Extensions::Time

Included in:
Time
Defined in:
lib/toy/extensions/time.rb

Instance Method Summary collapse

Instance Method Details

#from_store(value) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/toy/extensions/time.rb', line 15

def from_store(value, *)
  value = to_store(value)
  if ::Time.try(:zone).present? && value.present?
    value.in_time_zone(::Time.zone)
  else
    value
  end
end

#to_store(value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/toy/extensions/time.rb', line 4

def to_store(value, *)
  if value.nil? || value == ''
    nil
  else
    time_class = ::Time.try(:zone).present? ? ::Time.zone : ::Time
    time = value.is_a?(::Time) ? value : time_class.parse(value.to_s)
    # strip milliseconds as Ruby does micro and bson does milli and rounding rounded wrong
    at(time.to_i).utc if time
  end
end