#
# there are few ways to rid yourself of rails' insistence that datetime
# selects use 24 hr time - but this is a simple one.  just drop this code into
# ./app/helpers/application_helper.rb and your entire app will use am/pm in
# the selects.  note that the method still posts exactly the same values (24
# hr time) so you don't have to change a line of controller code to accept the
# values posted.
#

module ActionView
  module Helpers
    module DateHelper
      def select_hour(datetime, options = {})
        val = datetime ? (datetime.kind_of?(Fixnum) ? datetime : datetime.hour) : ''
        if options[:use_hidden]
          hidden_html(options[:field_name] || 'hour', val, options)
        else
          hour_options = []
          0.upto(23) do |hour|
            unit = hour < 12 ? :am : :pm
            selected = "selected='selected'" if val == hour
            value = leading_zero_on_single_digits hour
            hr = leading_zero_on_single_digits( unit == :am ? hour : (hour - 12) )
            hr = '12' if hr == '00'
            hour_options << %(<option value="#{ value }" #{ selected }>#{ hr }#{ unit }</option>\n)
          end
          select_html(options[:field_name] || 'hour', hour_options, options)
        end
      end
    end
  end
end