Module: DateHelper

Included in:
StackFu::Commands::ListCommand
Defined in:
lib/stackfu/date_helper.rb

Instance Method Summary collapse

Instance Method Details

#display_time_in_words(hash, include_seconds = false, options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/stackfu/date_helper.rb', line 63

def display_time_in_words(hash, include_seconds = false, options = {})
  options.symbolize_keys!
  hash.delete(:seconds) if !include_seconds && hash[:minutes]
  # Remove all the values that are nil.
  time_measurements = ["years",
                       "months",
                       "weeks",
                       "days",
                       "hours",
                       "minutes",
                       "seconds" ].delete_if do |key|
    hash[key].nil? || hash[key].zero? || 
    # Remove the keys that we don't want.
    (!options[:except].nil? && options[:except].include?(key)) ||
    # keep the keys we only want.
    (options[:only] && !options[:only].include?(key))
  end
  
  options.delete(:except)
  options.delete(:only)
  output = []
  time_measurements.each do |key|
    name = hash[key] > 1 ? key : key.singularize
    output += ["#{hash[key]} #{name}"]
  end
  
  # maybe only grab the first few values
  if options[:precision]
    output = output[0...options[:precision]]
    options.delete(:precision)
  end


  output.to_sentence(options)
end

#distance_of_time(seconds, options = {}) ⇒ Object



59
60
61
# File 'lib/stackfu/date_helper.rb', line 59

def distance_of_time(seconds, options = {})
  display_time_in_words(distance_of_time_hash(seconds), options)
end

#distance_of_time_hash(distance, from_time = nil, to_time = nil, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
# File 'lib/stackfu/date_helper.rb', line 10

def distance_of_time_hash(distance, from_time = nil, to_time = nil, options={})
  output = HashWithIndifferentAccess.new
  from_time ||= Time.now
  to_time ||= from_time + distance.seconds
  while distance > 0
    if distance < 1.minute
      output["seconds"] = distance.to_i
      distance = 0
    elsif distance < 1.hour
      output["minutes"], distance = distance.divmod(1.minute)
    elsif distance < 1.day
      output["hours"], distance = distance.divmod(1.hour)
    elsif distance < 28.days
      output["days"], distance = distance.divmod(1.day)
      # Time has to be greater than a month
    else
      smallest, largest = from_time < to_time ? [from_time, to_time] : [to_time, from_time]   
    
      months = (largest.year - smallest.year) * 12 + (largest.month - smallest.month)
      years, months = months.divmod(12)
    
      days = largest.day - smallest.day
      
      # Will otherwise incorrectly say one more day if our range goes over a day. 
      days -= 1 if largest.hour < smallest.hour
    
      if days < 0
        # Convert the last month to days and add to total
        months -= 1
        last_month = largest.advance(:months => -1)
        days += Time.days_in_month(last_month.month, last_month.year)
      end
    
      if months < 0
        # Convert a year to months
        years -= 1
        months += 12
      end
    
      output["years"] = years
      output["months"] = months
      output["days"] = days
    
      total_days, distance = distance.divmod(1.day)
    end
  end
  output
end

#distance_of_time_in_percent(from_time, current_time, to_time, options = {}) ⇒ Object



105
106
107
108
109
# File 'lib/stackfu/date_helper.rb', line 105

def distance_of_time_in_percent(from_time, current_time, to_time, options = {})
  options[:precision] ||= 0
  distance = to_time - from_time
  number_with_precision(((current_time - from_time) / distance) * 100, options).to_s + "%"
end

#distance_of_time_in_words(from_time, to_time, include_seconds = false, options = {}) ⇒ Object



99
100
101
102
103
# File 'lib/stackfu/date_helper.rb', line 99

def distance_of_time_in_words(from_time, to_time, include_seconds = false, options = {})
  return old_distance_of_time_in_words(from_time, to_time, include_seconds, options) if options.delete(:vague)
  hash = distance_of_time_in_words_hash(from_time, to_time, options)
  display_time_in_words(hash, include_seconds, options)
end

#distance_of_time_in_words_hash(from_time, to_time, options = {}) ⇒ Object



2
3
4
5
6
7
8
# File 'lib/stackfu/date_helper.rb', line 2

def distance_of_time_in_words_hash(from_time, to_time, options={})
  from_time = from_time.to_time if from_time.respond_to?(:to_time)
  to_time = to_time.to_time if to_time.respond_to?(:to_time)
  
  distance = (from_time - to_time).abs
  distance_of_time_hash(distance, from_time, to_time, options)
end