expanding the range of fast_string_to_time

Recently, in looking at profiling data from two different applications, I found that something like 5% of the application's time was being spent in converting strings to times on database fetches. Digging in, I found that the "fast" routine for doing this only worked if there was no timezone in the string. I ended up monkeypatching this in:

  # Rails has a "fast" time parsing shortcut when the database returns
  # a timestamp string without timezone. Unfortunately, ours does return a
  # timezone, but it's always UTC ("+00"). Hack so we can use the
  # fast routine still
  module ConnectionAdapters
    class Column
      module Format
        ISO_DATETIME_TZ = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?(\+\d\d)?\z/
      end

      class << self
        protected
        def fast_string_to_time(string)
          if string =~ Format::ISO_DATETIME_TZ
            tz = $8
            if tz && !(tz == "+00" && Base.default_timezone == :utc)
              return nil
            end

            microsec = ($7.to_f * 1_000_000).to_i
            new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec
          end
        end
      end
    end
  end

I don't know how generally applicable this patch would be. Clearly, it only helps if both your DB and your Rails app are configured to run in UTC. We use PostgreSQL, and I don't know if the same formating is used by other databases. But, I thought I would throw it out for comments.

-kevin

I don't know how generally applicable this patch would be. Clearly,
it only helps if both your DB and your Rails app are configured to run
in UTC. We use PostgreSQL, and I don't know if the same formating is
used by other databases. But, I thought I would throw it out for
comments.

This looks like a nice enhancement to the postgresql adapter. If this
is the only format that postgresql will send back for time with
timezone, then this seems like a great addition. If you create a
lighthouse ticket and attach a patch, people using other versions of
postgresql can verify it makes sense.