简体   繁体   English

Python:strftime将UTC时间戳转换为本地时间格式

[英]Python: strftime a UTC timestamp to local time format

I'd like to use the timestamp from a database result and convert it to my locale time format. 我想使用数据库结果中的时间戳并将其转换为我的语言环境时间格式。 The timestamp itself is saved in UTC format: 2015-03-30 07:19:06.746037+02 . 时间戳本身以UTC格式保存: 2015-03-30 07:19:06.746037+02 After calling print value.strftime(format) with the format %d.%m.%Y %H:%M %z the output will be 30.03.2015 07:19 +0200 . %d.%m.%Y %H:%M %z print value.strftime(format)调用print value.strftime(format) ,输出将为30.03.2015 07:19 +0200 This might be the correct way to display timestamps with timezone information but unfortunately users here are not accustomed to that. 这可能是显示带有时区信息的时间戳的正确方法,但是不幸的是,此处的用户不习惯这种方式。 What I want to achieve is the following for the given timestamp: 30.03.2015 09:19 . 对于给定的时间戳,我想要实现的目标是: 30.03.2015 09:19 Right now I'm adding two hours via 现在我要通过两个小时

is_dst = time.daylight and time.localtime().tm_isdst > 0
utc_offset = - (tine.altzone if is_dst else time.timezone)
value = value + timedelta(seconds=utc_offset)

I was wondering if there is a more intelligent solution to my problem. 我想知道是否有更智能的解决方案来解决我的问题。 ( timestamp.tzinfo has a offset value, can/should this be used instead? The solution needs to be DST aware too.) timestamp.tzinfo有一个offset值,可以/应该使用它吗?解决方案也需要DST感知。)

In your question the timestamp is already in desired timezone, so you don't need to do anything. 在您的问题中,时间戳已经在所需的时区中,因此您无需执行任何操作。

If you want to convert it to some other timezone you should be able to use; 如果您想将其转换为其他时区,则应该可以使用;

YourModel.datetime_column.op('AT TIME ZONE')('your timezone name')

or, 要么,

func.timezone('your timezone name', YourModel.datetime_column)

in SQLAlchemy level. 在SQLAlchemy级别。

On python level, consider using pytz 在python级别上,考虑使用pytz

You don't need to do conversions manually when you use time zone aware database timestamps unless the timezone you want to display is different from the system timezone. 使用时区感知型数据库时间戳时,无需手动进行转换,除非要显示的时区与系统时区不同。 When you read and write datetime objects to the database the timezone info of the datetime object is taken into account, this means what you get back is the time in the local time zone, in your case +0200. 当您将datetime对象读写到数据库时,考虑了datetime对象的时区信息,这意味着您返回的是本地时区中的时间,在本例中为+0200。

This SO post answers how to get local time from a timezoned timestamp. 该SO帖子回答了如何从时区时间戳获取本地时间。

Basically, use tzlocal. 基本上,使用tzlocal。

import time
from datetime import datetime

import pytz # $ pip install pytz
from tzlocal import get_localzone # $ pip install tzlocal

# get local timezone    
local_tz = get_localzone() 

# test it
# utc_now, now = datetime.utcnow(), datetime.now()
ts = time.time()
utc_now, now = datetime.utcfromtimestamp(ts), datetime.fromtimestamp(ts)

local_now = utc_now.replace(tzinfo=pytz.utc).astimezone(local_tz) # utc -> local
assert local_now.replace(tzinfo=None) == now

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM