Module: TimeDateHelpers::TimeHelpers
- Defined in:
- lib/time_date_helpers/time_helpers.rb
Instance Method Summary collapse
- #humanize_time(time, opt = {}) ⇒ Object
- #round_hours(start_time, end_time, opt = {}) ⇒ Object
- #round_minutes(time, opt = {}) ⇒ Object
Instance Method Details
#humanize_time(time, opt = {}) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/time_date_helpers/time_helpers.rb', line 73 def humanize_time(time, opt={}) # Set the default options = {:ampm => true, :with_seconds => false} # Merge whatever options the user has selected with the defaults .merge!(opt) # Make sure what is passed is legit return nil if time.nil? return nil unless time.class == Time if [:ampm] [:with_seconds] ? time.strftime("%I:%M:%S %P") : time.strftime("%I:%M %P") else [:with_seconds] ? time.strftime("%H:%M:%S") : time.strftime("%H:%M") end end |
#round_hours(start_time, end_time, opt = {}) ⇒ Object
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 |
# File 'lib/time_date_helpers/time_helpers.rb', line 38 def round_hours(start_time, end_time, opt={}) # Set the default options = {:direction => :up, :increment => 15} # Merge whatever options the user has selected with the defaults .merge!(opt) # make sure that increment is a Integer between 1 and 59 return nil unless [:increment].class == Integer return nil if ([:increment] > 59 || [:increment] < 1) # Set up some local variables used in calculations new_start_min, new_end_min, hr_adj = start_time.min, end_time.min, 0 total_segments = (60.0/[:increment]).ceil # Need to round up closest_segment_down = (new_min/[:increment]) closest_segment_up = (new_min/[:increment]) + 1 # Now it is time to actually adjust the minutes (and perhaps hour) if (new_min - [:increment]*closest_segment_down) == 0 new_min # we are on an increment, so just return the minutes elsif [:direction] == :up # if rounding up, need to check if up against the end-of-the-hour limit if closest_segment_up == total_segments new_min = 00; hr_adj=1 # near end of hour, so move up to next hour else new_min = [:increment]*(closest_segment_up) end else # we are rounding down, which is very easy new_min = [:increment]*(closest_segment_down) end # Finally, return the adjusted time Time.new(time.year, time.month, time.day, (time.hour+hr_adj), new_min, 0) end |
#round_minutes(time, opt = {}) ⇒ Object
3 4 5 6 7 8 9 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 |
# File 'lib/time_date_helpers/time_helpers.rb', line 3 def round_minutes(time, opt={}) # Set the default options = {:direction => :up, :increment => 15} # Merge whatever options the user has selected with the defaults .merge!(opt) # make sure that increment is a Integer between 1 and 59 return nil unless [:increment].class == Integer return nil if ([:increment] > 59 || [:increment] < 1) # Set up some local variables used in calculations new_min, hr_adj = time.min, 0 total_segments = (60.0/[:increment]).ceil # Need to round up closest_segment_down = (new_min/[:increment]) closest_segment_up = (new_min/[:increment]) + 1 # Now it is time to actually adjust the minutes (and perhaps hour) if (new_min - [:increment]*closest_segment_down) == 0 new_min # we are on an increment, so just return the minutes elsif [:direction] == :up # if rounding up, need to check if up against the end-of-the-hour limit if closest_segment_up == total_segments new_min = 00; hr_adj=1 # near end of hour, so move up to next hour else new_min = [:increment]*(closest_segment_up) end else # we are rounding down, which is very easy new_min = [:increment]*(closest_segment_down) end # Finally, return the adjusted time Time.new(time.year, time.month, time.day, (time.hour+hr_adj), new_min, 0) end |