views:

178

answers:

5

I'm still a bit slow with Python, so I haven't got this figured out beyond what's obviously in the docs, etc.

I've worked with Django a bit, where they've added some datetime formatting options via template tags, but in regular python code how can I get the 12-hour hour without a leading zero?

Is there a straightforward way to do this? I'm looking at the 2.5 and 2.6 docs for "strftime()" and there doesn't seem to be a formatting option there for this case.

Should I be using something else?

Feel free to include any other time-formatting tips that aren't obvious from the docs. =)

+1  A: 

I know it's pretty cheap, but you could just discard the first character if it's a zero :)

genpfault
And do forget about calling `lower()`, too!
Gabe
I prefer to leave the case as whatever the locale says it should be. ^_^
Mike DeSimone
+5  A: 

Nothing built-in to datetime will do it. You'll need to use something like:

datetime.time(1).strftime('%I:%M%p').lstrip('0')
Mike DeSimone
Accepted as the most "straightforward". My other favorite solution would be to use an external formatting library, like Django's DateFormat.
anonymous coward
+1  A: 

datetime.time objects expose the hour, minute and second fields. Making your own formatting with these is pretty trivial. Something like this:

return "%d:%02d %s" % (foo.hour % 12 + 0 if foo.hour % 12 else 12, #ugh
                       foo.minute,
                       "pm" if foo.hour >= 12 else "am")
badp
Don't downvote me if I got the AM/PM bit wrong, I never use this notation :)
badp
You got the AM/PM thing right, but for the first format parameter, if `foo.hour` is 0 or 12, the output hour should be `12`, not `0`. Things like this make me hate 12-hour time.
Mike DeSimone
Would you like `(foo.hour - 1) % 12 + 1` better? They both look ugly to me.
Mike DeSimone
Sorry about the 12-hour time bit. It is a hassle, but in particular this is for the lower level humans. I agree that it s-u-x.
anonymous coward
+1  A: 

Use %l to get the hour as a number between 1..12:

In [2]: datetime.time(hour=14,minute=35).strftime('%l:%M%p')
Out[2]: ' 2:35PM'

For more format codes, see http://au2.php.net/strftime.

unutbu
I get `':35PM'` on Windows with CPython 2.5.2. It does work on Cygwin though (also 2.5.2).
Brian Neal
I think I'd heard of that, and while I didn't specify that the solution should be cross platform - that certainly would be great. So this leaves a space at the beginning?
anonymous coward
+1  A: 

While I'm partial to Mike DeSimone's answer, for voting purposes I think this might be a worthwhile contribution...

The Django project contains a "PHP Compatible" date formatting class in django/utils/dateformat.py (trunk). It's used like so (shell example):

>>> import datetime
>>> from django.utils.dateformat import DateFormat
>>> d = datetime.datetime.now()
>>> df =  DateFormat(d)
>>> df.format('g:ia') # Format for Hour-no-leading-0, minutes, lowercase 'AM/PM'
u'9:10a.m.'

It fulfills the requirement here, and may be worth including in your project. With that, I'll say that you should verify the license permits such use... Any comments to clarify are welcome.

anonymous coward