python timestamp,Python中的datetime.fromtimestamp(os.path.getctime())给我一个时区感知值?...

本文探讨了如何在Python中使用os.path.getctime及os.path.getmtime获取文件创建时间和修改时间,并讨论了这些时间戳是否为时区感知的问题。通过示例展示了在OSX系统下获取到的时间戳是非时区感知的,并提供了进一步处理时区问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I am using calls such as this in Python 3.4:

x = datetime.fromtimestamp(os.path.getctime(somefilename))

y = datetime.fromtimestamp(os.path.getmtime(somefilename))

Will x and y be timezone-aware, as per the definition of that term in the datetime documentation? Does this vary between platforms? I assume that in theory the ctime and mtime of a file are measured against the seconds since the epoch in UTC, so the answer should be yes?

If so, is that true across all/most POSIX platforms? Specifically, is it true on modern Linux/OS X?

If not, is there a better way to handle this? How can I get timezone-aware ctime and mtime data? What timezone do platforms use for expressing/storing ctime and mtime?

解决方案

On OSX, at least, os.path.getctime returns a TZ-naive datetime in the system's timezone.

$ date

Mon Jun 8 15:08:40 PDT 2015

$ touch new_file

$ python

>>> from datetime import datetime

>>> import os

>>> datetime.fromtimestamp(os.path.getctime('new_file'))

datetime.datetime(2015, 6, 8, 15, 8, 42)

>>> print datetime.fromtimestamp(os.path.getctime('new_file')).tzinfo

None

time.timezone will give you the local timezone offset in seconds, not accounting for DST. The pytz library will probably be very useful to you.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值