Module: Time24

Defined in:
jekyll/_plugins/time24.rb

Overview

convert input from HH:MMPM or HH:MMAM to 24h to nearest even interval of minutes eg. convertTo24up(“03:05PM”,30) -> 15.5 eg. convertTo24up(“03:05PM”,15) -> 15.25 eg. convertTo24down(“03:05PM”,30) -> 15

Instance Method Summary collapse

Instance Method Details

#convertTo24down(input, interval) ⇒ Object



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
# File 'jekyll/_plugins/time24.rb', line 62

def convertTo24down(input,interval)

  if input.nil?
      Jekyll.logger.warn "time was nil"
      return nil
  end

  pm = false
  if input.include? "AM"    
      input.gsub("AM", "")
  elsif input.include? "PM" 
      input.gsub("PM", "")
      pm = true
  else
      Jekyll.logger.warn "time missing AM or PM #{ '%s' % input}"
      return nil
  end
  
  if !input.include? ":"
      Jekyll.logger.warn "time missing colon #{ '%s' % input}"
      return nil
  end
  
  parts = input.split(":")
  
  hour = parts[0].to_i 
  if (hour == 12)
      hour = 0
  end
  if pm
      hour+=12
  end
  
  minutes = parts[1].to_i
  
  if (minutes % interval != 0)
      minutes = ((minutes / interval).floor * interval).to_i;
      if (minutes < 0)
          minutes = 0
      end    
  end
  
  hour24 = hour.to_f + (minutes.to_f / 60.0)
  return (hour24)    
  
end

#convertTo24up(input, interval) ⇒ Object



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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'jekyll/_plugins/time24.rb', line 6

def convertTo24up(input,interval)

  if input.nil?
      Jekyll.logger.warn "time was nil"
      return nil
  end


  pm = false
  if input.include? "AM"    
      input.gsub("AM", "")
  elsif input.include? "PM" 
      input.gsub("PM", "")
      pm = true
  else
      Jekyll.logger.warn "time missing AM or PM #{ '%s' % input}"
      return nil
  end
  
  if !input.include? ":"
      Jekyll.logger.warn "time missing colon #{ '%s' % input}"
      return nil
  end
  
  parts = input.split(":")
  
  hour = parts[0].to_i 
  if (hour == 12)
      hour = 0
  end
  if pm
      hour+=12
  end
  
  minutes = parts[1].to_i
  
 # Jekyll.logger.warn "up time conversion #{ '%s %d %d' % [input,hour,minutes]}"
  
  if (minutes % interval != 0)
      minutes = ( (1 + (minutes / interval).floor) * interval).to_i;
      if (minutes >= 60)
          minutes = 0
          hour += 1
      end    
  end
  
  if (hour >= 24)
      hour -= 24
  end
  
 # Jekyll.logger.warn "      up time rounding #{ '%s %d %d' % [input,hour,minutes]}"

  hour24 = hour.to_f + (minutes.to_f / 60.0)
  return (hour24)        
end