How to convert a time string in a Google AppEngine db.TimeProperty?

懵懂的女人 提交于 2019-12-24 05:48:50

问题


As per question title, how to convert a Python string to a Google App Engine db.TimeProperty?

I tried to do:

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

However I get the following error:

BadValueError: Property time must be a time, but was 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)


回答1:


I'm not familiar with the (superseded) db docs, but from the NDB Cheat Sheet db.TimeProperty() corresponds directly to ndb.TimeProperty(). And from ndb's Date and Time Properties:

Three property types are available for storing date- and time-related values:

  • DateProperty
  • TimeProperty
  • DateTimeProperty

These take values belonging to the corresponding classes (date, time, datetime) of the standard Python datetime module. The most general of the three is DateTimeProperty, which denotes both a calendar date and a time of day; the others are occasionally useful for special purposes requiring just a date (such as a date of birth) or just a time (such as a meeting time). For technical reasons, DateProperty and TimeProperty are subclasses of DateTimeProperty, but you shouldn't depend on this inheritance relationship (and note that it differs from the inheritance relationships between the underlying classes defined by the datetime module itself).

So I'd write it:

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


来源:https://stackoverflow.com/questions/42263875/how-to-convert-a-time-string-in-a-google-appengine-db-timeproperty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!