Compressing the string format for time

Trying to write the date in a format I like. Currently, I found by mistake that this works:

datetime.now(timezone.utc).strftime("%Y%m%dT%TZ")
'20241207T11:57:37Z'

Note that Im not specifying %H:%M:%S (but I could)

The %T does that instead, but the option isn’t documented afaik.

But it was somewhat unexpected that %T does the same.

Apparently the reason could be:

The full set of format codes supported varies across platforms, because Python calls the platform C library’s strftime() function, and platform variations are common.

  • So my question is: do we have a crossplatform way to specify it in a single character? I don’t fully understand the details.

Currently, I do: date = datetime.now(timezone.utc).strftime("%Y_%m_%d_T%H_%M_%SZ") but I don’t like it very much.

I think its a docs bug. You could raise a bug ticket reporting this.

2 Likes

Agreed. %T is defined by both the ISO C standard and the POSIX standard. It should be as supported as any other specifier listed in the Python documentation.

2 Likes

Thanks, I submitted an issue/question to the docs repo.

1 Like