2017-03-03 42 views
-1

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

没有记录返回。

将日期保存为字符串,并将日期查询为字符串,这是该项目的可行解决方法。只是认为它值得张贴 - 任何人看到这个?建议吗?

+0

更新:添加了第二个字段定义为'代码'DateTimeProperty() - 保存相同的错误值。 –

回答

0

您在appengine上生成的日期时间是UTC,并且它将作为该日期时间存储,不带时区信息。 当您查看它时,它将被转换为太平洋时间。

您所做的查询不正确:您正在生成datetime而不是date,因此您与之比较的时间会有所不同。

+0

谢谢!没有意识到日期查看器为我本地化。 –