Class: Time

Inherits:
Object
  • Object
show all
Defined in:
lib/time_diff.rb

Class Method Summary collapse

Class Method Details

.create_formatted_component_hash(components, formatted_intervals) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/time_diff.rb', line 35

def self.create_formatted_component_hash(components, formatted_intervals)
  formatted_components = {}
  index = 0
  components.each do |component|
    formatted_components[:"#{formatted_intervals[index]}"] = component
    index = index + 1
  end
  formatted_components
end

.diff(start_date, end_date, format_string = '%y, %M, %w, %d and %h:%m:%s') ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/time_diff.rb', line 6

def self.diff(start_date, end_date, format_string='%y, %M, %w, %d and %h:%m:%s')
  #I18n.load_path += Dir.glob("lib/*.yml")
  start_time = start_date.to_time if start_date.respond_to?(:to_time)
  end_time = end_date.to_time if end_date.respond_to?(:to_time)
  distance_in_seconds = ((end_time - start_time).abs).round

  components = get_time_diff_components(%w(year month week day hour minute second), distance_in_seconds)
  time_diff_components = {:year => components[0], :month => components[1], :week => components[2], :day => components[3], :hour => components[4], :minute => components[5], :second => components[6]}

  formatted_intervals = get_formatted_intervals(format_string)
  components = get_time_diff_components(formatted_intervals, distance_in_seconds)
  formatted_components = create_formatted_component_hash(components, formatted_intervals)
  format_string = remove_format_string_for_zero_components(formatted_components, format_string)
  time_diff_components[:diff] = format_date_time(formatted_components, format_string) unless format_string.nil?
  return time_diff_components
end

.format_date_time(time_diff_components, format_string) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/time_diff.rb', line 55

def Time.format_date_time(time_diff_components, format_string)
  format_string.gsub!('%y', "#{time_diff_components[:year]} #{pluralize('year', time_diff_components[:year])}") if time_diff_components[:year] 
  format_string.gsub!('%M', "#{time_diff_components[:month]} #{pluralize('month', time_diff_components[:month])}") if time_diff_components[:month]
  format_string.gsub!('%w', "#{time_diff_components[:week]} #{pluralize('week', time_diff_components[:week])}") if time_diff_components[:week]
  format_string.gsub!('%d', "#{time_diff_components[:day]} #{pluralize('day', time_diff_components[:day])}") if time_diff_components[:day]
  format_string.gsub!('%H', "#{time_diff_components[:hour]} #{pluralize('hour', time_diff_components[:hour])}") if time_diff_components[:hour]
  format_string.gsub!('%N', "#{time_diff_components[:minute]} #{pluralize('minute', time_diff_components[:minute])}") if time_diff_components[:minute]
  format_string.gsub!('%S', "#{time_diff_components[:second]} #{pluralize('second', time_diff_components[:second])}") if time_diff_components[:second]
  format_string.gsub!('%h', format_digit(time_diff_components[:hour]).to_s) if time_diff_components[:hour]
  format_string.gsub!('%m', format_digit(time_diff_components[:minute]).to_s) if time_diff_components[:minute]
  format_string.gsub!('%s', format_digit(time_diff_components[:second]).to_s) if time_diff_components[:second]
  format_string
end

.format_digit(number) ⇒ Object



86
87
88
# File 'lib/time_diff.rb', line 86

def Time.format_digit(number)
  return '%02d' % number
end

.get_formatted_intervals(format_string) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/time_diff.rb', line 23

def self.get_formatted_intervals(format_string)
  intervals = []
  intervals << 'year' if format_string.include?('%y')
  intervals << 'month' if format_string.include?('%M')
  intervals << 'week' if format_string.include?('%w')
  intervals << 'day' if format_string.include?('%d')
  intervals << 'hour' if format_string.include?('%h') || format_string.include?('%H')
  intervals << 'minute' if format_string.include?('%m') || format_string.include?('%N')
  intervals << 'second' if format_string.include?('%s') || format_string.include?('%S')
  intervals
end

.get_time_diff_components(intervals, distance_in_seconds) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/time_diff.rb', line 45

def self.get_time_diff_components(intervals, distance_in_seconds)
  components = []
  intervals.each do |interval|
      component = (distance_in_seconds / 1.send(interval)).floor
      distance_in_seconds -= component.send(interval)
      components << component
  end
  components
end

.pluralize(word, count) ⇒ Object



69
70
71
# File 'lib/time_diff.rb', line 69

def Time.pluralize(word, count)
  return count != 1 ? I18n.t(word.pluralize, :default => word.pluralize) : I18n.t(word, :default =>  word)
end

.remove_format_string_for_zero_components(time_diff_components, format_string) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/time_diff.rb', line 73

def Time.remove_format_string_for_zero_components(time_diff_components, format_string)
  format_string.gsub!('%y, ','') if time_diff_components[:year] == 0
  format_string.gsub!('%M, ','') if time_diff_components[:month] == 0
  format_string.gsub!('%w, ','') if time_diff_components[:week] == 0
  if format_string.slice(0..1) == '%d'
    format_string.gsub!('%d ','') if time_diff_components[:day] == 0
  else
    format_string.gsub!(', %d','') if time_diff_components[:day] == 0
  end
  format_string.slice!(0..3) if format_string.slice(0..3) == 'and ' 
  format_string
end