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

Incorrect behaviour when parsing strings as time

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

      When parsing strings as time the ActiveSupport::TimeZone.parse was returning nil on malformed Time strings and then converting into a float of 0.0 and becoming a Time. However the Time.parse throws the expected ArgumentError which mongoid throws back to the user.

      When using Time with zone:

       ruby 
      Mongoid.use_activesupport_time_zone = true
      
      class TestDateTimeField
        include Mongoid::Document
      
        field :dt, type: DateTime
      end
      
      test = TestDateTimeField.new
      
      test.dt = "not a time"
      test.dt #=> Thu, 01 Jan 1970 01:00:00 +0100
      
      test.dt = ""
      test.dt #=> nil
      
      test.dt = 1234567
      test.dt #=> Thu, 15 Jan 1970 07:56:07 +0100
      
      test.dt = "1234567"
      test.dt #=> Sun, 01 Jan 1234 00:00:00 -0001
      

      When using just Time

       ruby 
      Mongoid.use_activesupport_time_zone = false
      
      test.dt = "not a time"
      test.dt #=> Mongoid::Errors::InvalidTime: 'not a time' is not a valid Time.
      
      test.dt = ""
      test.dt #=> nil
      
      test.dt = 1234567
      test.dt #=> Thu, 15 Jan 1970 07:56:07 +0100
      
      test.dt = "1234567"
      test.dt #=> Sun, 01 Jan 1234 00:00:00 +0100
      

      `

            Assignee:
            Unassigned Unassigned
            Reporter:
            camallen camallen
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: