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.
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
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"
You can also get the current time using Time.now
, but in this case there are the following differences.
config / application.rb
, but it gets the current time based on the environment variable or the system time zone, so * * There is a concern that the time will be significantly different **.Since you can use the method of TimeWizhZone class, it seems good to get it here.
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
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)
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
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)
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
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.
Recommended Posts