Параметр Ruby's Time.strftime% P не работает? - PullRequest
3 голосов
/ 01 марта 2011

Я создаю приложение rails и столкнулся с тем, что кажется странным поведением. В соответствии с документацией Ruby по 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 patchlevel 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 соответствует ожидаемому значению в нижнем регистре. С Time.now.strftime% P отображается в верхнем регистре P.

В последнем примере используется класс ActiveSupport :: TimeWithZone, который тоже отображает неправильно (2 часа назад утром).

Это ожидаемое поведение? Или я должен где-нибудь сообщить об ошибке, и если да, то где лучше всего это сделать?

1 Ответ

2 голосов
/ 01 марта 2011

С 1.9.2-p180 в irb:

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"

С 1.8.7 в irb:

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 не показывает поддержку "% P" для 1.8.7:

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...