class ActiveRecord::ConnectionAdapters::CockroachDB::PostgresqlInterval::Parser

Constants

PARTS
PARTS_IN_SECONDS
REGEX

modified regex from github.com/jeremyevans/sequel/blob/master/lib/sequel/extensions/pg_interval.rb#L86

Public Class Methods

parse(string) click to toggle source
# File lib/active_record/connection_adapters/cockroachdb/oid/interval.rb, line 72
def self.parse(string)
  matches = REGEX.match(string)
  raise(ParseError) unless matches

  # 1 => years, 2 => months, 3 => days, 4 => nil, 5 => hours,
  # 6 => minutes, 7 => seconds with fraction digits, 8 => fractional portion of 7
  duration = 0
  parts = {}

  if matches[1]
    val = matches[1].to_i
    duration += val * PARTS_IN_SECONDS[:years]
    parts[:years] = val
  end

  if matches[2]
    val = matches[2].to_i
    duration += val * PARTS_IN_SECONDS[:months]
    parts[:months] = val
  end

  if matches[3]
    val = matches[3].to_i
    duration += val * PARTS_IN_SECONDS[:days]
    parts[:days] = val
  end

  if matches[5]
    val = matches[5].to_i
    duration += val * PARTS_IN_SECONDS[:hours]
    parts[:hours] = val
  end

  if matches[6]
    val = matches[6].to_i
    duration += val * PARTS_IN_SECONDS[:minutes]
    parts[:minutes] = val
  end

  if matches[7]
    val = matches[7].to_f
    duration += val * PARTS_IN_SECONDS[:seconds]
    parts[:seconds] = val
  end

  ActiveSupport::Duration.new(duration, parts)
end