When I wrote the background, it became a poem, so I will write the conclusion first.
environment Rails 5.2.4.2 Ruby 2.6.6
--If you specify a date with DateTime, the instance will be a Date class. --The Date class does not have timezone information, so it will be +0000. --The Date class has a time zone when it becomes the DateTime / Time class. --When comparing Date class and DateTime / Time class (ʻActiveSupport :: TimeWithZone` class in Rails), you need to convert Date class to DateTime class.
Things happened around midnight. When I run RSpec, there are some things that have nothing to do with the implementation.
When I looked it up, it seems that the part that I really have to return is not returning. The behavior I want you to do is "return when the date and time of the last tweet is later than yesterday".
return if last_tweet.tweeted_at > DateTime.yesterday
So I looked it up.
[2] pry(#<RegisteredTag>)> DateTime.yesterday
=> Thu, 21 May 2020
Yeah, it's right.
[3] pry(#<RegisteredTag>)> DateTime.yesterday.to_time
=> 2020-05-21 00:00:00 +0900
It's okay.
[4] pry(#<RegisteredTag>)> last_tweet.tweeted_at.to_time
=> 2020-05-21 00:30:02 +0900
That's right, let's compare.
[6] pry(#<RegisteredTag>)> last_tweet.tweeted_at > DateTime.yesterday.to_time
=> true
If you compare it, it should be. Now let's go back to the original code.
[5] pry(#<RegisteredTag>)> last_tweet.tweeted_at > DateTime.yesterday
=> false
Fafafa? ?? ?? ??
So, let's check it again.
[33] pry(main)> DateTime.yesterday
=> Thu, 21 May 2020
[34] pry(main)> after_9
=> Thu, 21 May 2020 09:21:38 +0900
[35] pry(main)> DateTime.yesterday < after_9
=> true
[37] pry(main)> before_9
=> Thu, 21 May 2020 08:31:48 +0900
[38] pry(main)> DateTime.yesterday < before_9
=> false
8 o'clock <DateTime.yesterday <9 o'clock
So it seems that the timezone is not applied to DateTime.yesterday
.
[22] pry(main)> Date.yesterday
=> Thu, 21 May 2020
[23] pry(main)> Date.yesterday < before_9
=> false
[24] pry(main)> Date.yesterday < after_9
=> true
It behaves the same as DateTime.
[31] pry(main)> Date.yesterday.class
=> Date
[32] pry(main)> DateTime.yesterday.class
=> Date
Oh, even if you make it with DateTime, the instance class will be Date.
So Date.yesterday
and DateTime.yesterday
seem to be the same thing.
Recommended Posts