Skip to main content

A small catch in Python documentation (strftime).

Every developer knows datetime.strftime() method, but some don't know that its argument's format is not documented in full in the official documentation.

For example, you won't find any mention of %s directive there which means UNIX epoch time, timestamp. It works (at least with every major version of Python on Linux, FreeBSD, and MacOS), I've tested it a few times myself, it's just not documented.

I recently saw a piece of code where developers, being unaware of such a directive, used custom format string with both and time.time(), a function generator, and on top of it messed with model migrations (it all was about FileField.upload_to if you know what I'm talking about) when they could just use %s.

From other undocumented features you might be interested in these:

%C is a century (%Y//100), could be helpful for complimenting %y or something, I guess.

%u is a weekday number with Monday as 1 and Sunday as 7 (as opposed to %w representing Sunday as 0).

And padding/case modifiers (specifications are for reference only):

%_d (pad with blank),
%-H (no padding),
%0e (pad with zeros),
%^A (uppercase),
%#Z (effectively lowercase for %Z, not useful with anything else).

(Note: They were introduced by GNU extension but work on BSD/MacOS and other modern systems.)

I'm not writing documentation here, but for the sake of completeness, here is the list of all specifications supported by python library on major platforms but not mentioned in Python docs:

%C %D %e %F %G %g %h %k %l %n %R %r %s %T %t %u %V (and modifiers %_* %-* %0* %^* %#* %E* %O*)

Most of them are just shortcuts (like %F, %T, or %D) or simply useless (like %V, %G, %g stuff) but please refer to strftime(3) or standards for explanations.

Popular posts from this blog

Russia becomes an internet outcast for some reason

It's starting to happen more and more often to me. I'm trying to use some service and all of a sudden I see an error page, sometimes a custom one but more often generated by cloudflare or a similar service, with error message effectively saying that IP addresses from Russia are banned on the service. And what is even more interesting, most of this services don't have any particular policy reasons not to allow Russian users, it's not not like they are publishing prescribed materials or doing anything interesting whatsoever. A payment processor, a popular online course provider, some random forums and information sites, one site with educational videos that I like (that one is especially funny because I'm banned from the subscription page and so can't pay them but their cdn does't care and I can actually download any video I want). But why does it happen? Is there some recommendation in some popular security guideline "block Russia, Cambodia, and Afghan

ORICO — small things that stand out

More than once I've written about stuff made by Big Brand manufacturers so I decided to balance things out by writing about a mostly-unknown but good things. In short, if you need something small like HDD enclosure or some cable or USB hub or a dongle of some kind, check if ORICO makes it and chances are you won't be disappointed. It's usually slightly more expensive than low-end stuff from aliexpress but, in my opinion, it's usually worth it. Disclaimer. This post is a shameless promotion of their brand although, unfortunately, nobody paid me for it or even sent me any free stuff. Guys from ORICO and not, if you read this, I'll gladly review whatever free goodies you send me :) I only do honest reviews so better make sure to send the good stuff. At this point I own a USB hub, three different 2.5" HDD enclosures, and a precision screwdriver set from them. And probably something else I don't remember. Here are the pics: USB hub in its natural habitat

Terrible experience with bluetooth headsets and what to do about it

If you own a Bluetooth A2DP headset and any kind of personal computer, good chance is you tried connecting them together. Isn't it nice to watch movies and listen to music free from cluttering wires? Well, good chance is you hated that experience and if that was the reason for buying the headset, you might've even considered throwing it away (or using it only with your phone). Why do they suck so much? Everything points to the fact that it's not a hardware problem. The same headset most likely will work flawlessly paired with an android device or even the same computer under a different operating system (windows users report huge difference between, like, 8.0 and 8.1, and not in favor of the latter, surprisingly, I think only mac users report good stuff about their macbooks and beats although I didn't try it myself so they might all be faking it :) ). And most likely it's not even the drivers or other low-level stuff, android and desktop linux has mostly the same