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

[Validate]