解决python time.strftime("%X")打印时区乱码的问题
针对
import time
print(time.strftime('%Z'))
打印出中国时区乱码问题的解决方案
**
一.解决方法
在 print(time.strftime("%X")) 之前插入
import ctypes, locale
locale.setlocale(locale.LC_ALL, '')
ctypes.cdll.ucrtbase._tzset()
二.例子
在这里我需要打印出现在的时区,代码如下:
# 解决中国·时区乱码问题
import time
import ctypes, locale
print(locale.setlocale(locale.LC_ALL, ''))
print(ctypes.cdll.ucrtbase._tzset())
print(time.strftime('%Z'))
print(time.strftime('%Z %Y-%m-%d %H:%M:%S'))
结果如下
Chinese (Simplified)_China.936
0
中国标准时间
中国标准时间 2020-12-01 22:25:58
ok问题解决
三.寻找解决方案的过程
国内网站搜索很长时间搜索不出来,无奈在谷歌浏览器里搜索,我看到了github社区同样在问这样的问题。我将它分享出来
链接: https://github.com/neolee/pilot/issues/768.
之后我看到了这样的回复:
链接: https://bugs.python.org/issue16322.
进入链接,通过大致的浏览,我得到了最关键的信息:
这里就是代码的由来
四.后记
感谢技术大佬的无私付出和默默奉献,让我们这些小白少走了很多弯路。
同时感谢国内外的技术人员无私的技术分享。
上述缘由便是我写博客的目的。
如有不足,欢迎留言