将字符串时间戳(带时区偏移)转换为本地时间。 。 ?蟒蛇 [英] Convert string timestamp (with timezone offset) to local time. . ? python

查看:149
本文介绍了将字符串时间戳(带时区偏移)转换为本地时间。 。 ?蟒蛇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字符串时间戳转换为正确的datetime对象。我遇到的问题是有时区偏移,我所做的一切似乎都不起作用。

I am trying to convert a string timestamp into a proper datetime object. The problem I am having is that there is a timezone offset and everything I am doing doesn't seem to work.

最终我想将字符串时间戳转换为日期时间对象在我的机器时区。

Ultimately I want to convert the string timestamp into a datetime object in my machines timezone.

# string timestamp     
date = u"Fri, 16 Jul 2010 07:08:23 -0700"


推荐答案

dateutil 包适用于解析日期/时间:

The dateutil package is handy for parsing date/times:

In [10]: date = u"Fri, 16 Jul 2010 07:08:23 -0700"

In [11]: from dateutil.parser import parse

In [12]: parse(date)
Out[12]: datetime.datetime(2010, 7, 16, 7, 8, 23, tzinfo=tzoffset(None, -25200))

最后,要转换为您当地的时区,

Finally, to convert into your local timezone,

In [13]: parse(date).astimezone(YOUR_LOCAL_TIMEZONE)

这篇关于将字符串时间戳(带时区偏移)转换为本地时间。 。 ?蟒蛇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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