GAE中保存日期的奇怪结果(带有ndb库的Python)。 来自Web表单的入站字符串是格式为%a%m /%d /%y(Fri 3/3/17)。 用datetime.strptime解析得到日期值。 当它保存在DateProperty()字段中时,该值始终在前一天的16:00和00.000 PST。DateProperty中的日期错误()通过ndb保存
postDate = datetime.datetime.strptime(self.request.get('date-'+
hashed_id),'%a %m/%d/%y')
logging.info('postDate as Date: %s',postDate)
postDateStr = datetime.datetime.strftime(postDate,'%D')
logging.info('postDateStr: %s',postDateStr)
thisPost = ScheduledPost(id = postID,
...
postDate = postDate,
postDateStr = postDateStr
)
日志结果:
postDate as Date: 2017-03-03 00:00:00
postDateStr: 03/03/17
到目前为止好,对不对?但在数据存储接口中,该记录显示:
PostDate: 2017-03-02 (16:00:00:000 PST)
PostDateStr: 03/03/17
糟糕。
工作站是在太平洋时间 - 但把它留在一边 - 日期查询似乎证实日期是错误的。假设今天是3/3/17 -
today = dt.datetime.now()
ScheduledPost.query(ScheduledPost.postDate == today).fetch()
没有记录返回。
将日期保存为字符串,并将日期查询为字符串,这是该项目的可行解决方法。只是认为它值得张贴 - 任何人看到这个?建议吗?
更新:添加了第二个字段定义为'代码'DateTimeProperty() - 保存相同的错误值。 –