简体   繁体   English

将带有时区的UTC日期时间转换为本地

[英]Convert UTC datetime with timezone to local

I have a string with a date in UTC and tzinfo 我在UTC和tzinfo中有一个带日期的字符串

"Thu, 01 Oct 2015 00:02:01 +0200"

How can I do to convert it to my local time so it outputs 如何将其转换为本地时间,以便输出

"2015-10-02 02:02:01"

I tried 我试过了

parser.parse("Thu, 01 Oct 2015 00:02:01 +0200")

but I can't find a way to sum this tzinfo to the time to get my local time. 但是我找不到一种方法可以将此tzinfo与时间相加以获得本地时间。

Thanks in advance 提前致谢

Edit: The question is diferent as It includes time diference in the given string and the point is to add or sutract this time diference that sometimes requires to change the date as in the provided example. 编辑:问题是不同的,因为它在给定的字符串中包括时差,而重点是添加或减少有时需要更改日期的时差,如提供的示例中所示。

Here's a stdlib solution: 这是一个stdlib解决方案:

#!/usr/bin/env python
from datetime import datetime
from email.utils import parsedate_tz, mktime_tz

timestamp = mktime_tz(parsedate_tz("Thu, 01 Oct 2015 00:02:01 +0200"))
print(datetime.fromtimestamp(timestamp)) # get local time

The input time format is rfc 5322 time format that is understood by email module . 输入的时间格式是email模块可以理解的 rfc 5322时间格式 datetime.fromtimestamp() may fail for past/future dates if it doesn't use a historical time zone database on a given platform. datetime.fromtimestamp()在给定平台上未使用历史时区数据库,则可能无法使用过去/将来的日期。 A portable solution is to use pytz module, to get access to the tz database: 一个可移植的解决方案是使用pytz模块,以访问tz数据库:

#!/usr/bin/env python
from datetime import datetime
from email.utils import parsedate_tz, mktime_tz
import tzlocal # $ pip install tzlocal

local_timezone = tzlocal.get_localzone() # return pytz tzinfo
timestamp = mktime_tz(parsedate_tz("Thu, 01 Oct 2015 00:02:01 +0200"))
print(datetime.fromtimestamp(timestamp, local_timezone))

I think the following does what you want by implementing a couple of concrete tzinfo classes: 我认为以下通过实现几个具体的tzinfotzinfo您的要求:

from datetime import datetime, timedelta, tzinfo
import time as _time

_ZERO = timedelta(0)
_STDOFFSET = timedelta(seconds=-_time.timezone)
if _time.daylight:
    _DSTOFFSET = timedelta(seconds=-_time.altzone)
else:
    _DSTOFFSET = _STDOFFSET

_DSTDIFF = _DSTOFFSET - _STDOFFSET

class UTC(tzinfo):
    """ Concrete tzinfo time zone class for UTC
    """
    def utcoffset(self, dt):
        return _ZERO
    def tzname(self, dt):
        return "UTC"
    def dst(self, dt):
        return _ZERO

UTC = UTC()

class LocalTimezone(tzinfo):
    """ Concrete tzinfo time zone class for current timezone
    """
    def utcoffset(self, dt):
        if self._isdst(dt):
            return _DSTOFFSET
        else:
            return _STDOFFSET

    def dst(self, dt):
        if self._isdst(dt):
            return _DSTDIFF
        else:
            return _ZERO

    def tzname(self, dt):
        return _time.tzname[self._isdst(dt)]

    # everything depends on this private method which assumes 'dt' argument
    # is in local time
    def _isdst(self, dt):
        tt = (dt.year, dt.month, dt.day,
              dt.hour, dt.minute, dt.second,
              dt.weekday(), 0, -1)  # last -1 means dst unknown (mktime will fill in)
        stamp = _time.mktime(tt)  # mktime returns secs
        tt = _time.localtime(stamp)  # localtime returns time_struct with tm_isdst attribute
        return tt.tm_isdst > 0

LOCAL = LocalTimezone()

stamp = 'Thu, 01 Oct 2015 00:02:01 +0200'
dt = datetime(*_time.strptime(' '.join(stamp.split()[:-1]),
                              '%a, %d %b %Y %H:%M:%S')[:6], tzinfo=UTC)
local_dt = dt.astimezone(LOCAL)
print(local_dt.strftime('%Y-%M-%d %H:%M:%S'))

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

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