Python - 将UTC datetime字符串转换为本地datetime [英] Python - Convert UTC datetime string to local datetime

查看:510
本文介绍了Python - 将UTC datetime字符串转换为本地datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有必要将时间转换成utc。最近有一个请求让我的应用程序成为时区知道,我一直在自己的圈子。许多关于将本地时间转换为utc的信息,我发现相当基本(也许我也在做错),但是我没有找到任何有关容易将utc时间转换为最终用户时区的信息。



简而言之,android应用程序发送我(appengine app)数据,在该数据中是一个时间戳。要将该时间戳存储到我正在使用的utc时间:

  datetime.utcfromtimestamp(timestamp)

这似乎正在工作。当我的应用程序存储数据时,它正在5小时存储(我是EST -5)



数据存储在appengine的BigTable中,并且在检索到它以这样的字符串出来:

 2011-01-21 02:37:21

如何在用户正确的时区将此字符串转换为DateTime?



此外,用户时区信息的推荐存储是什么? (你通常如何存储tz信息,即:-5:00或EST等?)我确定我的第一个问题的答案可能包含第二个参数的答案。

解决方案

如果您不想提供自己的 tzinfo ,请查看 python-dateutil 库。它在 zoneinfo(Olson)数据库之上提供 tzinfo 实现以便您可以通过某种规范的名称来引用时区规则。

  from datetime import datetime 
from dateutil import tz

#方法1:硬编码区:
from_zone = tz.gettz('UTC')
to_zone = tz.gettz('America / New_York')

#方法2:自动检测区域:
from_zone = tz.tzutc()
to_zone = tz.tzlocal()

#utc = datetime.utcnow )
utc = datetime.strptime('2011-01-21 02:37:21','%Y-%m-%d%H:%M:%S')

#在
#datetime对象为naive时默认为
utc = utc.replace(tzinfo = from_zone)

#转换日期时间对象时区
central = utc.astimezone(to_zone)

修改展开示例以显示 strptime 用法



编辑2 修正API使用情况,以显示更好的入门点方法



编辑3 包含自动检测方法时区(Yarin)


I've never had to convert time to and from utc. Recently had a request to have my app be timezone aware, and I've been running myself in circles. Lots of information on converting local time to utc, which I found fairly elementary (maybe I'm doing that wrong as well), but I can not find any information on easily converting the utc time to the end-users timezone.

In a nutshell, and android app sends me (appengine app) data and within that data is a timestamp. To store that timestamp to utc time I am using:

datetime.utcfromtimestamp(timestamp)

That seems to be working. When my app stores the data, it is being store as 5 hours ahead (I am EST -5)

The data is being stored on appengine's BigTable, and when retrieved it comes out as a string like so:

"2011-01-21 02:37:21"

How do I convert this string to a DateTime in the users correct time zone?

Also, what is the recommended storage for a users timezone information? (How do you typically store tz info ie: "-5:00" or "EST" etc etc ?) I'm sure the answer to my first question might contain a parameter the answers the second.

解决方案

If you don't want to provide your own tzinfo objects, check out the python-dateutil library. It provides tzinfo implementations on top of a zoneinfo (Olson) database such that you can refer to time zone rules by a somewhat canonical name.

from datetime import datetime
from dateutil import tz

# METHOD 1: Hardcode zones:
from_zone = tz.gettz('UTC')
to_zone = tz.gettz('America/New_York')

# METHOD 2: Auto-detect zones:
from_zone = tz.tzutc()
to_zone = tz.tzlocal()

# utc = datetime.utcnow()
utc = datetime.strptime('2011-01-21 02:37:21', '%Y-%m-%d %H:%M:%S')

# Tell the datetime object that it's in UTC time zone since 
# datetime objects are 'naive' by default
utc = utc.replace(tzinfo=from_zone)

# Convert time zone
central = utc.astimezone(to_zone)

Edit Expanded example to show strptime usage

Edit 2 Fixed API usage to show better entry point method

Edit 3 Included auto-detect methods for timezones (Yarin)

这篇关于Python - 将UTC datetime字符串转换为本地datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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