Skip to content

Commit b183dcd

Browse files
mdotterermetaskills
authored andcommitted
Fix mangled times after save when default_timezone is set to local (#609)
1 parent 4a83685 commit b183dcd

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

lib/active_record/connection_adapters/sqlserver/type/datetime.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,16 @@ def quoted(value)
3535
private
3636

3737
def fast_string_to_time(string)
38-
fast_string_to_time_zone.strptime(string, fast_string_to_time_format).time
38+
time = ActiveSupport::TimeZone['UTC'].strptime(string, fast_string_to_time_format)
39+
new_time(time.year, time.month, time.day, time.hour,
40+
time.min, time.sec, Rational(time.nsec, 1_000))
3941
rescue ArgumentError
4042
super
4143
end
4244

4345
def fast_string_to_time_format
4446
"#{::Time::DATE_FORMATS[:_sqlserver_datetime]}.%N".freeze
4547
end
46-
47-
def fast_string_to_time_zone
48-
::Time.zone || ActiveSupport::TimeZone['UTC']
49-
end
50-
5148
end
5249
end
5350
end

test/cases/coerced_tests.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,28 @@ def test_update_attributes_coerced
586586
# assert_nothing_raised { topic.reload }
587587
# assert_equal topic.title, Topic.find(1234).title
588588
end
589+
590+
def test_update_date_time_attributes
591+
Time.use_zone("Eastern Time (US & Canada)") do
592+
topic = Topic.find(1)
593+
time = Time.zone.parse("2017-07-17 10:56")
594+
topic.update_attributes!(written_on: time)
595+
assert_equal(time, topic.written_on)
596+
end
597+
end
598+
599+
def test_update_date_time_attributes_with_default_timezone_local
600+
with_env_tz 'America/New_York' do
601+
with_timezone_config default: :local do
602+
Time.use_zone("Eastern Time (US & Canada)") do
603+
topic = Topic.find(1)
604+
time = Time.zone.parse("2017-07-17 10:56")
605+
topic.update_attributes!(written_on: time)
606+
assert_equal(time, topic.written_on)
607+
end
608+
end
609+
end
610+
end
589611
end
590612

591613

0 commit comments

Comments
 (0)