我为用户提供了一个数据库“ TimeZoneField”类型,如何将其与“ datetime.datetime()”对象一起使用以获取strftime()字符串,例如'%Y-%m-%dT%H:%M:% S.000Z'?

最佳答案

这用于Google Calendar的Python数据API提要,并且需要大量工作,因为Python的日期时间库不支持ISO 8601:
http://wiki.python.org/moin/WorkingWithTime

另外,如果您将带有.000Z时区的日期传输到Google日历,则对于在EDT和其他事件中发生的事件,它会忽略DST(夏令时)(因此一年中的某些时间可能会减少一小时)。

这是我的解决方法:假设start_time和end_time是可识别时区的datetime.datetime对象:

    timezone_string = start_datetime.strftime('%z')[0:3] +“:” + start_datetime.strftime('%z')[3:6]
    start_time = start_datetime.strftime('%Y-%m-%dT%H:%M:%S'+ timezone_string)
    end_time = end_datetime.strftime('%Y-%m-%dT%H:%M:%S'+ timezone_string)


请注意,stftime(“%z”)不包含该“:”字符来分隔Google日历API所需的偏移量中的小时/分钟。

关于python - Python/Django:有DB TimeZoneField供用户使用,如何与日期时间和时间结合使用以获取strftime(),例如'%Y-%m-%dT%H:%M:%S.000Z'?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4060354/

10-09 18:55