【问题标题】:Convert string second including timezone to a formated string date将包含时区的字符串秒转换为格式化的字符串日期
【发布时间】:2019-01-10 11:37:57
【问题描述】:

我正在尝试将如下所示的字符串秒数动态转换为字符串日期。

'1545239561 +0100'

问题是最后插入的时区,我找不到任何使用良好格式从该字符串中检索日期的python时间对象方法。

我的尝试:

>>>seconds = '1545239561 +0100'
>>>time.strftime('%y%m%d-%H%M%S-%f', datetime.datetime.fromtimestamp(seconds)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required (got type str)
>>>time.strptime(seconds)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/3.6.4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 559, in _strptime_time
    tt = _strptime(data_string, format)[0]
  File "/usr/local/Cellar/python/3.6.4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 362, in _strptime
    (data_string, format))
ValueError: time data '1545239561 +0100' does not match format '%a %b %d %H:%M:%S %Y'
>>>time.strptime(seconds, "%S +%Z")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/3.6.4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 559, in _strptime_time
    tt = _strptime(data_string, format)[0]
  File "/usr/local/Cellar/python/3.6.4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 362, in _strptime
    (data_string, format))
ValueError: time data '1545239561 +0100' does not match format '%S +%Z'

【问题讨论】:

    标签: python date datetime


    【解决方案1】:

    我会尝试分别处理这两个值并将它们合并为单个 datetime:

    >>>from datetime import datetime
    >>>s = '1545239561 +0100'
    >>>seconds, offset = s.split()
    >>>datetime.fromtimestamp(int(seconds)).replace(tzinfo=datetime.strptime(offset, "%z").tzinfo)
    datetime.datetime(2018, 12, 19, 17, 12, 41, tzinfo=datetime.timezone(datetime.timedelta(0, 3600)))
    

    【讨论】:

      【解决方案2】:

      是的@mfrackwiak...

      我这样做了

      >>> epoch = "1545239561 +0100"
      >>> seconds, offset = epoch.split()
      >>> datetime.fromtimestamp(int(seconds)).replace(tzinfo=datetime.strptime(offset, "%z").tzinfo).strftime('%Y-%m-%d %H:%M:%S-%Z')
      '2018-12-19 18:12:41-UTC+01:00'
      >>> 
      

      【讨论】:

      • 不是真正的错误答案,因为您仍在采取好的方法,这里的微妙之处在于插入的时区
      【解决方案3】:

      你可以试试下面的例子:

      from datetime import datetime,timedelta
      # split the timevalue and offset value
      ss = '1545239561 +0100'.split()
      format =  "%A, %B %d, %Y %I:%M:%S"
      # calculate the hour and min in the offset
      hour  = int(ss[1][0:2])
      min =   int(ss[1][2:])
      # calculate the time from the sec and convert it to datetime object
      time_from_sec = datetime.strptime(datetime.fromtimestamp(int(ss[0])).strftime(
          format), format)
      # add the offset delta value to the time calculated
      time_with_delta_added = time_from_sec + timedelta(hours=hour,minutes=min)
      print(time_with_delta_added)
      

      输出:

      2018-12-19 12:22:41
      

      【讨论】:

      • 是的,但我想包括时区,看看我上面的答案
      猜你喜欢
      • 2015-10-02
      • 1970-01-01
      • 2016-04-11
      • 2013-02-21
      • 2013-10-28
      • 1970-01-01
      • 1970-01-01
      • 2018-05-17
      • 2020-01-20
      相关资源
      最近更新 更多