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  

Public Instance methods

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

[Source]

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

[Validate]