【问题标题】:How to convert a time string in a Google AppEngine db.TimeProperty?如何在 Google AppEngine db.TimeProperty 中转换时间字符串?
【发布时间】:2017-07-04 23:16:10
【问题描述】:

根据问题标题,如何将 Python string 转换为 Google App Engine db.TimeProperty

我尝试过:

obj.time = strptime("10:00", "%H:%M")

但是我收到以下错误:

BadValueError: 属性时间必须是时间,但是是 time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=10, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)

【问题讨论】:

  • 你真的不应该使用 db 库,它已被弃用多年。
  • @DanielRoseman 它也适用于 ndb。

标签: python google-app-engine google-cloud-platform google-cloud-datastore


【解决方案1】:

我不熟悉(被取代的)db 文档,但从NDB Cheat Sheet db.TimeProperty() 直接对应于ndb.TimeProperty()。而来自ndbDate and Time Properties

三种属性类型可用于存储与日期和时间相关的 价值观:

  • DateProperty
  • TimeProperty
  • DateTimeProperty

这些取值属于相应的类(date, time, datetime) 的标准 Python datetime 模块。最多 三者中的一般是DateTimeProperty,表示两者 日历日期和时间;其他的偶尔有用 仅需要日期(例如出生日期)的特殊用途 或只是一个时间(例如会议时间)。由于技术原因, DatePropertyTimePropertyDateTimeProperty,但你不应该依赖这个继承 关系(并注意它与继承不同 datetime 定义的底层类之间的关系 模块本身)。

所以我会写它:

obj.time = datetime.datetime.strptime("10:00", "%H:%M").time()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 2010-11-27
    相关资源
    最近更新 更多