Class: HumanTime
- Inherits:
-
Object
- Object
- HumanTime
- Defined in:
- lib/humantime.rb
Constant Summary collapse
- SECOND =
1- MINUTE =
60- HOUR =
3600- DAY =
86400- MONTH =
2629743- YEAR =
31556926
Class Method Summary collapse
-
.between(start_time, end_time) ⇒ Object
Output the difference betwen two integer / DateTime / Time values.
-
.output(int) ⇒ Object
Parse an integer and return the string representation of a time.
Instance Method Summary collapse
-
#mappings ⇒ Object
From github.com/hpoydar/chronic_duration For future parsing methods.
Class Method Details
.between(start_time, end_time) ⇒ Object
Output the difference betwen two integer / DateTime / Time values
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/humantime.rb', line 114 def self.between start_time, end_time et = end_time.to_i st = start_time.to_i if et > st diff = et - st elsif st > et diff = st - et else diff = 0 end self.output diff end |
.output(int) ⇒ Object
Parse an integer and return the string representation of a time
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 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/humantime.rb', line 11 def self.output int str = [] display = ['years', 'months', 'days', 'hours', 'minutes', 'seconds'] return "0 seconds" if int == 0 # Years if int > ( YEAR - 1 ) && display.include?('years') years = int / YEAR int = int - ( years * YEAR ) display.delete 'minutes' display.delete 'seconds' display.delete 'hours' if years.round == 1 str << "#{years.round} year" else str << "#{years.round} years" display.delete('months') end end # Months if int < YEAR && int > ( MONTH - 1 ) && display.include?('months') months = int / MONTH int = int - ( months * MONTH ) display.delete 'minutes' display.delete 'seconds' display.delete 'hours' if months.round == 1 str << "#{months.round} month" elsif months.round == 12 str << "1 year" else str << "#{months.round} months" display.delete 'days' end end # Days if int < MONTH && int > ( DAY - 1 ) && display.include?('hours') days = int / DAY int = int - ( days * DAY ) display.delete 'minutes' display.delete 'seconds' if days.round == 1 str << "#{days.round} day" else # Display weeks, because we're cool like that if days.round % 7 == 0 weeks = days.round / 7 if weeks == 1 str << "1 week" else str << "#{weeks} weeks" end else str << "#{days.round} days" end display.delete 'hours' end end # Hours if int < DAY && int > ( HOUR - 1 ) && display.include?('hours') hours = int / HOUR int = int - ( hours * HOUR ) display.delete 'seconds' if hours.round == 1 str << "#{hours.round} hour" else str << "#{hours.round} hours" end end # Minutes if int < HOUR && int > ( MINUTE - 1 ) && display.include?('minutes') mins = int / MINUTE int = int - ( mins * MINUTE ) if mins.round == 1 str << "#{mins.round} minute" else str << "#{mins.round} minutes" end end # Seconds if int < MINUTE && int > 0 && display.include?('seconds') if int.round == 1 str << "#{int.round} second" else str << "#{int.round} seconds" end end str.join(' ') end |
Instance Method Details
#mappings ⇒ Object
From github.com/hpoydar/chronic_duration For future parsing methods
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/humantime.rb', line 130 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 |