是“-1月”吗?在 SQLite DATETIME 函数中“损坏”?
当使用 DATETIME 函数进行一些算术运算时,我发现了以下行为:
select DATETIME('now', '-1month', 'start ofmonth')
预计时间:2011-02-01 00:00:0
结果:2011-03-01 00:00:0 -
选择 DATETIME('now', '-2 月', '月初') 失败
预计时间:2011-01-01 00:00:0
结果:2011-01-01 00:00:0 - 好的,
这似乎只从昨天开始发生,我的集成测试发现了这一点。我的猜测是,减去的月份是以天为单位计算的,并且以某种方式使用了上一个完整月份的天数(而不是当前的天数)。最后一个完整的月份,2 月有 28 天,因此结果)
通过更改 DATETIME 函数的输入顺序可以轻松解决这个问题,如下所示:DATETIME('now', 'start of Month', '-1 Month') ,但这仍然是一种棘手的行为,并且可能会导致被忽视的错误。
我正在使用 System.Data.SQLite.DLL / 版本 1.0.66.0 / 2010 年 4 月 18 日
还有其他人发现此行为吗?这是一个(已知)错误吗?还是我“做错了”?
When using the DATETIME function to do some arithmetic, I've found the following behaviour:
select DATETIME('now', '-1 month', 'start of month')
expected: 2011-02-01 00:00:0
result: 2011-03-01 00:00:0 - Fail
select DATETIME('now', '-2 month', 'start of month')
expected: 2011-01-01 00:00:0
result: 2011-01-01 00:00:0 - Ok
This only appears to be happening since yesterday, my integration tests picked up on this. My guess is that the month that is substracted is calculated in days and that somehow the number of days in the last full month is used (not the current). The last full month, february featured 28 days, hence the result)
This is easily resolved by changing the order of input to the DATETIME function, like so: DATETIME('now', 'start of month', '-1 month'), but still it is tricky behaviour and likely to result in bugs that get unnoticed.
I am using System.Data.SQLite.DLL / version 1.0.66.0 / Apr 18, 2010
Anybody else found this behaviour? Is this a (known) bug? Or am I 'doing it wrong'?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
现在
减去一个月 = 2 月 30 日 = 3 月 2 日。然后start of Month
为您提供三月的开始时间。这可能不是任何人想要的行为,但它是文档所描述的:转到 http://www.sqlite.org/lang_datefunc.html。 sqlite.org/lang_datefunc.html 并搜索“通过渲染工作”。now
minus one month = 30th of February = 2nd of March. Thenstart of month
gives you the start of March. This is probably not the behaviour anyone ever wants, but it's what the documentation describes: go to http://www.sqlite.org/lang_datefunc.html and search for "works by rendering".而不是:
你尝试过吗:
我认为第二个总是会给你上个月的第一天。如果当前月份的天数多于上个月,前者有时会给出当前月份的第一天。
Instead of:
Have you tried:
I think the second will always give you the first day of the previous month. The former will sometimes give you the first day of the current month if there are more days in the current month than in the previous month.
我很确定这是有效的如文档所述。
我预计正常化之前的变化是 2 月 30 日,这不是一个有效的日期。
I'm pretty sure that's working as documented.
I'd expect the change before normalizing to be Feb 30, which isn't a valid date.