Python Path().stat().st_时区?

Python Path().stat().st_时区?,python,datetime,timezone,python-3.8,stat,Python,Datetime,Timezone,Python 3.8,Stat,我正在尝试使用Cygwin中的Python3.8获取文件的上次修改时间 因此,如果我做了stat.profile我会得到: File: .profile Size: 1236 Blocks: 4 IO Block: 65536 regular file Device: 46e61a95h/1189485205d Inode: 8162774324632653 Links: 1 Access: (0755/-rwxr-xr-x) Uid:

我正在尝试使用Cygwin中的Python3.8获取文件的上次修改时间

因此,如果我做了
stat.profile
我会得到:

  File: .profile
  Size: 1236            Blocks: 4          IO Block: 65536  regular file
Device: 46e61a95h/1189485205d   Inode: 8162774324632653  Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (197609/   pepol)   Gid: (197609/   pepol)
Access: 2020-09-14 15:16:04.773101900 +0700
Modify: 2020-09-14 15:15:21.977809000 +0700
Change: 2020-09-14 15:16:04.055602500 +0700
 Birth: 2020-09-14 15:16:04.052652900 +0700
但是如果我尝试使用Python获取文件的时间戳:

从pathlib导入路径
从日期时间导入日期时间
p1=路径(“.profile”)
p1st=p1.stat()
dts=datetime.fromtimestamp(p1st.st\u mtime)
打印(str(dts))
我得到了这个“天真”(无时区)的结果:

现在我感到困惑的是:

  • stat
    输出所示,我的时区是UTC+07:00
  • 我的国家没有DST
  • Windows的时区设置正确
  • 15:15:21.977809000+0700
    相当于
    08:15:21.977809000+0000

为什么
pathlib.Path().stat()
获取的时间戳比UTC时间戳提前了1小时?它实际使用的时区是什么?

确保在
cygwin
中使用cygwin的Python。您可以通过
$which python3
检查cygwin使用的Python版本。应该返回,例如
/usr/bin/python3

  • 如果在Cygwin中使用Windows Python安装,它将无法正确确定计算机的时区(操作系统设置)(Windows Python配置为在Windows上执行此操作,而不是在Unix环境中,反之亦然)

旁注,由于
pathlib.Path().stat()
返回的时间戳是POSIX时间戳,因此您可以使用例如
datetime.fromtimestamp(p1st.st_mtime,tz=timezone.utc)
直接获得utc。

您使用哪种Python,cygwin还是Windows?在cygwin中,
echo$TZ
是否显示正确的时区?旁注:
pathlib.Path().stat()
返回的时间戳是POSIX时间戳,因此您可以使用
datetime.fromtimestamp(p1st.st\u mtime,tz=timezone.utc)
直接获得utc。@MrFuppes holy bejeebus,可能就是这样!我用
检查了它,它指向Windows的Python<代码>echo$TZ
已经显示了正确的时区,顺便说一句。谢谢你的提示@MrFuppes更新:设置
tz=timezone.utc
正确返回utc时间戳,耶!如果您可以将您的提示作为答案张贴在此处,我将很高兴将其标记为解决方案。相关:
2020-09-14 09:15:21.977809