使用Python将时间从UTC转换为GMT [英] Convert time from UTC to GMT with Python

查看:429
本文介绍了使用Python将时间从UTC转换为GMT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Python应用程序,该程序将YML文件转换为我的博客(静态网站生成器)的静态HTML页面。我想添加一个RSS feed。在某个地方,我读到出版日期必须是:

I'm writting a python app that convert YML files to static HTML pages for my blog (static site generator). I want to add an RSS feed. On a some place I read that the publication date must be:

<pubDate>Wed, 05 Jul 2017 18:38:23 GMT</pubDate>

但是我有:

<pubDate>2017-07-05T18:38:23+00:00</pubDate>

Python中的 datetime API确实很麻烦关于这些。如何将字符串 2017-07-05T18:38:23 + 00:00 转换为GMT?

The datetime API from Python is really cumbersome about these. How to convert the string 2017-07-05T18:38:23+00:00 to GMT?

谢谢

推荐答案

import datetime
basedate="2017-07-05T18:38:23+00:00"
formatfrom="%Y-%m-%dT%H:%M:%S+00:00"
formatto="%a %d %b %Y, %H:%M:%S GMT"
print datetime.datetime.strptime(basedate,formatfrom).strftime(formatto)

将为您返回正确的转录:2017年7月5日星期三,格林尼治标准时间

will return you the correct transcription : Wed 05 Jul 2017, 18:38:23 GMT

格式: http://strftime.org/

这篇关于使用Python将时间从UTC转换为GMT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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