Module: ChronicDuration

Extended by:
ChronicDuration
Included in:
ChronicDuration
Defined in:
lib/chronic_duration.rb

Defined Under Namespace

Classes: Duration, DurationParseError

Constant Summary collapse

@@raise_exceptions =
false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.raise_exceptionsObject



10
11
12
# File 'lib/chronic_duration.rb', line 10

def self.raise_exceptions
  !!@@raise_exceptions
end

.raise_exceptions=(value) ⇒ Object



14
15
16
# File 'lib/chronic_duration.rb', line 14

def self.raise_exceptions=(value)
  @@raise_exceptions = !!value
end

Instance Method Details

#decimal_places(number) ⇒ Object



85
86
87
# 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 = {}) ⇒ Object

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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 = {}) ⇒ Object

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



21
22
23
24
# File 'lib/chronic_duration.rb', line 21

def parse(string, opts = {})
  result = calculate_from_words(cleanup(string), opts)
  result == 0 ? nil : result
end