6

我正在使用红宝石。

我试图弄清楚如何转换以下内容..

14变成2 pm

0变成12 am

23变成11 pm

谢谢!

4

3 回答 3

14
[14, 0, 23].map { |hour| Time.parse("#{hour}:00").strftime("%l %P") }
=> [" 2 pm", "12 am", "11 pm"]
于 2013-03-10T23:10:55.563 回答
5

我喜欢@Mori 的回答,因为它让 Time 完成了繁重的工作,但使用 Benchmark 我们可以看到哪个 CPU 最密集:

这就是我写的:

def am_pm(hour)
  meridian = (hour >= 12) ? 'pm' : 'am'
  hour = case hour
        when 0, 12
          12
        when 13 .. 23
          hour - 12
        else
          hour
        end

  "#{ hour } #{ meridian }"
end

基准:

require 'benchmark'
require 'time'

def mori(hour)
  Time.parse("#{hour}:00").strftime("%l %P")
end

def convert h
  p, l = h.divmod(12)
  "#{l.zero? ? 12 : l} #{p.zero? ? "a" : "p"}m"
end

def am_pm(hour)
  meridian = (hour >= 12) ? 'pm' : 'am'
  hour = case hour
        when 0, 12
          12
        when 13 .. 23
          hour - 12
        else
          hour
        end

  "#{ hour } #{ meridian }"
end

def hour_24_to_12(hour_24)
  hour_12 = hour_24 - 12

  if hour_12 == 0
    return "12 pm"
  elsif hour_12 == -12
    return "12 am"
  elsif hour_12 > 0
    return "#{hour_12} pm"
  elsif hour_12 < 0
    return "#{hour_24} am"
  else
    # The passed-in time is outside the range o 0-23,
    # so you may need to deal with this however you
    # feel is appropriate
  end
end

HRS = [0, 14, 23]
N = 100_000

puts RUBY_VERSION

HRS.each { |hr| puts "am_pm:         #{ am_pm(hr)         }" }
HRS.each { |hr| puts "convert:       #{ convert(hr)       }" }
HRS.each { |hr| puts "hour_24_to_12: #{ hour_24_to_12(hr) }" }
HRS.each { |hr| puts "mori:          #{ mori(hr)          }" }

Benchmark.bm(13) do |b|
  b.report('am_pm')         { N.times { HRS.each { |h| am_pm(h)         } } }
  b.report('convert')       { N.times { HRS.each { |h| convert(h)       } } }
  b.report('hour_24_to_12') { N.times { HRS.each { |h| hour_24_to_12(h) } } }
  b.report('mori')          { N.times { HRS.each { |h| mori(h)          } } }
end

哪个输出:

1.9.3
am_pm:         12 am
am_pm:         2 pm
am_pm:         11 pm
convert:       12 am
convert:       2 pm
convert:       11 pm
hour_24_to_12: 12 am
hour_24_to_12: 2 pm
hour_24_to_12: 11 pm
mori:          12 am
mori:           2 pm
mori:          11 pm
                    user     system      total        real
am_pm           1.230000   0.000000   1.230000 (  1.228159)
convert         1.280000   0.000000   1.280000 (  1.286672)
hour_24_to_12   0.640000   0.000000   0.640000 (  0.642993)
mori           20.940000   0.020000  20.960000 ( 21.003620)

同样的测试只在 Ruby 2.0-p0 下运行:

2.0.0
am_pm:         12 am
am_pm:         2 pm
am_pm:         11 pm
convert:       12 am
convert:       2 pm
convert:       11 pm
hour_24_to_12: 12 am
hour_24_to_12: 2 pm
hour_24_to_12: 11 pm
mori:          12 am
mori:           2 pm
mori:          11 pm
                    user     system      total        real
am_pm           0.440000   0.000000   0.440000 (  0.433303)
convert         0.410000   0.000000   0.410000 (  0.412183)
hour_24_to_12   0.200000   0.000000   0.200000 (  0.201894)
mori            9.910000   0.010000   9.920000 (  9.921940)
于 2013-03-11T05:27:34.357 回答
2
def convert h
  p, l = h.divmod(12)
  "#{l.zero? ? 12 : l} #{p.zero? ? "a" : "p"}m"
end
convert(14) # => "2 pm"
convert(0)  # => "12 am"
convert(23) # => "11 pm"
于 2013-03-10T23:19:09.617 回答