Class Sequel::Postgres::IntervalDatabaseMethods::Parser
In: lib/sequel/extensions/pg_interval.rb
Parent: Object

Creates callable objects that convert strings into ActiveSupport::Duration instances.

Methods

call  

Constants

PARSER = /\A([+-]?\d+ years?\s?)?([+-]?\d+ mons?\s?)?([+-]?\d+ days?\s?)?(?:(?:([+-])?(\d{2,10}):(\d\d):(\d\d(\.\d+)?))|([+-]?\d+ hours?\s?)?([+-]?\d+ mins?\s?)?([+-]?\d+(\.\d+)? secs?\s?)?)?\z/o   Regexp that parses the full range of PostgreSQL interval type output.

Public Instance methods

Parse the interval input string into an ActiveSupport::Duration instance.

[Source]

     # File lib/sequel/extensions/pg_interval.rb, line 73
 73:         def call(string)
 74:           raise(InvalidValue, "invalid or unhandled interval format: #{string.inspect}") unless matches = PARSER.match(string)
 75: 
 76:           value = 0
 77:           parts = []
 78: 
 79:           if v = matches[1]
 80:             v = v.to_i
 81:             value += 31557600 * v
 82:             parts << [:years, v]
 83:           end
 84:           if v = matches[2]
 85:             v = v.to_i
 86:             value += 2592000 * v
 87:             parts << [:months, v]
 88:           end
 89:           if v = matches[3]
 90:             v = v.to_i
 91:             value += 86400 * v
 92:             parts << [:days, v]
 93:           end
 94:           if matches[5]
 95:             seconds = matches[5].to_i * 3600 + matches[6].to_i * 60
 96:             seconds += matches[8] ? matches[7].to_f : matches[7].to_i
 97:             seconds *= -1 if matches[4] == '-'
 98:             value += seconds
 99:             parts << [:seconds, seconds]
100:           elsif matches[9] || matches[10] || matches[11]
101:             seconds = 0
102:             if v = matches[9]
103:               seconds += v.to_i * 3600
104:             end
105:             if v = matches[10]
106:               seconds += v.to_i * 60
107:             end
108:             if v = matches[11]
109:               seconds += matches[12] ? v.to_f : v.to_i
110:             end
111:             value += seconds
112:             parts << [:seconds, seconds]
113:           end
114: 
115:           ActiveSupport::Duration.new(value, parts)
116:         end

[Validate]