Class: Time

Inherits:
Object show all
Defined in:
lib/mongo_mapper/support.rb

Class Method Summary collapse

Class Method Details

.from_mongo(value) ⇒ Object



191
192
193
194
195
196
197
# File 'lib/mongo_mapper/support.rb', line 191

def self.from_mongo(value)
  if MongoMapper.use_time_zone? && value.present?
    value.in_time_zone(Time.zone)
  else
    value
  end
end

.to_mongo(value) ⇒ Object



181
182
183
184
185
186
187
188
189
# File 'lib/mongo_mapper/support.rb', line 181

def self.to_mongo(value)
  if value.nil? || value == ''
    nil
  else
    time = value.is_a?(Time) ? value : MongoMapper.time_class.parse(value.to_s)
    # Convert time to milliseconds since BSON stores dates with that accurracy, but Ruby uses microseconds
    Time.at((time.to_f * 1000).round / 1000.0).utc if time
  end
end