Class: HumanTime
- Inherits:
-
Object
- Object
- HumanTime
- Defined in:
- lib/humantime.rb
Constant Summary collapse
- SECOND =
1
- MINUTE =
60
- HOUR =
3600
- DAY =
86400
- WEEK =
604800
- MONTH =
2629743
- YEAR =
31556926
Class Method Summary collapse
-
.between(start_time, end_time, options = {}) ⇒ Object
Output the difference betwen two integer / DateTime / Time values.
-
.output(int, options = {}) ⇒ 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, options = {}) ⇒ Object
Output the difference betwen two integer / DateTime / Time values
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/humantime.rb', line 135 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, options = {}) ⇒ Object
Parse an integer and return the string representation of a time
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/humantime.rb', line 12 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') ) || [:round_to] == MONTH months = int / MONTH int = int - ( months * MONTH ) display.delete 'minutes' display.delete 'seconds' display.delete 'hours' if [:round_to] == MONTH months = months.round + 1 int = 0 end 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')) || [:round_to] == DAY days = int / DAY int = int - ( days * DAY ) display.delete 'minutes' display.delete 'seconds' if [:round_to] == DAY days = days.round + 1 int = 0 end 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') ) || [:round_to] == HOUR hours = int / HOUR int = int - ( hours * HOUR ) display.delete 'seconds' if [:round_to] == HOUR hours = hours.round + 1 int = 0 end if hours == 1 str << "#{hours} hour" else str << "#{hours} hours" end end # Minutes if ( int < HOUR && int > ( MINUTE - 1 ) && display.include?('minutes') ) || [:round_to] == MINUTE mins = int / MINUTE int = int - ( mins * MINUTE ) if [:round_to] == MINUTE mins = mins.round + 1 int = 0 end if mins == 1 str << "#{mins} minute" else str << "#{mins} 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
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/humantime.rb', line 151 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 |