Python:获取基于DST的时区作为时间戳 [英] Python: get timezone based on DST for a timestamp

查看:141
本文介绍了Python:获取基于DST的时区作为时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了这样的代码来获取特定时间段内基于DST的时区:

I wrote such a code to get timezone based on DST for an specific epoch time:

def getTimeZoneFromEpoch(epoch)
    if time.daylight and time.gmtime(epoch).tm_isdst==1:
        return -time.altzone/3600.0
    else:
        return -time.timezone/3600.0

但是我不确定它的正确性,事实上目前我犯了1小时错误。也许我应该在这段代码中交换altzone和timezone
,但是我从python的帮助(时间模块)中了解的内容不是这样:

But i'm not sure its correct, in fact at the moment i mistakes by 1 hour. Maybe i should swap altzone and timezone in this code, but its not what i understood from python's help (time module):

timezone -- difference in seconds between UTC and local standard time
altzone -- difference in  seconds between UTC and local DST time
tm_isdst
    1 if summer time is in effect, 0 if not, and -1 if unknown

我是否误以为是东西?

推荐答案

我已经测试了此代码以获得VM的区域设置UTC偏移量。顺便说一句,这仅在被测量时才真正有效。我不确定您的代码是否等效。

I've tested this code to obtain the VM's locale UTC offset. Which, by the way, is only really valid at the moment it is measured. I'm not sure whether your code is equivalent or not.

def local_ephemeral_UTC_offset(epoch_time=None):
  u"Returns a datetime.timedelta object representing the local time offset from UTC at the moment"
  if epoch_time == None:
    epoch_time = time()
  return datetime.fromtimestamp(epoch_time) - datetime.utcfromtimestamp(epoch_time)

这篇关于Python:获取基于DST的时区作为时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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