Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-2118

DateTime <-> Time conversion discards timezone and milliseconds

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.0
    • Affects Version/s: None
    • Component/s: None

      Consider the following example:

      class Person
      include Mongoid::Document
      field :updated_at, type: Time
      end

      p = Person.new
      time = DateTime.parse("2012-06-17 18:42:15.123Z")
      p.updated_at = time
      puts p.updated_at
      puts p.updated_at.to_f - time.to_f # should be close to 0

      The result on my machine is 2012-06-17 18:42:15 +0200 and -7200.12299990654.

      If I look at mongoid/extensions/date_time.rb, Mongoid does the following to convert the DateTime to a Time:

      ::Time.configured.local(year, month, day, hour, min, sec)

      Essentially, it throws away the timezone and any fractions of a second.

      Personally I don't care in which timezone the result is (UTC or my local timezone), as long as the actual point in time stays the same. In other words, p.updated_at.to_f should be equal to time.to_f, apart from rounding issues.

      This was tested with Mongoid 3.0.0.rc and Ruby 1.9.2p290.

            Assignee:
            durran Durran Jordan
            Reporter:
            rkistner rkistner
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: