16349: Skip converting DB timestamps to & from localtime.
authorTom Clegg <tom@tomclegg.ca>
Mon, 27 Apr 2020 20:57:55 +0000 (16:57 -0400)
committerPeter Amstutz <peter.amstutz@curii.com>
Mon, 8 Jun 2020 18:58:18 +0000 (14:58 -0400)
The previous code already returned the correct time. This change
merely avoids converting the time to the local time zone in
PostgreSQL, and back to UTC again in Rails.

This might also avoid rare problems in unusual edge cases where Rails
and PostgreSQL session use different time zones, like a configuration
mishap or a race during a daylight savings time transition.

Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@tomclegg.ca>

services/api/lib/db_current_time.rb

index fdb66415210aed9b95338f6dafc6e377de45ec50..5e1634ecb96f17661002afc3eb62dea44100adad 100644 (file)
@@ -3,9 +3,13 @@
 # SPDX-License-Identifier: AGPL-3.0
 
 module DbCurrentTime
-  CURRENT_TIME_SQL = "SELECT clock_timestamp()"
+  CURRENT_TIME_SQL = "SELECT clock_timestamp() AT TIME ZONE 'UTC'"
 
   def db_current_time
-    Time.parse(ActiveRecord::Base.connection.select_value(CURRENT_TIME_SQL)).to_time
+    Time.parse(ActiveRecord::Base.connection.select_value(CURRENT_TIME_SQL) + " +0000")
+  end
+
+  def db_transaction_time
+    Time.parse(ActiveRecord::Base.connection.select_value("SELECT current_timestamp AT TIME ZONE 'UTC'") + " +0000")
   end
 end