Skip to content

Commit

Permalink
Time keys now strip milliseconds. Rounding was occasionally inaccurat…
Browse files Browse the repository at this point in the history
…e such as at the end of the month which was not cool. If you hate this, let me know and why. Personally I have never cared about milliseconds.
  • Loading branch information
jnunemaker committed May 12, 2010
1 parent c4af008 commit ad25155
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/mongo_mapper/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ def self.to_mongo(value)
else
time_class = Time.try(:zone).present? ? Time.zone : Time
time = value.is_a?(Time) ? value : 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).floor / 1000.0).utc if time
# strip milliseconds as Ruby does micro and bson does milli and rounding rounded wrong
at(time.to_i).utc if time
end
end

Expand Down
6 changes: 3 additions & 3 deletions test/unit/test_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ class SupportTest < Test::Unit::TestCase
end

should "be time to milliseconds if string" do
Time.to_mongo('2000-01-01 01:01:01.123456').to_f.should == Time.local(2000, 1, 1, 1, 1, 1, 123000).utc.to_f
Time.to_mongo('2000-01-01 01:01:01.123456').to_f.should == Time.local(2000, 1, 1, 1, 1, 1, 0).utc.to_f
end

should "be time in utc if time" do
Expand All @@ -308,13 +308,13 @@ class SupportTest < Test::Unit::TestCase
context "Time#to_mongo with Time.zone" do
should "be time to milliseconds if time" do
Time.zone = 'Hawaii'
Time.to_mongo(Time.zone.local(2009, 8, 15, 14, 0, 0, 123456)).to_f.should == Time.utc(2009, 8, 16, 0, 0, 0, 123000).to_f
Time.to_mongo(Time.zone.local(2009, 8, 15, 14, 0, 0, 123456)).to_f.should == Time.utc(2009, 8, 16, 0, 0, 0, 0).to_f
Time.zone = nil
end

should "be time to milliseconds if string" do
Time.zone = 'Hawaii'
Time.to_mongo('2009-08-15 14:00:00.123456').to_f.should == Time.utc(2009, 8, 16, 0, 0, 0, 123000).to_f
Time.to_mongo('2009-08-15 14:00:00.123456').to_f.should == Time.utc(2009, 8, 16, 0, 0, 0, 0).to_f
Time.zone = nil
end

Expand Down

0 comments on commit ad25155

Please sign in to comment.