Skip to content
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

TZ offset minute has to be negated in the Western Hemisphere #46872

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions activemodel/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
* Fix a bug where type casting of string to `Time` and `DateTime` doesn't
calculate minus minute value in TZ offset correctly.

*Akira Matsuda*

* Port the `type_for_attribute` method to Active Model. Classes that include
`ActiveModel::Attributes` will now provide this method. This method behaves
the same for Active Model as it does for Active Record.
Expand Down
8 changes: 7 additions & 1 deletion activemodel/lib/active_model/type/helpers/time_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ def fast_string_to_time(string)
end

if $8
offset = $8 == "Z" ? 0 : $8.to_i * 3600 + $9.to_i * 60
offset = \
if $8 == "Z"
0
else
offset_h, offset_m = $8.to_i, $9.to_i
offset_h.to_i * 3600 + (offset_h.negative? ? -1 : 1) * offset_m * 60
end
end

new_time($1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, usec, offset)
Expand Down
1 change: 1 addition & 0 deletions activemodel/test/cases/type/time_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def test_type_cast_time
assert_equal ::Time.utc(2000, 1, 1, 16, 45, 54), type.cast("2015-06-13T19:45:54+03:00")
assert_equal ::Time.utc(1999, 12, 31, 21, 7, 8), type.cast("06:07:08+09:00")
assert_equal ::Time.utc(2000, 1, 1, 16, 45, 54), type.cast(4 => 16, 5 => 45, 6 => 54)
assert_equal ::Time.utc(2000, 1, 1, 3, 30, 0), type.cast("2023-01-01T00:00:00-03:30")
end

def test_user_input_in_time_zone
Expand Down