Python中的datetime.fromtimestamp(os.path.getctime())给我一个时区感知值? [英] Does datetime.fromtimestamp(os.path.getctime()) in Python give me a timezone-aware value?

查看:1928
本文介绍了Python中的datetime.fromtimestamp(os.path.getctime())给我一个时区感知值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python 3.4中使用这样的调用:

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))

根据 x 和 y 具有时区感知功能。 org / 3.4 / library / datetime.htmlrel =nofollow>在datetime文档中定义该术语?这在平台之间有所不同吗?我认为在理论上,文件的ctime和mtime是从UTC时代以来的秒数来衡量的,所以答案应该是肯定的?

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?

如果是,那么所有/大多数POSIX平台都是真的吗?具体来说,在现代Linux / OS X上是否正确?

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

如果没有,是否有更好的处理方法?如何获取时区感知时间和时间数据?平台用于表达/存储ctime和mtime的时区是什么?

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?

推荐答案

在OSX上,至少, os.path.getctime 在系统时区返回TZ-naive datetime。

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 将为您提供本地时区偏移(以秒为单位),不计入DST pytz 图书馆可能对您非常有用。

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.

这篇关于Python中的datetime.fromtimestamp(os.path.getctime())给我一个时区感知值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆