0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DateTimeのズレ

Posted at

sqlで範囲検索するとずれる

irb(main):073:0> a = DateTime.now
=> Fri, 10 May 2024 00:45:56 +0900
irb(main):076:0> Model.where(created_at:b .. a)
  Model Load (0.5ms)  SELECT `models`.* FROM `models` WHERE `models`.`created_at` BETWEEN '2024-05-03 00:00:00' AND '2024-05-09 15:45:56.557590'
=> []

Fri, 10 May 2024 00:45:56 +0900'2024-05-09 15:45:56.557590'になっている。

9時間前になる。
+0900が9時間前になっているからだと思う。+0000すると時差が解消される

irb(main):078:0> a = "#{DateTime.now.to_date} 23:59:59 +0000"
=> "2024-05-10 23:59:59 +0000"
irb(main):079:0> Model.where(created_at:b .. a)
  Model Load (0.5ms)  SELECT `models`.* FROM `models` WHERE `models`.`created_at` BETWEEN '2024-05-03 00:00:00' AND '2024-05-10 23:59:59'
=> []

"2024-05-10 23:59:59 +0000"が '2024-05-10 23:59:59'になっている。

感想

今更DateTimeのことを知った。

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?