如何将时区偏移量的时间戳字符串转换为本地时间? [英] How can I convert a timestamp string with timezone offset to local time?

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

问题描述

我正在尝试将字符串时间戳转换为正确的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 = "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天全站免登陆