Time parsing issue

<% if schedule.date_scheduled.strftime('%I:%M %p') == '12:00 AM' %>
  <td class="leftAlign">TBA</td>
<% else %>
  <td class="leftAlign"><%=h schedule.date_scheduled.strftime('%I:%M
%p') %></td>
<% end %>

Works fine - datetime formats that are say "2009-06-30 17:30:00" will
show up as:
=> 05:30 PM

But if I change this to a 12-hour clock without the leading 0 using

schedule.date_scheduled.strftime('%l:%M %p')
=>

.. nothing..

If I'm not using the correct formatting string for a 12-hour clock
without the leading 0, which is the correct output?

Thanks.

<% if schedule.date_scheduled.strftime('%I:%M %p') == '12:00 AM' %>
<td class="leftAlign">TBA</td>
<% else %>
<td class="leftAlign"><%=h schedule.date_scheduled.strftime('%I:%M
%p') %></td>
<% end %>

Works fine - datetime formats that are say "2009-06-30 17:30:00" will
show up as:
=> 05:30 PM

But if I change this to a 12-hour clock without the leading 0 using

I don't understand, when you say 'you change this to a 12-hour clock'
what is it that you have changed? The value in schedule.date_scheduled
or what?

Colin

schedule.date_scheduled.strftime('%I:%M %p')
=> 05:30 PM

schedule.date_scheduled.strftime('%l:%M %p')
=> nothing...

.. it should equal

schedule.date_scheduled.strftime('%l:%M %p')
=> 5:30 PM

I'm trying to remove the leading 0 off the hours slot...

schedule.date_scheduled.strftime('%I:%M %p')
=> 05:30 PM

schedule.date_scheduled.strftime('%l:%M %p')
=> nothing...

I thought I must be going blind as in gmail in FF the two lines above
look identical, it was only when I copied and pasted into my editor in
order to do a compare that I realised the first on is %<upper case i>
and the second is %<lower case L>. I don't see lower case L in the
docs for strftime, which may explain the problem.

This link has some possibly helpful suggestions for removing the
leading zero: http://snippets.dzone.com/posts/show/2952

Colin

I had been looking at:

http://www.nullislove.com/2007/05/16/time-for-strftime/

Thanks for that, a very useful link.

If you scroll down towards the bottom, there's a very large table that
contains the formats. I was trying to follow the one for 12-hour
without the leading number.

I guess this means I have to use gsub..

Interestingly, in irb:
irb(main):006:0> Time.now.strftime("%I:%M %p") # upper case i
=> "03:39 PM"
irb(main):007:0> Time.now.strftime("%l:%M %p") # lower case l
=> " 3:39 PM"

What class is your schedule.date_scheduled? (use
schedule.date_scheduled.class to find out)

Colin

Could it be that something on the way to the browser chokes on the leading whitespace? Maybe try the code in script/console, and continue adding stuff around your strftime(...) until you find the culprit.

Regards,

Felix

NM..

This actually does work:

schedule.date_scheduled.strftime('%I:%M %p').gsub(/0?(\d):/,'\1:')

I had to place it in another place because I forgot I was looking for a
conditional. The %l does not work and it may just be ruby version
related. I'm sure that 1.9 came out with new time features and maybe
that's part of them..

It is ok for me on ruby 1.8.7, rails 2.3.2.

Colin

ripple:~ hassan$ ruby -v
ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]
ripple:~ hassan$ irb
irb(main):001:0> Time.now.strftime("%I:%M %p") # u/c I as in India
=> "08:46 AM"
irb(main):002:0> Time.now.strftime("%l:%M %p") # l/c l as in Lima
=> " 8:46 AM"
irb(main):003:0>

FWIW,

I already solved this using gsub.

But, just to follow-through with this - I'm not lying when I did my
checks:

irb(main):001:0> Time.now.strftime("%I:%M %p")
=> "12:57 PM"
irb(main):002:0> Time.now.strftime("%l:%M %p")
=> ""
irb(main):003:0>

l = lower case (l)ima (l)ime (l)azy (l)ucky..

Again, it does not compute with my IRB and I'm using:

ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]