[RUBY] [Rails] Precautions when comparing date and time with DateTime

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

Conclusion

--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.

poem

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.

debug

[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.

Date comparison

[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.

I also tried it on Date.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.

Take a look at the class

[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

[Rails] Precautions when comparing date and time with DateTime
[Rails] Search method when date and time have each column
Date and time
[Java] Precautions when comparing character strings with character strings
Change date and time to Japanese notation in Rails
[Rails] Validate the start time (datetime type) and end time
Check when created_at, datetime, etc. are not saved in Japan time in Docker and Rails
Set the date and time from the character string with POI
Sample code to parse date and time with Java SimpleDateFormat
[Rails] Understand migration up and down by comparing with change
[Rails] Save start time and end time
Precautions when replacing backticks with gsub
[Ruby] Date, Time, Datetime class basics
Precautions when creating PostgreSQL with docker-compose
[MySQL] [java] Receive date and time
[Caution !!] Precautions when converting Rails devise and view files to haml
Which class should I use to get the date and time in my Rails app (Time, DateTime, TimeWithZone)
A collection of methods often used when manipulating time with TimeWithZone of Rails
Building Rails 6 and PostgreSQL environment with Docker
Explanation of Ruby Time and Date objects
[Rails] [Memo] When to add = to <%%> and when not
[JSR-310 Date and Time API] Precautions for format definition when converting from Japanese calendar character string to date type
Format the date and time given by created_at
[Rails] Error resolution when generating tokens with PAYJP
Date and time acquisition method using DateAndTime API
Rails development environment created with VSCode and devcontainer
How to build API with GraphQL and Rails