python - 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()
我如何为超过一天的帖子执行此操作?
解决方案
您可以使用模运算:
(now % (60 * 60 * 24)) >= (60 * 60 * 24 - 60)
这说明了乘法。你当然可以这样写:
(now % 86400) >= (86400 - 60)
对于大多数人(甚至未来的你)来说,这可能并不明显60*60*24
= 86400
。
推荐阅读
- php - mod_rewrite 忽略查询字符串并替换 ? 和 &
- jmeter - 解释 JMeter 结果
- jhipster - 部署到 Pivotal CF 的 Jhipster 默认生成的应用程序不会进行身份验证
- google-cloud-pubsub - 发布时间歇性授权失败
- javascript - 使用苍鹭公式计算周长和面积的javascript函数
- swift - 如何缩放 SCNNodes 以适应盒子?
- android - Android 房间数据库始终以 Primarykey 0 开头,用于新条目
- java - java:在另一个测试中访问对象的最佳实践
- angular - Kendo UI 网格中的过滤列模板
- c++ - 何时:std::abs(x -y) < std::numeric_limits
::分钟()