-
Notifications
You must be signed in to change notification settings - Fork 36
fix: use DateTime instead of Time for time data type #392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,10 +44,28 @@ def test_set_and_save_time | |
| assert_equal expected_time, record.start_time | ||
| end | ||
|
|
||
| def test_date_time_string_value_with_subsecond_precision | ||
| def test_date_time_string_value_with_timezone_aware_attributes | ||
| TestTypeModel.time_zone_aware_attributes = true | ||
| TestTypeModel.reset_column_information | ||
|
|
||
| string_value = "2017-07-04 14:19:10.897761" | ||
| expected_time = ::Time.local 2017, 07, 4, 14, 19, 10, 897761 | ||
|
|
||
| record = TestTypeModel.new start_time: string_value | ||
| assert_equal expected_time, record.start_time.utc | ||
|
|
||
| record.save! | ||
| assert_equal expected_time, record.start_time.utc | ||
|
|
||
| assert_equal record, TestTypeModel.find_by(start_time: string_value) | ||
| ensure | ||
| TestTypeModel.time_zone_aware_attributes = false | ||
| TestTypeModel.reset_column_information | ||
| end | ||
|
|
||
| def test_date_time_string_value_with_subsecond_precision | ||
| string_value = "2017-07-04 14:19:10.897761" | ||
| expected_time = ::Time.local 2017, 07, 4, 14, 19, 10, 897761 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using expected_time = ::Time.utc 2017, 7, 4, 14, 19, 10, 897761 |
||
|
|
||
| record = TestTypeModel.new start_time: string_value | ||
| assert_equal expected_time, record.start_time.utc | ||
|
|
@@ -71,7 +89,7 @@ def test_date_time_with_string_value_with_non_iso_format | |
| assert_equal record, TestTypeModel.find_by(start_time: string_value) | ||
| end | ||
|
|
||
| def test_default_year_is_correct | ||
| def test_multiparameter_time | ||
| expected_time = ::Time.utc(2000, 1, 1, 10, 30, 0) | ||
| record = TestTypeModel.new start_time: { 4 => 10, 5 => 30 } | ||
|
|
||
|
|
@@ -82,6 +100,18 @@ def test_default_year_is_correct | |
|
|
||
| assert_equal expected_time, record.start_time | ||
| end | ||
|
|
||
| def test_multiparameter_datetime | ||
| expected_time = ::Time.utc(2020, 12, 25, 10, 30, 0) | ||
| record = TestTypeModel.new start_time: { 1 => 2020, 2 => 12, 3 => 25, 4 => 10, 5 => 30 } | ||
|
|
||
| assert_equal expected_time, record.start_time | ||
|
|
||
| record.save! | ||
| record.reload | ||
|
|
||
| assert_equal expected_time, record.start_time | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ | |
| module ActiveRecord | ||
| module Type | ||
| module Spanner | ||
| class Time < ActiveRecord::Type::Time | ||
| class Time < ActiveRecord::Type::DateTime | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing the base class to In Ruby's standard library, Consider updating the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't sound accurate to me, '2025-03-07T01:23:00.45678Z'.in_time_zone.class
=> ActiveSupport::TimeWithZone
'2025-03-07T01:23:00.45678Z'.in_time_zone.rfc3339(9)
=> "2025-03-07T01:23:00.456780000Z"
DateTime.parse('2025-03-07T01:23:00.45678Z').class
=> DateTime
DateTime.parse('2025-03-07T01:23:00.45678Z').rfc3339(9)
=> "2025-03-07T01:23:00.456780000+00:00" |
||
| def serialize_with_isolation_level value, isolation_level | ||
| if value == :commit_timestamp | ||
| return "PENDING_COMMIT_TIMESTAMP()" if isolation_level == :dml | ||
|
|
@@ -23,21 +23,6 @@ def serialize value | |
| val = super value | ||
| val.acts_like?(:time) ? val.utc.rfc3339(9) : val | ||
| end | ||
|
|
||
| def user_input_in_time_zone value | ||
| return value.in_time_zone if value.is_a? ::Time | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure this is needed given the superclass calls |
||
| super value | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def cast_value value | ||
| if value.is_a? ::String | ||
| value = value.empty? ? nil : ::Time.parse(value) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The superclass has optimized versions of parsing string times, so removed in favor of that. This did cause me to update the tests, but the tests aren't exactly accurate since they do not have the time zone set on Active Record to UTC. |
||
| end | ||
|
|
||
| value | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
::Time.localmakes the test dependent on the time zone of the environment where the tests are running. This can lead to flaky tests or failures on machines not set to UTC, especially when comparing against.utcvalues. It is better to use::Time.utcto ensure consistency with how the string value is likely to be parsed in a Spanner context. Additionally, avoid leading zeros in integer literals (like07) as they can be interpreted as octal and cause errors for values like08or09.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The goal of these tests is to ensure that it gets converted to UTC since
time_zone_aware_attributesis truthy.