获取GMT时间给定日期和UTC偏移量在python [英] Get the GMT time given date and UTC offset in python

查看:174
本文介绍了获取GMT时间给定日期和UTC偏移量在python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以下格式的日期字符串'%Y%m%d%H%M%S'例如'19981024103115'
和UTC本地偏移量的另一个字符串,例如'+ 0100'



所以结果将是'1998-10-24 09: 31:15'

解决方案

你可以使用 dateutil

 >>>> from dateutil.parser import parse 
>>> dt = parse('19981024103115 + 0100')
>>> dt
datetime.datetime(1998,10,24,10,31,15,tzinfo = tzoffset(None,3600))
>>> dt.utctimetuple()
time.struct_time(tm_year = 1998,tm_mon = 10,tm_mday = 24,tm_hour = 9,tm_min = 31,tm_sec = 15,tm_wday = 5,tm_yday = 297,tm_isdst = 0)


I have a date string of the following format '%Y%m%d%H%M%S' for example '19981024103115' and another string of the UTC local offset for example '+0100'

What's the best way in python to convert it to the GMT time

So the result will be '1998-10-24 09:31:15'

解决方案

You could use dateutil for that:

>>> from dateutil.parser import parse
>>> dt = parse('19981024103115+0100')
>>> dt
datetime.datetime(1998, 10, 24, 10, 31, 15, tzinfo=tzoffset(None, 3600))
>>> dt.utctimetuple()
time.struct_time(tm_year=1998, tm_mon=10, tm_mday=24, tm_hour=9, tm_min=31, tm_sec=15, tm_wday=5, tm_yday=297, tm_isdst=0)

这篇关于获取GMT时间给定日期和UTC偏移量在python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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