简体   繁体   English

将zope DateTime对象转换为Python datetime对象的最佳方法是什么?

[英]What is the best way to convert a zope DateTime object into Python datetime object?

I need to convert a zope 2 DateTime object into a Python datetime object. 我需要将zope 2 DateTime对象转换为Python datetime对象。 What is the best way to do that? 最好的方法是什么? Thanks, Erika 谢谢,Erika

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

modernthingy = zopethingy.asdatetime()
modernthingy = datetime.datetime.fromtimestamp(zopethingy.timeTime())

The datetime instance is timezone-naive; datetime实例是timezone-naive; if you need to support timezones (as Zope2's DateTime does), I recommend third-party extension package pytz . 如果你需要支持时区(如Zope2的DateTime那样),我推荐第三方扩展包pytz

If you mean this one 如果你的意思是这个

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

then reverse is 然后逆转

>>> 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)

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

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