Time.now from server instead of client computers
If I am not mistaken, the Time.now
function in Ruby is taken from the client's computer. How can we ensure that it's taken from the server, so that it remains the same time as the 开发者_StackOverflow社区server even if the client's date/month/year is wrong?
Thank you.
Time.now
is executed on the server.
If you'd like to set the timezone, add the following in config/application.rb
config.time_zone = 'Central Time (US & Canada)'
To see all the timezones available, run the following command in your terminal
rake time:zones:all
In older versions of rails (rails <= 3.x
), you can use
rake -D time
Time.now is always evaluated on the server running the code. In the case of a Ruby on Rails application, this would be the web server.
Time.now
only reports the time of the machine running the script. On its own the interpreter can't run code on a user's machine.
精彩评论