Ruby's Time.strftime %P option doesn't work? -
i'm building rails application , have come across seems strange behaviour. according ruby's documentation of time.strftime(), %p , %p valid options:
%p - meridian indicator (``am'' or ``pm'') %p - meridian indicator (``am'' or ``pm'')
using rails console (rails 3.0.3, ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]) observe following behaviour:
>> 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"
note how in datetime.now.strftime, %p evaluates expected lowercase pm. time.now.strftime %p rendered uppercase p.
the final example uses activesupport::timewithzone class, , renders incorrectly (2 hours ago in morning).
is expected behaviour? or should file bug somewhere, , if so, where's best place so?
with 1.9.2-p180 in irb:
welcome irb. 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"
with 1.8.7 in irb:
welcome irb. 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 shows no "%p" support 1.8.7:
format meaning: %a - abbreviated weekday name (``sun'') %a - full weekday name (``sunday'') %b - abbreviated month name (``jan'') %b - full month name (``january'') %c - preferred local date , time representation %d - day of month (01..31) %h - hour of day, 24-hour clock (00..23) %i - hour of day, 12-hour clock (01..12) %j - day of year (001..366) %m - month of year (01..12) %m - minute of hour (00..59) %p - meridian indicator (``am'' or ``pm'') %s - second of minute (00..60) %u - week number of current year, starting first sunday first day of first week (00..53) %w - week number of current year, starting first monday first day of first week (00..53) %w - day of week (sunday 0, 0..6) %x - preferred representation date alone, no time %x - preferred representation time alone, no date %y - year without century (00..99) %y - year 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's ri
doc says:
ri activesupport::timewithzone#strftime ----------------------------------- activesupport::timewithzone#strftime strftime(format) ------------------------------------------------------------------------ replaces +%z+ , +%z+ directives +zone+ , +formatted_offset+, respectively, before passing time#strftime, zone information correct
Comments
Post a Comment