python后端 flask框架 计算时间差 并根据时间差条件返回flag值

python后端 计算时间差 并根据时间差条件返回flag值

话不多说 直接上代码

在这里插入图片描述

先上代码片段 这里的逻辑是根据现在的实际和数据库存储的时间差小于16天的 则返回flag1 否则flag为0

sub_time = abs((datetime.datetime.strptime(str(i[4]), "%Y-%m-%d %H:%M:%S") - datetime.datetime.now()).days)
     if sub_time <= 16:
         temp["flag"] = 1
     else:
         temp["flag"] = 0

全部代码

@app.route('/xxxx', methods=['GET'])
def second():
    if request.method == "GET":
        _id = request.args.get('id')
        print(_id)
        db.ping(reconnect=True)
        # cursor.execute("select * from content where parent=\"" + str(_id) + "\" and DATE_SUB(CURDATE(), INTERVAL 15 DAY) <= date(time) order by time desc")
        cursor.execute("select * from content where parent=\"" + str(_id) + "\" order by time desc")
        data = cursor.fetchall()
        temp = {
     }
        result = []
        if data:
            print(data)
            for i in data:
                temp["id"] = i[0]
                temp["name"] = i[1]
                temp["link"] = i[2]
                temp["parent"] = i[3]
                temp["time"] = i[4]
                sub_time = abs((datetime.datetime.strptime(str(i[4]), "%Y-%m-%d %H:%M:%S") - datetime.datetime.now()).days)
                if sub_time <= 16:
                    temp["flag"] = 1
                else:
                    temp["flag"] = 0
                result.append(temp.copy())
            return jsonify({
     "code": 200, "data": result})
        elif data == ():
            _data = {
     "code": 13, "msg": "暂无此类数据"}
            return jsonify(_data)
        else:
            _data = {
     "code": -1, "msg": "wrong,try again"}
            return jsonify(_data)

你可能感兴趣的:(python,后端)