OK so I wrote these two functions (and I did not even unit test them, so they're probably wrong):

gist.github.com/glyph/ad25a9e8

There must be a better way to do this with the standard library, right? Something buried in `datetime` somewhere?

@glyph closest I can think of (via strftime.org/)
>>> datetime.time(13,0,0).strftime('%I %p or %-I %p')
'01 PM or 1 PM'
>>> time.strptime('1 PM', '%I %p').tm_hour
13

%-1 (without zero padding) is platform specific
%p is localised by locale

Follow

@alex @glyph I didn’t think you need the .time there, right?

@pganssle @glyph I don't follow. In that code I used "import datetime; import time", I didn't use "from datetime import ...".

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.