Python年龄验证

时间:2011-11-05 20:27:47

标签: python datetime verification

在我的加入脚本中,您必须加入13岁以上才能加入。问题是当我在某些日期检查它时,只有在我一个月前这样做才能起作用。

    cur_time = datetime.datetime.utcnow() - datetime.timedelta(days=4848.12)
    time_13 = str(cur_time.strftime("%Y-%m-%d"))
    bmonth = self.get_argument('bmonth', '')
    bday = self.get_argument('bday', '')
    byear = self.get_argument('byear', '')
    birthday = byear + '-' + bmonth + '-' + bday
    if time_13 <= birthday:
        c_age = True
    else:
        c_age = ''
    if c_age:
        response = tornado.escape.json_encode({"error":"true","msg":"You must be 13 years of age or older to join uSocial'N"})

1 个答案:

答案 0 :(得分:4)

最好比较日期和日期范围而不是字符串。

假设您获得生日零件的字符串:

bmonth = self.get_argument('bmonth', '')
bday = self.get_argument('bday', '')
byear = self.get_argument('byear', '')

bd = datetime.datetime(int(byear), int(bmonth), int(bday))
min_age = datetime.timedelta(weeks = 52*13)
if datetime.datetime.now() - bd < min_age:
    response = tornado.escape.json_encode({"error":"true", "msg":
                     "You must be 13 years of age or older to join uSocial'N"})