[RUBY] A collection of methods often used when manipulating time with TimeWithZone of Rails

I often used the Rails app to calculate the price for a specific period and to play with the time to verify it, but I was impressed by how easy it was to get the time I wanted, so I used it at that time. Time manipulation methods are summarized instead of memorandum.

What is Time or TimeWithZone in the first place?

I will introduce it because it was written in detail in the following article. In conclusion, when using it with Rails, using TimeWithZone makes a lot of progress. This article assumes that you will be using this class. Differences between Time, Date, DateTime, TimeWithZone between Ruby and Rails -Qiita

Get the current time

Super basic time acquisition system. For the time being, it's basic. I think that it is often converted into various shapes based on this.

#Get the current time
Time.current
=> Wed, 15 Jan 2020 13:18:07 UTC +00:00

#Check class
Time.current.class
=> ActiveSupport::TimeWithZone

#Convert to string type
Time.current.strftime("%F")
=> "2020-01-15"

Tips: Difference from Time.now

You can also get the current time using Time.now, but in this case there are the following differences.

Since you can use the method of TimeWizhZone class, it seems good to get it here.

Get the day before or the next day

I would like to get yesterday's data and next month's data, but at this level it is convenient because I can bring it in one shot as shown below.

#yesterday
Time.current.yesterday

#next day
Time.current.tomorrow

#last month
Time.current.prev_month

#Next month
Time.current.next_month

#Beginning of the month
Time.current.beginning_of_month

#End of month
Time.current.end_of_month

#Previous year
Time.current.prev_year

#following year
Time.current.next_year

Get the time N days, N months, N years ago

If you want to change it on any date or month, you can get it by writing as follows.

#3 days ago/3 days later
Time.current.ago(3.days)
Time.current.since(3.days)

#3 months ago/3 months later
Time.current.ago(3.month)
Time.current.since(3.month)

#3 years ago/3 years later
Time.current.ago(3.years)
Time.current.since(3.years)

Get the date and day of the week

When calculating the number of days, each number can be brought in by writing as follows. The type at this time is a basic int type.

#Year
Time.current.year
=> 2020

#Month
Time.current.month
=> 6

#date
Time.current.day
=> 7

Time difference

Subtracting TimeWithZone returns UNIXTIME (seconds). By converting this using Time.zone.at, you can use it when you want to get the date etc. from the difference between two dates and times.

#Convert time from setting to TimeWithZone
time1 = Time.zone.parse('2020-01-05 12:00:00')
time2 = Time.zone.parse('2020-01-01 10:00:00')

#Subtract to get UNIXTIME (seconds)
time1 - time2
=> 352800.0

#Convert seconds to TimeWithZone
Time.zone.at(time1 - time2)

Get any time

It is used when you want to acquire the data generated on a specific date in a test.

Time.zone.parse('2020-01-01')
=> Wed, 01 Jan 2020 00:00:00 UTC +00:00

Time.zone.parse('2020-01-01 12:00:00')
=> Wed, 01 Jan 2020 12:00:00 UTC +00:00

#Create a new TimeWithZone based on the current time
Time.zone.parse("#{Time.current.strftime('%F')} #{Time.current.hour}:00:00")
=> Wed, 15 Jan 2020 13:00:00 UTC +00:00

Summary

Before I touched it, I was a little weak at time control, but I learned more by touching it. If there are other convenient operation methods that can be done, I will add them.

Articles that I used as a reference

Recommended Posts

A collection of methods often used when manipulating time with TimeWithZone of Rails
List of methods used when manipulating strings
Ruby methods often used in Rails
A collection of RSpecs that I used frequently
A collection of methods that replace characters entered in hashtag search with a nice feeling
A review of the code used by rails beginners
[Rails] Precautions when comparing date and time with DateTime
A collection of commands that were frequently used on heroku
Be careful of initialization timing when using MessageEncryptor with Rails 5.2 / 6.0
Summary of initial work when creating an app with Rails
A series of steps to create portfolio deliverables with Rails
When I used Slick on Rails, it competed with Turbolinks.
Existing records disappear when building a model of has_one (rails)
The story of the first Rails app refactored with a self-made helper
[Rails] Avoid SELECT issued when creating a model with belongs_to defined!
Mechanism and characteristics of Collection implementation class often used in Java
Naming convention when creating a new controller or model with rails