Module: DateHelper
- Included in:
- StackFu::Commands::ListCommand
- Defined in:
- lib/stackfu/date_helper.rb
Instance Method Summary collapse
- #display_time_in_words(hash, include_seconds = false, options = {}) ⇒ Object
- #distance_of_time(seconds, options = {}) ⇒ Object
- #distance_of_time_hash(distance, from_time = nil, to_time = nil, options = {}) ⇒ Object
- #distance_of_time_in_percent(from_time, current_time, to_time, options = {}) ⇒ Object
- #distance_of_time_in_words(from_time, to_time, include_seconds = false, options = {}) ⇒ Object
- #distance_of_time_in_words_hash(from_time, to_time, options = {}) ⇒ Object
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, = {}) .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. (![:except].nil? && [:except].include?(key)) || # keep the keys we only want. ([:only] && ![:only].include?(key)) end .delete(:except) .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 [:precision] output = output[0...[:precision]] .delete(:precision) end output.to_sentence() end |
#distance_of_time(seconds, options = {}) ⇒ Object
59 60 61 |
# File 'lib/stackfu/date_helper.rb', line 59 def distance_of_time(seconds, = {}) display_time_in_words(distance_of_time_hash(seconds), ) 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, ={}) 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, = {}) [:precision] ||= 0 distance = to_time - from_time number_with_precision(((current_time - from_time) / distance) * 100, ).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, = {}) return old_distance_of_time_in_words(from_time, to_time, include_seconds, ) if .delete(:vague) hash = distance_of_time_in_words_hash(from_time, to_time, ) display_time_in_words(hash, include_seconds, ) 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, ={}) 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, ) end |