【问题标题】:Convert EST timestamp to GMT taking into account Daylight Savings考虑夏令时将 EST 时间戳转换为 GMT
【发布时间】:2015-08-04 20:40:04
【问题描述】:

我有一个时间戳,它表示自 1970 年以来的毫秒数 1432202088224,它转换为 Thursday, May 21, 2015 5:54:48 AM EDT。我想编写一个 python 函数,将该时间戳转换为 GMT 中的毫秒数。我不能天真地将四个小时(3600000 毫秒)添加到现有时间戳,因为半年我将休息一小时。

我尝试使用datetimepytz 编写函数

def convert_mills_GMT(milliseconds):
    converted_raw = datetime.datetime.fromtimestamp(milliseconds/1000.0)
    date_eastern = eastern.localize(converted_raw, is_dst=True)
    date_utc = date_eastern.astimezone(utc)
    return int(date_utc.strftime("%s")) * 1000

使用1432202088224 的输入,此函数返回1432220088000,即Thursday, May 21, 2015 10:54:48 AM EDT,而我想要的是上午9:54。我错过了什么?

【问题讨论】:

    标签: python timezone pytz


    【解决方案1】:

    没有“EST 时间戳”之类的东西。如果您需要“GMT 时间戳”,那么您已经有了它。

    从以毫秒数给出的 POSIX 时间戳获取 UTC 时间:

    >>> from datetime import datetime, timedelta
    >>> timestamp = 1432202088224
    >>> utc_time = datetime(1970, 1, 1) + timedelta(milliseconds=timestamp)
    >>> utc_time.strftime('%A, %B %d, %Y %H:%M:%S %p UTC')
    'Thursday, May 21, 2015 09:54:48 AM UTC'
    

    我们可以通过将UTC时间转换回“EST”时区来检查结果是否正确:

    >>> import pytz # $ pip install pytz
    >>> est = utc_time.replace(tzinfo=pytz.utc).astimezone(pytz.timezone('US/Eastern'))
    >>> est.strftime('%A, %B %d, %Y %H:%M:%S %p %Z')
    'Thursday, May 21, 2015 05:54:48 AM EDT'
    

    【讨论】:

      【解决方案2】:

      Don't use .strftime("%s"). It is not supported, and may silently fail. 相反,要将 UTC 日期时间转换为时间戳,请使用 one of the methods shown here,具体取决于您的 Python 版本:

      Python 3.3+:

      timestamp = dt.timestamp()
      

      Python3(
      epoch = datetime(1970, 1, 1, tzinfo=timezone.utc)
      timestamp = (dt - epoch) / timedelta(seconds=1)
      

      Python 2.7+:

      timestamp = (dt.replace(tzinfo=None) - datetime(1970, 1, 1)).total_seconds()
      

      Python2(
      def totimestamp(dt, epoch=datetime(1970,1,1)):
          td = dt - epoch
          # return td.total_seconds()
          return (td.microseconds + (td.seconds + td.days * 86400) * 10**6) / 10**6 
      timestamp = totimestamp(dt.replace(tzinfo=None))
      

      因此,您的convert_mills_GMT 应该是这样的

      def convert_mills_GMT(milliseconds, 
                            utc=pytz.utc,
                            eastern=pytz.timezone('US/Eastern')
      ):
          converted_raw = DT.datetime.fromtimestamp(milliseconds/1000.0)
          date_eastern = eastern.localize(converted_raw, is_dst=True)
          date_utc = date_eastern.astimezone(utc)
          timestamp = ...
          return int(timestamp) * 1000
      

      以Python2.7为例,

      import datetime as DT
      import pytz
      
      def convert_mills_GMT(milliseconds, 
                            utc=pytz.utc,
                            eastern=pytz.timezone('US/Eastern')
      ):
          converted_raw = DT.datetime.fromtimestamp(milliseconds/1000.0)
          date_eastern = eastern.localize(converted_raw, is_dst=True)
          date_utc = date_eastern.astimezone(utc)
          timestamp = ((date_utc.replace(tzinfo=None) - DT.datetime(1970, 1, 1))
                       .total_seconds())
          return int(timestamp) * 1000
      
      print(DT.datetime.utcfromtimestamp(convert_mills_GMT(1432202088224)/1000.0))
      

      打印

      2015-05-21 09:54:48
      

      【讨论】:

        猜你喜欢
        • 2018-01-27
        • 2014-04-01
        • 1970-01-01
        • 2014-06-01
        • 2017-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多