Skip to content

Commit

Permalink
fix problem with handling time of 00:00
Browse files Browse the repository at this point in the history
  • Loading branch information
maurice.aubrey committed Dec 29, 2007
1 parent 49ef67f commit 572c1bb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ USAGE:

Authors: Nick Muerdter (original code)
Maurice Aubrey <maurice.aubrey+12hour@gmail.com>

Home Page:
http://code.google.com/p/rails-twelve-hour-time-plugin/
23 changes: 15 additions & 8 deletions lib/12_hour_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,16 @@ module ActionView::Helpers::DateHelper
def select_hour_with_ampm(datetime, options = {})
options[:twelve_hour] or return select_hour_without_ampm(datetime, options)

val = ''
if datetime
val = datetime.kind_of?(Fixnum) ? datetime : datetime.hour
val -= 12 if val > 12
end
hour = _12_hour(datetime)

if options[:use_hidden]
return hidden_html(options[:field_name] || 'hour', val, options)
return hidden_html(options[:field_name] || 'hour', hour, options)
end

hour_options = []
1.upto(12) do |hour|
1.upto(12) do |val|
selected = (hour == val) ? ' selected="selected"' : ''
hour_options << %(<option value="#{leading_zero_on_single_digits(hour)}"#{selected}>#{leading_zero_on_single_digits(hour)}</option>\n)
hour_options << %(<option value="#{leading_zero_on_single_digits(val)}"#{selected}>#{leading_zero_on_single_digits(val)}</option>\n)
end

select_html(options[:field_name] || 'hour', hour_options, options)
Expand Down Expand Up @@ -73,6 +69,17 @@ def select_time_with_ampm(datetime = Time.now, options = {})
end

alias_method_chain :select_time, :ampm

private

def _12_hour(datetime)
return '' if datetime.blank?

hour = datetime.kind_of?(Fixnum) ? datetime : datetime.hour
hour -= 12 if hour > 12

return hour
end
end

class ActionView::Helpers::InstanceTag
Expand Down
8 changes: 8 additions & 0 deletions test/12_hour_time_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ class TwelveHourTimeTest < Test::Unit::TestCase
include ActionView::Helpers::FormOptionsHelper
include ActionView::Helpers::DateHelper

def test_24_to_12_hour
assert_equal(12, _12_hour(0), "12 AM")
assert_equal(1, _12_hour(1), "1 AM")
assert_equal(12, _12_hour(12), "12 PM")
assert_equal(1, _12_hour(13), "1 PM")
assert_equal(12, _12_hour(24), "12 AM")
end

def test_select_hour_12
time = Time.parse '2005-07-27 15:03:34'

Expand Down

0 comments on commit 572c1bb

Please sign in to comment.