如何从Python获取UTC格式的文件修改日期

如何从Python获取UTC格式的文件修改日期,python,Python,我想从Python中获取文件的UTC修改日期。下面的代码返回我的Linux配置时区(GMT-5)中的日期。我要UTC的。或者如何配置操作系统的时区以使用pytz进行转换 $ python Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>

我想从Python中获取文件的UTC修改日期。下面的代码返回我的Linux配置时区(GMT-5)中的日期。我要UTC的。或者如何配置操作系统的时区以使用pytz进行转换

$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> import os
>>> dt=os.path.getmtime('/home/user/.bashrc')
>>> datetime.datetime.fromtimestamp(dt)
datetime(2012, 5, 30, 21, 18, 10)
谢谢

试试看


用你当地的时区来代替“美国/纽约”,下面应该可以做到这一点

In [11]: import datetime, pytz

In [12]: right_now = datetime.datetime.now()

In [13]: right_now_utc = right_now.replace(tzinfo=pytz.timezone("America/New_York")).astimezone(pytz.utc)

In [14]: right_now
Out[14]: datetime.datetime(2012, 7, 9, 20, 31, 21, 999536)

In [15]: right_now_utc
Out[15]: datetime.datetime(2012, 7, 10, 1, 31, 21, 999536, tzinfo=<UTC>)
[11]中的
:导入日期时间,pytz
[12]中:right\u now=datetime.datetime.now()
在[13]中:right\u now\u utc=right\u now.replace(tzinfo=pytz.timezone(“美国/纽约”)).astimezone(pytz.utc)
在[14]:现在
Out[14]:datetime.datetime(2012,7,9,20,31,21999536)
在[15]:现在是utc
Out[15]:datetime.datetime(2012,7,10,1,31,21999536,tzinfo=)
In [11]: import datetime, pytz

In [12]: right_now = datetime.datetime.now()

In [13]: right_now_utc = right_now.replace(tzinfo=pytz.timezone("America/New_York")).astimezone(pytz.utc)

In [14]: right_now
Out[14]: datetime.datetime(2012, 7, 9, 20, 31, 21, 999536)

In [15]: right_now_utc
Out[15]: datetime.datetime(2012, 7, 10, 1, 31, 21, 999536, tzinfo=<UTC>)