Class: ActiveRecord::ConnectionAdapters::CockroachDB::PostgresqlInterval::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/connection_adapters/cockroachdb/oid/interval.rb

Constant Summary collapse

PARTS =
ActiveSupport::Duration::PARTS
PARTS_IN_SECONDS =
ActiveSupport::Duration::PARTS_IN_SECONDS
REGEX =
/\A([+-]?\d+ years?\s?)?([+-]?\d+ mons?\s?)?([+-]?\d+ days?\s?)?(?:([+-])?(\d{2,10}):(\d\d):(\d\d(\.\d+)?))?\z/

Class Method Summary collapse

Class Method Details

.parse(string) ⇒ Object

Raises:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# 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