为什么“%-d”或“%-e”会删除前导 space 或零?

Why does "%-d", or "%-e" remove the leading space or zero?

关于 SO 问题 904928 (Python strftime - date without leading 0?) Ryan 回答:

Actually I had the same problem and I realised that, if you add a hyphen between the % and the letter, you can remove the leading zero.

For example %Y/%-m/%-d.

我遇到了同样的问题,这是一个很好的解决方案,但是,为什么会这样?

>>> import datetime
>>> datetime.datetime(2015, 3, 5).strftime('%d')
'05'

>>> datetime.datetime(2015, 3, 5).strftime('%-d')
'5'

# It also works with a leading space
>>> datetime.datetime(2015, 3, 5).strftime('%e')
' 5'

>>> datetime.datetime(2015, 3, 5).strftime('%-e')
'5'

# Of course other numbers doesn't get stripped
>>> datetime.datetime(2015, 3, 15).strftime('%-e')
'15'

我找不到任何相关文件? -> python datetime docs / python string operations

这似乎在 windows 机器上不起作用,好吧,我不使用 windows,但知道为什么它不起作用会很有趣吗?

Python datetime.strftime() delegates to C strftime() function that is platform-dependent:

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. To see the full set of format codes supported on your platform, consult the strftime(3) documentation.

Glibc notes for strftime(3):

- (dash) Do not pad a numeric result string.

我的 Ubuntu 机器上的结果:

>>> from datetime import datetime
>>> datetime.now().strftime('%d')
'07'
>>> datetime.now().strftime('%-d')
'7'