首页 > 解决方案 > Flask-sqlalchemy 按 unix 间隔过滤

问题描述

我将该created_at列作为 unix 整数存储在我的Post表上。我需要从特定日期开始过滤所有具有created_at介于23h59和旧之间的 unix 时间戳的帖子。24h00

换句话说,我需要过滤自创建以来 24 小时间隔的最后一分钟内的所有帖子。

此查询适用于不到一天的帖子

post_min = now - 86400
post_max = now - 86400 + 60

posts = Post.query.filter(Post.created_at >= post_min, Post.created_at < post_max).all()

我如何为超过一天的帖子执行此操作?

标签: pythonflasksqlalchemyflask-sqlalchemy

解决方案


您可以使用模运算:

(now % (60 * 60 * 24)) >= (60 * 60 * 24 - 60)

这说明了乘法。你当然可以这样写:

(now % 86400) >= (86400 - 60)

对于大多数人(甚至未来的你)来说,这可能并不明显60*60*24= 86400


推荐阅读