module ChronicDuration

Public Class Methods

raise_exceptions() click to toggle source
# File lib/chronic_duration.rb, line 10
def self.raise_exceptions
  !!@@raise_exceptions
end
raise_exceptions=(value) click to toggle source
# File lib/chronic_duration.rb, line 14
def self.raise_exceptions=(value)
  @@raise_exceptions = !!value
end

Public Instance Methods

decimal_places(number) click to toggle source
# File lib/chronic_duration.rb, line 85
def decimal_places number
  number.to_s.split('.').last.length if number.is_a? Float
end
output(seconds, opts = {}) click to toggle source

Given an integer and an optional format, returns a formatted string representing elapsed time

# File lib/chronic_duration.rb, line 28
def output(seconds, opts = {})

  opts[:format] ||= :default
  unit_of_measures = [:years, :months, :days, :hours, :minutes]
  unit_of_measures << :seconds unless opts[:hide_seconds]

  duration = Duration.new seconds

  joiner = ' '
  process = nil

  case opts[:format]
  when :micro
    dividers = {
      :years => 'y', :months => 'm', :days => 'd', :hours => 'h', :minutes => 'm', :seconds => 's' }
    joiner = ''
  when :short
    dividers = {
      :years => 'y', :months => 'm', :days => 'd', :hours => 'h', :minutes => 'm', :seconds => 's' }
  when :default
    dividers = {
      :years => ' yr', :months => ' mo', :days => ' day', :hours => ' hr', :minutes => ' min', :seconds => ' sec',
      :pluralize => true }
  when :long
    dividers = {
      :years => ' year', :months => ' month', :days => ' day', :hours => ' hour', :minutes => ' minute', :seconds => ' second',
      :pluralize => true }
  when :chrono
    dividers = {
      :years => ':', :months => ':', :days => ':', :hours => ':', :minutes => ':', :seconds => ':', :keep_zero => true }
    process = lambda do |str|
      # Pad zeros
      # Get rid of lead off times if they are zero
      # Get rid of lead off zero
      # Get rid of trailing :
      str.gsub(/\b\d\b/) { |d| ("%02d" % d) }.gsub(/^(00:)+/, '').gsub(/^0/, '').gsub(/:$/, '')
    end
    joiner = ''
  end

  result = []
  unit_of_measures.each do |t|
    num = duration.send t
    num = ("%.#{decimal_places seconds }f" % num) if num.is_a?(Float) && t == :seconds
    result << humanize_time_unit( num, dividers[t], dividers[:pluralize], dividers[:keep_zero] )
  end

  result = result.join(joiner).squeeze(' ').strip

  if process
    result = process.call(result)
  end

  result.length == 0 ? nil : result

end
parse(string, opts = {}) click to toggle source

Given a string representation of elapsed time, return an integer (or float, if fractions of a second are input)

# File lib/chronic_duration.rb, line 21
def parse(string, opts = {})
  result = calculate_from_words(cleanup(string), opts)
  result == 0 ? nil : result
end

Private Instance Methods

calculate_from_words(string, opts) click to toggle source
# File lib/chronic_duration.rb, line 136
def calculate_from_words(string, opts)
  val = 0
  words = string.split(' ')
  words.each_with_index do |v, k|
    if v =~ float_matcher
      val += (convert_to_number(v) * duration_units_seconds_multiplier(words[k + 1] || (opts[:default_unit] || 'seconds')))
    end
  end
  val
end
cleanup(string) click to toggle source
# File lib/chronic_duration.rb, line 147
def cleanup(string)
  res = string.downcase
  res = filter_by_type(Numerizer.numerize(res))
  res = res.gsub(float_matcher) {|n| " #{n} "}.squeeze(' ').strip
  res = filter_through_white_list(res)
end
convert_to_number(string) click to toggle source
# File lib/chronic_duration.rb, line 154
def convert_to_number(string)
  string.to_f % 1 > 0 ? string.to_f : string.to_i
end
duration_units_list() click to toggle source
# File lib/chronic_duration.rb, line 158
def duration_units_list
  %w(seconds minutes hours days weeks months years)
end
duration_units_seconds_multiplier(unit) click to toggle source
# File lib/chronic_duration.rb, line 161
def duration_units_seconds_multiplier(unit)
  return 0 unless duration_units_list.include?(unit)
  case unit
  when 'years';   31536000 # doesn't accounts for leap years
  when 'months';  3600 * 24 * 30
  when 'weeks';   3600 * 24 * 7
  when 'days';    3600 * 24
  when 'hours';   3600
  when 'minutes'; 60
  when 'seconds'; 1
  end
end
error_message() click to toggle source
# File lib/chronic_duration.rb, line 174
def error_message
  'Sorry, that duration could not be parsed'
end
filter_by_type(string) click to toggle source

Parse 3:41:59 and return 3 hours 41 minutes 59 seconds

# File lib/chronic_duration.rb, line 179
def filter_by_type(string)
  if string.gsub(' ', '') =~ /#{float_matcher}(:#{float_matcher})+/
    res = []
    string.gsub(' ', '').split(':').reverse.each_with_index do |v,k|
      return unless duration_units_list[k]
      res << "#{v} #{duration_units_list[k]}"
    end
    res = res.reverse.join(' ')
  else
    res = string
  end
  res
end
filter_through_white_list(string) click to toggle source

Get rid of unknown words and map found words to defined time units

# File lib/chronic_duration.rb, line 199
def filter_through_white_list(string)
  res = []
  string.split(' ').each do |word|
    if word =~ float_matcher
      res << word.strip
      next
    end
    stripped_word = word.strip.gsub(/^,/, '').gsub(/,$/, '')
    if mappings.has_key?(stripped_word)
      res << mappings[stripped_word]
    elsif !join_words.include?(stripped_word) and ChronicDuration.raise_exceptions
      raise DurationParseError, "An invalid word #{word.inspect} was used in the string to be parsed."
    end
  end
  res.join(' ')
end
float_matcher() click to toggle source
# File lib/chronic_duration.rb, line 193
def float_matcher
  /[0-9]*\.?[0-9]+/
end
humanize_time_unit(number, unit, pluralize, keep_zero) click to toggle source
# File lib/chronic_duration.rb, line 128
def humanize_time_unit(number, unit, pluralize, keep_zero)
  return '' if number == 0 && !keep_zero
  res = "#{number}#{unit}"
  # A poor man's pluralizer
  res << 's' if !(number == 1) && pluralize
  res
end
join_words() click to toggle source
# File lib/chronic_duration.rb, line 250
def join_words
  ['and', 'with', 'plus']
end
mappings() click to toggle source
# File lib/chronic_duration.rb, line 216
def mappings
  {
    'seconds' => 'seconds',
    'second'  => 'seconds',
    'secs'    => 'seconds',
    'sec'     => 'seconds',
    's'       => 'seconds',
    'minutes' => 'minutes',
    'minute'  => 'minutes',
    'mins'    => 'minutes',
    'min'     => 'minutes',
    'm'       => 'minutes',
    'hours'   => 'hours',
    'hour'    => 'hours',
    'hrs'     => 'hours',
    'hr'      => 'hours',
    'h'       => 'hours',
    'days'    => 'days',
    'day'     => 'days',
    'dy'      => 'days',
    'd'       => 'days',
    'weeks'   => 'weeks',
    'week'    => 'weeks',
    'w'       => 'weeks',
    'months'  => 'months',
    'mos'     => 'months',
    'month'   => 'months',
    'years'   => 'years',
    'year'    => 'years',
    'yrs'     => 'years',
    'y'       => 'years'
  }
end
white_list() click to toggle source
# File lib/chronic_duration.rb, line 254
def white_list
  self.mappings.map {|k, v| k}
end