Ruby 的 Time.strftime %P 选项不起作用?

标签 ruby

我正在构建一个 Rails 应用程序并且遇到了一些看起来很奇怪的行为。根据Ruby's documentation of Time.strftime() , %P 和 %p 是有效的选项:

%p - Meridian indicator (``AM''  or  ``PM'')
%P - Meridian indicator (``am''  or  ``pm'')

使用 Rails 控制台(Rails 3.0.3,ruby 1.8.7(2009-06-12 补丁级别 174)[universal-darwin10.0])我观察到以下行为:

>> DateTime.now.strftime("%l:%M%P on %A %e %Y")
=> " 2:23pm on Tuesday  1 2011"
>> Time.now.strftime("%l:%M%P on %A %e %Y")
=> " 2:23P on Tuesday  1 2011"
>> Time.now.strftime("%l:%M%p on %A %e %Y")
=> " 2:23PM on Tuesday  1 2011"
>> 2.hours.ago.strftime("%l:%M%P on %A %e %Y")
=> "11:29P on Monday 28 2011"

请注意在 DateTime.now.strftime 中,%P 的计算结果是预期的小写字母 pm。使用 Time.now.strftime %P 呈现为大写 P。

最后一个例子使用了 ActiveSupport::TimeWithZone 类,它也呈现不正确(2 小时前是早上)。

这是预期的行为吗?或者我应该在某个地方提交错误,如果是这样,最好的地方是哪里?

最佳答案

在 irb 中使用 1.9.2-p180:

Welcome to IRB. You are using ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.6.0]. Have fun ;)
>> Time.now.strftime('%p %P') #=> "PM pm"
>> DateTime.now.strftime('%p %P') #=> "PM pm"

在 irb 中使用 1.8.7:

Welcome to IRB. You are using ruby 1.8.7 (2011-02-18 patchlevel 334) [x86_64-darwin10.6.0]. Have fun ;)
>> Time.now.strftime('%p %P') #=> "PM P"
>> DateTime.now.strftime('%p %P') #=> "PM pm"

Rubydoc.info不显示对 1.8.7 的“%P”支持:

Format meaning:

  %a - The abbreviated weekday name (``Sun'')
  %A - The  full  weekday  name (``Sunday'')
  %b - The abbreviated month name (``Jan'')
  %B - The  full  month  name (``January'')
  %c - The preferred local date and time representation
  %d - Day of the month (01..31)
  %H - Hour of the day, 24-hour clock (00..23)
  %I - Hour of the day, 12-hour clock (01..12)
  %j - Day of the year (001..366)
  %m - Month of the year (01..12)
  %M - Minute of the hour (00..59)
  %p - Meridian indicator (``AM''  or  ``PM'')
  %S - Second of the minute (00..60)
  %U - Week  number  of the current year,
          starting with the first Sunday as the first
          day of the first week (00..53)
  %W - Week  number  of the current year,
          starting with the first Monday as the first
          day of the first week (00..53)
  %w - Day of the week (Sunday is 0, 0..6)
  %x - Preferred representation for the date alone, no time
  %X - Preferred representation for the time alone, no date
  %y - Year without a century (00..99)
  %Y - Year with century
  %Z - Time zone name
  %% - Literal ``%'' character

   t = Time.now
   t.strftime("Printed on %m/%d/%Y")   #=> "Printed on 04/09/2003"
   t.strftime("at %I:%M%p")            #=> "at 08:56AM"

ActiveSupport 的 ri 文档说:

ri ActiveSupport::TimeWithZone#strftime
----------------------------------- ActiveSupport::TimeWithZone#strftime
     strftime(format)
------------------------------------------------------------------------
     Replaces +%Z+ and +%z+ directives with +zone+ and
     +formatted_offset+, respectively, before passing to Time#strftime,
     so that zone information is correct

关于Ruby 的 Time.strftime %P 选项不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5149584/

相关文章:

ruby-on-rails - 在 Rails 上运行自定义 rake 任务

ruby-on-rails - rails 中的手机号码服务提供商?

ruby-on-rails - 如何解决 NotIdentifiedByImageMagickError?

javascript - 如何在 Watir 中引用 JavaScript 工具提示

ruby - 并发::Promise.all?不起作用

sql - 在 Rails 和 Postgresql 中查找带空格和不带空格的单词

ruby - 使用 SimpleForm 上传 CarrierWave;为什么 "remote_image_url"和 "image"保存到同一数据库列?

ruby-on-rails - 如何为模型地理编码设置 VCR 全局磁带?

ruby-on-rails - 在 RubyOnRails 应用程序中生成图形

ruby - enum#feed 如何在 ruby​​ 中工作?