【问题标题】:What is the best way to convert a zope DateTime object into Python datetime object?将 zope DateTime 对象转换为 Python datetime 对象的最佳方法是什么?
【发布时间】:2010-04-05 23:59:40
【问题描述】:

我需要将 zope 2 DateTime 对象转换为 Python 日期时间对象。最好的方法是什么?谢谢,埃里卡

【问题讨论】:

    标签: python zope


    【解决方案1】:

    较新的 DateTime 实现(2.11 及更高版本)有一个 asdatetime 方法,该方法返回一个 python datetime.datetime 实例:

    modernthingy = zopethingy.asdatetime()
    

    【讨论】:

      【解决方案2】:
      modernthingy = datetime.datetime.fromtimestamp(zopethingy.timeTime())
      

      datetime 实例是 timezone-naive;如果你需要支持时区(就像 Zope2 的 DateTime 一样),我推荐第三方扩展包 pytz

      【讨论】:

        【解决方案3】:

        如果你的意思是this 一个

        .strftime('%m/%d/%Y %H:%M') =  04/25/2005 10:19
        

        然后反向是

        >>> time.strptime('04/25/2005 10:19','%m/%d/%Y %H:%M')
        time.struct_time(tm_year=2005, tm_mon=4, tm_mday=25, tm_hour=10, tm_min=19, tm_sec=0, tm_wday=0, tm_yday=115, tm_isdst=-1)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多