Python 2.7-尝试将UTC字符串转换为本地时间,同时考虑DST

Python 2.7-尝试将UTC字符串转换为本地时间,同时考虑DST,python,Python,我有这样一个UTC时间字符串 supplied_datetime = 20160711230000 -0500 这是格式 yyyyMMddhhmmss +/-hhmm 现在,如果我从原始时间中减去(-5小时),它应该是 supplied_datetime = 20160711180000 下一部分是,我需要根据当地时间进行更正,确保考虑任何dst时间 假设我在英国,UTC 0000,但我们在DST+0100,那么最终显示给用户的时间是 supplied_datetime = 2016071

我有这样一个UTC时间字符串

supplied_datetime = 20160711230000 -0500
这是格式

yyyyMMddhhmmss +/-hhmm
现在,如果我从原始时间中减去(-5小时),它应该是

supplied_datetime = 20160711180000
下一部分是,我需要根据当地时间进行更正,确保考虑任何dst时间

假设我在英国,UTC 0000,但我们在DST+0100,那么最终显示给用户的时间是

supplied_datetime = 20160711190000
因此,公式是
supplied\u datetime-(supplied\u utc\u offset+local\u utc\u offset)

这是我在这里问之前所知道的

local_utc_offset = calendar.timegm(time.localtime()) - calendar.timegm(time.gmtime(time.mktime(time.localtime())))

supplied_utc_offset = parse(programme.get('start')[:20])

如果日期时间字符串的格式始终一致,则可以在给定utc日期时间的情况下手动获取本地日期时间

time.timezone
以秒为单位给出本地偏移量

然后,您只需要解析datetime字符串,并将其与本地偏移量(以小时为单位)以及datetime字符串中的偏移量一起添加:

from datetime import datetime
import time
dst = time.localtime().tm_isdst # daylight saving
local_offset = (dst-(time.timezone / 3600))  * 10000  # Local offset in hours format
strtime = '20160711230000 -0500'  # The original datetime string
dtstr, offset = strtime[:-6], strtime[-5:]  # separate the offset from the datetime string
utc_dt = datetime.strptime(dtstr, '%Y%m%d%H%M%S')
local_dtstr = str(int(dtstr) + int(offset) * 100 + local_offset)  # get local time string
local_dt = datetime.strptime(local_dtstr, '%Y%m%d%H%M%S')

In [37]: utc_dt
Out[37]: datetime.datetime(2016, 7, 11, 23, 0)

In [38]: local_dt
Out[38]: datetime.datetime(2016, 7, 11, 19, 0)

如果日期时间字符串的格式始终一致,则可以在给定utc日期时间的情况下手动获取本地日期时间

time.timezone
以秒为单位给出本地偏移量

然后,您只需要解析datetime字符串,并将其与本地偏移量(以小时为单位)以及datetime字符串中的偏移量一起添加:

from datetime import datetime
import time
dst = time.localtime().tm_isdst # daylight saving
local_offset = (dst-(time.timezone / 3600))  * 10000  # Local offset in hours format
strtime = '20160711230000 -0500'  # The original datetime string
dtstr, offset = strtime[:-6], strtime[-5:]  # separate the offset from the datetime string
utc_dt = datetime.strptime(dtstr, '%Y%m%d%H%M%S')
local_dtstr = str(int(dtstr) + int(offset) * 100 + local_offset)  # get local time string
local_dt = datetime.strptime(local_dtstr, '%Y%m%d%H%M%S')

In [37]: utc_dt
Out[37]: datetime.datetime(2016, 7, 11, 23, 0)

In [38]: local_dt
Out[38]: datetime.datetime(2016, 7, 11, 19, 0)

好的,在@CentAu的帮助下,我成功地回答了这个问题,如下所示

import time
import calendar
from datetime import datetime, timedelta

def datetime_from_utc_to_local(input_datetime):
    #Get local offset from UTC in seconds
    local_offset_in_seconds = calendar.timegm(time.localtime()) - calendar.timegm(time.gmtime(time.mktime(time.localtime())))
    #split time from offset
    input_datetime_only, input_offset_only = input_datetime[:-6], input_datetime[-5:]
    #Convert input date to a proper date
    input_datetime_only_dt = datetime.strptime(input_datetime_only, '%Y%m%d%H%M%S')
    #Convert input_offset_only to seconds
    input_offset_mins, input_offset_hours = int(input_offset_only[3:]), int(input_offset_only[:-2])
    input_offset_in_total_seconds = (input_offset_hours * 60 * 60) + (input_offset_mins * 60);
    #Get the true offset taking into account local offset
    true_offset_in_seconds = local_offset_in_seconds + input_offset_in_total_seconds
    # add the true_offset to input_datetime
    local_dt = input_datetime_only_dt + timedelta(seconds=true_offset_in_seconds)
    return str(input_datetime) + ' ' + str(local_dt)

t = datetime_from_utc_to_local("20160711230000 +0300")
print (t)

好的,在@CentAu的帮助下,我成功地回答了这个问题,如下所示

import time
import calendar
from datetime import datetime, timedelta

def datetime_from_utc_to_local(input_datetime):
    #Get local offset from UTC in seconds
    local_offset_in_seconds = calendar.timegm(time.localtime()) - calendar.timegm(time.gmtime(time.mktime(time.localtime())))
    #split time from offset
    input_datetime_only, input_offset_only = input_datetime[:-6], input_datetime[-5:]
    #Convert input date to a proper date
    input_datetime_only_dt = datetime.strptime(input_datetime_only, '%Y%m%d%H%M%S')
    #Convert input_offset_only to seconds
    input_offset_mins, input_offset_hours = int(input_offset_only[3:]), int(input_offset_only[:-2])
    input_offset_in_total_seconds = (input_offset_hours * 60 * 60) + (input_offset_mins * 60);
    #Get the true offset taking into account local offset
    true_offset_in_seconds = local_offset_in_seconds + input_offset_in_total_seconds
    # add the true_offset to input_datetime
    local_dt = input_datetime_only_dt + timedelta(seconds=true_offset_in_seconds)
    return str(input_datetime) + ' ' + str(local_dt)

t = datetime_from_utc_to_local("20160711230000 +0300")
print (t)

你的代码使用整数吗?或者使用字符串?我不是100%确定,但听起来你在做与这里相反的事情:@Bamcclur我不能使用pytz作为我正在开发的环境,它不支持它。@极权主义者--你在什么环境中开发?据我所知,
pytz
是纯python,因此您必须处于一个非常奇特的环境中,它才不受支持…@mgilson它是plex media server的插件。您的代码使用整数吗?或者使用字符串?我不是100%确定,但听起来你在做与这里相反的事情:@Bamcclur我不能使用pytz作为我正在开发的环境,它不支持它。@极权主义者--你在什么环境中开发?据我所知,
pytz
是纯python,因此您必须处于一个非常奇特的环境中,它才不受支持…@mgilson它是plex media server的插件。将其设置为配置选项(显示您是否在日光节约)。如果你需要自动判断你是否在dst,这显然是行不通的。是的,它需要自动。我已经在上面的代码中计算出了本地时间偏移量,只是为了理解你的问题,你的主要问题是DST?这取决于一年中的时间,您需要您的程序能够确定您是否在日光节约?不,这只是一个声明,说明代码需要考虑DST。我的主要问题是将字符串的-0500部分转换为秒,这样我就可以进行计算。现在,它会自动计算出您是否在DST中。最后一行之前的1行根据时区差异(在字符串的最后5个字符中指定)将偏移量转换为本地偏移量。它适用于负时间偏移和正时间偏移。将其设置为配置选项(显示您是否处于日光节约状态)。如果你需要自动判断你是否在dst,这显然是行不通的。是的,它需要自动。我已经在上面的代码中计算出了本地时间偏移量,只是为了理解你的问题,你的主要问题是DST?这取决于一年中的时间,您需要您的程序能够确定您是否在日光节约?不,这只是一个声明,说明代码需要考虑DST。我的主要问题是将字符串的-0500部分转换为秒,这样我就可以进行计算。现在,它会自动计算出您是否在DST中。最后一行之前的1行根据时区差异(在字符串的最后5个字符中指定)将偏移量转换为本地偏移量。它适用于负时间偏移和正时间偏移。