Jerry Cheung
Jerry Cheung

Reputation: 1268

How to store UTC time values in Mongo with Mongoid?

The behavior I'm observing with the Mongoid adapter is that it'll save 'time' fields with the current system timezone into the database. Note that it's the system time and not Rail's environment's Time.zone. If I change the system timezone, then subsequent saves will pick up the current system timezone.

# system currently at UTC -7
@record.time_attribute = Time.now.utc
@record.save

# in mongo, the value is "time_attribute" : "Mon May 17 2010 12:00:00 GMT-0700 (QYZST)"
@record.reload.time_attribute.utc?  # false

Upvotes: 4

Views: 1471

Answers (1)

Jim Garvin
Jim Garvin

Reputation: 4906

Try setting the use_utc mongoid config parameter to true.

It tells Mongoid that you want to return times in UTC: http://github.com/durran/mongoid/blob/master/lib/mongoid/config.rb#L22

Upvotes: 1

Related Questions