{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/33715","repository_url":"https://api.github.com/repos/rails/rails","labels_url":"https://api.github.com/repos/rails/rails/issues/33715/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/33715/comments","events_url":"https://api.github.com/repos/rails/rails/issues/33715/events","html_url":"https://github.com/rails/rails/issues/33715","id":353855111,"node_id":"MDU6SXNzdWUzNTM4NTUxMTE=","number":33715,"title":"Precision value in Time with microsecond sometimes gets offset or inaccurate after AR is created","user":{"login":"calvinchso","id":1727450,"node_id":"MDQ6VXNlcjE3Mjc0NTA=","avatar_url":"https://avatars1.githubusercontent.com/u/1727450?v=4","gravatar_id":"","url":"https://api.github.com/users/calvinchso","html_url":"https://github.com/calvinchso","followers_url":"https://api.github.com/users/calvinchso/followers","following_url":"https://api.github.com/users/calvinchso/following{/other_user}","gists_url":"https://api.github.com/users/calvinchso/gists{/gist_id}","starred_url":"https://api.github.com/users/calvinchso/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/calvinchso/subscriptions","organizations_url":"https://api.github.com/users/calvinchso/orgs","repos_url":"https://api.github.com/users/calvinchso/repos","events_url":"https://api.github.com/users/calvinchso/events{/privacy}","received_events_url":"https://api.github.com/users/calvinchso/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-08-24T16:47:28Z","updated_at":"2018-08-25T15:36:10Z","closed_at":null,"author_association":"NONE","body":"### Steps to reproduce\r\nPlease use the following code to reproduce the issue. Please note the test passes in Rails 4.2.10 but fails in Rails 5.2.1.\r\n\r\n```ruby\r\n# frozen_string_literal: true\r\n  \r\nbegin\r\n  require \"bundler/inline\"\r\nrescue LoadError => e\r\n  $stderr.puts \"Bundler version 1.10 or later is required. Please update your Bundler\"\r\n  raise e\r\nend\r\n\r\ngemfile(true) do\r\n  source \"https://rubygems.org\"\r\n\r\n  git_source(:github) { |repo| \"https://github.com/#{repo}.git\" }\r\n\r\n  gem \"rails\", '5.2.1'\r\n  gem \"sqlite3\"\r\nend\r\n\r\nrequire \"active_record\"\r\nrequire \"minitest/autorun\"\r\nrequire \"logger\"\r\n\r\n# This connection will do for database-independent bug reports.\r\nActiveRecord::Base.establish_connection(adapter: \"sqlite3\", database: \":memory:\")\r\nActiveRecord::Base.logger = Logger.new(STDOUT)\r\n\r\nActiveRecord::Schema.define do\r\n  create_table :posts, force: true do |t|\r\n    t.datetime :posted_at, precision: 6\r\n    t.timestamps\r\n  end\r\nend\r\n\r\nclass Post < ActiveRecord::Base\r\nend\r\n\r\nclass PrecisionTest < Minitest::Test\r\n  def test_precision\r\n    100.times do\r\n      time_in_str = Time.now.to_f.round(6).to_s\r\n      $stdout.puts \"Creating a post record with posted_at = #{time_in_str}\"\r\n      time = Time.at(time_in_str.to_f)\r\n      assert_equal time_in_str, time.to_f.to_s\r\n      post = Post.create(:posted_at => time)\r\n      assert_equal time.to_f, post.posted_at.to_f\r\n    end\r\n  end\r\nend\r\n```\r\n\r\n### Expected behavior\r\nPrecision value in Time with microsecond should be kept accurate after AR is created.\r\n\r\n### Actual behavior\r\nPrecision value in Time with microsecond sometimes gets offset or inaccurate after AR is created.\r\n\r\nAs you can see, the first iteration, i.e. the first two assertions with posted_at = \"1535128380.190231\", has passed but in the next iteration, i.e. the last two assertions with posted_at = \"1535128380.203914\" has failed.\r\n\r\nPlease also note that in the second iteration, the generated INSERT statement has already got the wrong precision value, i.e. \"2018-08-24 16:33:00.203913\" and it should be \"1535128380.203914\"\r\n\r\n```\r\n# Running:\r\n\r\nCreating a post record with posted_at = 1535128380.190231\r\nD, [2018-08-24T09:33:00.197604 #59993] DEBUG -- :    (0.1ms)  begin transaction\r\nD, [2018-08-24T09:33:00.198085 #59993] DEBUG -- :   ↳ precision_test.rb:44\r\nD, [2018-08-24T09:33:00.201193 #59993] DEBUG -- :   Post Create (0.1ms)  INSERT INTO \"posts\" (\"posted_at\", \"created_at\", \"updated_at\") VALUES (?, ?, ?)  [[\"posted_at\", \"2018-08-24 16:33:00.190231\"], [\"created_at\", \"2018-08-24 16:33:00.198468\"], [\"updated_at\", \"2018-08-24 16:33:00.198468\"]]\r\nD, [2018-08-24T09:33:00.201512 #59993] DEBUG -- :   ↳ precision_test.rb:44\r\nD, [2018-08-24T09:33:00.202889 #59993] DEBUG -- :    (0.1ms)  commit transaction\r\nD, [2018-08-24T09:33:00.203173 #59993] DEBUG -- :   ↳ precision_test.rb:44\r\nCreating a post record with posted_at = 1535128380.203914\r\nD, [2018-08-24T09:33:00.204247 #59993] DEBUG -- :    (0.0ms)  begin transaction\r\nD, [2018-08-24T09:33:00.204535 #59993] DEBUG -- :   ↳ precision_test.rb:44\r\nD, [2018-08-24T09:33:00.208526 #59993] DEBUG -- :   Post Create (0.2ms)  INSERT INTO \"posts\" (\"posted_at\", \"created_at\", \"updated_at\") VALUES (?, ?, ?)  [[\"posted_at\", \"2018-08-24 16:33:00.203913\"], [\"created_at\", \"2018-08-24 16:33:00.205034\"], [\"updated_at\", \"2018-08-24 16:33:00.205034\"]]\r\nD, [2018-08-24T09:33:00.208948 #59993] DEBUG -- :   ↳ precision_test.rb:44\r\nD, [2018-08-24T09:33:00.210380 #59993] DEBUG -- :    (0.1ms)  commit transaction\r\nD, [2018-08-24T09:33:00.210697 #59993] DEBUG -- :   ↳ precision_test.rb:44\r\nF\r\n\r\nFailure:\r\nPrecisionTest#test_precision [/Users/calvin/projects/newapp/precision_test.rb:45]:\r\nExpected: 1535128380.203914\r\n  Actual: 1535128380.203913\r\n\r\n\r\nbin/rails test precision_test.rb:38\r\n\r\n\r\n\r\nFinished in 0.023380s, 42.7716 runs/s, 171.0864 assertions/s.\r\n1 runs, 4 assertions, 1 failures, 0 errors, 0 skips\r\n```\r\n\r\n\r\n### System configuration\r\n**Rails version**: 5.2.1\r\n\r\n**Ruby version**: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]\r\n"},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/415977453","html_url":"https://github.com/rails/rails/issues/33715#issuecomment-415977453","issue_url":"https://api.github.com/repos/rails/rails/issues/33715","id":415977453,"node_id":"MDEyOklzc3VlQ29tbWVudDQxNTk3NzQ1Mw==","user":{"login":"calvinchso","id":1727450,"node_id":"MDQ6VXNlcjE3Mjc0NTA=","avatar_url":"https://avatars1.githubusercontent.com/u/1727450?v=4","gravatar_id":"","url":"https://api.github.com/users/calvinchso","html_url":"https://github.com/calvinchso","followers_url":"https://api.github.com/users/calvinchso/followers","following_url":"https://api.github.com/users/calvinchso/following{/other_user}","gists_url":"https://api.github.com/users/calvinchso/gists{/gist_id}","starred_url":"https://api.github.com/users/calvinchso/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/calvinchso/subscriptions","organizations_url":"https://api.github.com/users/calvinchso/orgs","repos_url":"https://api.github.com/users/calvinchso/repos","events_url":"https://api.github.com/users/calvinchso/events{/privacy}","received_events_url":"https://api.github.com/users/calvinchso/received_events","type":"User","site_admin":false},"created_at":"2018-08-25T15:36:10Z","updated_at":"2018-08-25T15:36:10Z","author_association":"NONE","body":"Thanks. In our case, we're sure the input value is already truncated at the microsecond level, and we don't want the value to be altered somehow. Is there any way / which line should we patch it if we want the expected behavior? "}}
{"actor":{"display_login":"calvinchso"}}