Eri*_*itt 9 python windows time timezone localtime
当Python在Windows下运行时,如果在Python实例的生命周期内更改时区,则time.localtime不会报告正确的时间.在Linux下,总是可以运行time.tzset来缓解这样的问题,但在Windows中似乎没有等效的.
有没有办法解决这个问题而不做一些荒谬的事情,哦,我不知道......
#!/bin/env python
real_localtime = eval(subprocess.Popen(
["python","-c", "import time;repr(time.localtime())"],
stdout=subprocess.PIPE).communicate()[0])
Run Code Online (Sandbox Code Playgroud)
一个更合理的解决方案是将Kernel32的GetLocalTime与pywin32或ctypes一起使用。任何时区变化都会立即反映出来。
import ctypes
class SYSTEMTIME(ctypes.Structure):
_fields_ = [
('wYear', ctypes.c_int16),
('wMonth', ctypes.c_int16),
('wDayOfWeek', ctypes.c_int16),
('wDay', ctypes.c_int16),
('wHour', ctypes.c_int16),
('wMinute', ctypes.c_int16),
('wSecond', ctypes.c_int16),
('wMilliseconds', ctypes.c_int16)]
SystemTime = SYSTEMTIME()
lpSystemTime = ctypes.pointer(SystemTime)
ctypes.windll.kernel32.GetLocalTime(lpSystemTime)
print SystemTime.wHour, SystemTime.wMinute
Run Code Online (Sandbox Code Playgroud)