Module: TimeDateHelpers::TimeHelpers

Defined in:
lib/time_date_helpers/time_helpers.rb

Instance Method Summary collapse

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
  options = {:ampm => true, :with_seconds => false}
  # Merge whatever options the user has selected with the defaults
  options.merge!(opt)
  # Make sure what is passed is legit
  return nil if time.nil? 
  return nil unless time.class == Time
  if options[:ampm]
    options[:with_seconds] ? time.strftime("%I:%M:%S %P") : time.strftime("%I:%M %P")
  else
    options[: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
  options = {:direction => :up, :increment => 15}
  # Merge whatever options the user has selected with the defaults
  options.merge!(opt)

  # make sure that increment is a Integer between 1 and 59
  return nil unless options[:increment].class == Integer
  return nil if (options[:increment] > 59 || options[: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/options[:increment]).ceil # Need to round up
  closest_segment_down = (new_min/options[:increment])
  closest_segment_up = (new_min/options[:increment]) + 1

  # Now it is time to actually adjust the minutes (and perhaps hour)
  if (new_min - options[:increment]*closest_segment_down) == 0
    new_min  # we are on an increment, so just return the minutes
  elsif options[: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 = options[:increment]*(closest_segment_up)
    end
  else
    # we are rounding down, which is very easy
    new_min = options[: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
  options = {:direction => :up, :increment => 15}
  # Merge whatever options the user has selected with the defaults
  options.merge!(opt)

  # make sure that increment is a Integer between 1 and 59
  return nil unless options[:increment].class == Integer
  return nil if (options[:increment] > 59 || options[:increment] < 1)

  # Set up some local variables used in calculations
  new_min, hr_adj = time.min, 0
  total_segments = (60.0/options[:increment]).ceil # Need to round up
  closest_segment_down = (new_min/options[:increment])
  closest_segment_up = (new_min/options[:increment]) + 1

  # Now it is time to actually adjust the minutes (and perhaps hour)
  if (new_min - options[:increment]*closest_segment_down) == 0
    new_min  # we are on an increment, so just return the minutes
  elsif options[: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 = options[:increment]*(closest_segment_up)
    end
  else
    # we are rounding down, which is very easy
    new_min = options[: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