昨天写到日期和时间的处理,需要输入日期和时间,存储到GAE的DataStore中。 GAE有三个与日期时间有关的Property: DateTimePropertyDatePropertyTimeProperty 这三个Property,其实最终都是以datetime.datetime存储,但是使用的时候又有一点不同。 Python中有很棒的解析字符串到时间对象的方法:time.strptime(string[, format]),这个方法会把字符串按照你想要的格式解析进来,放到一个时间对象中。返回值是一个struct_time。解析了之后还要存到GAE的DataStore对象中去,暂时找到了下面的方法: tDate = time.strptime(self.request.get(‘getup_date’),’%m/%d/%Y’)record.recordDate = datetime.date(tDate.tm_year, tDate.tm_mon, tDate.tm_mday)tGetup = time.strptime(self.request.get(‘getup_at’), ‘%H:%M’)record.getupAt = datetime.time(tGetup.tm_hour,tGetup.tm_min)tSleep = time.strptime(self.request.get(‘sleep_at’), ‘%H:%M’)record.sleepAt = datetime.time(tSleep.tm_hour,tSleep.tm_min) 这样就能把12/20/2008这样的日期,以及07:30这样的时间存成想要的对象了。注意如果直接把解析的结果赋给TimeProperty和DateProperty的话会出错。 另外,time.strftime(formart[, t])能够很好的将时间日期对象换成你想要的格式。
昨天晚上试验Google App Engine (GAE),要在页面上显示一个时间的估计。我原本设计是在数据库中只存一个字段,即估计的分钟数,然后在页面上计算一下来显示,却被折腾了好几个小时,第一次试验有点失败,特记之。 我传到页面上的是一个对象数组,那么在页面上就得这么写: {% for m in messages %} <li> {{m.name}} – {{m.estimateMinutes}} 分钟 </li> {% endfor %} 嗯,运行没有错误,很高兴。接下来我想大家还是比较习惯看小时:分钟的方式呈现,于是改了一下逻辑。 {% for m in messages %} <li> {{m.name}} – {% if m.estimateMinutes >= 60 %} {{m.estimateMinutes/60}} 小时 {% endif %} {% if m.estimateMinutes%60 >= 0 %} {{m.estimateMinutes%60}} [...]