Get The Current Time
Working with time and time zones in server development can get complicated. Time-sensitive code that worked locally can unexpected fail when deployed to a server in a different time zone. Or users can end up seeing timestamps that look a few hours off.
To avoid this kinds of mistakes in Rails development, we should avoid using Time.now
and instead use Time.current
.
Rails saves timestamps to the database in UTC time zone. We should always use Time.current for any database queries, so that Rails will translate and compare the correct times.
My server's default time zone is UTC. Time.now
gives me my computer's system time (Central Time). Time.current
gives me the time in UTC. If I then change the server's time zone to Eastern Time, Time.now
still offers up my system time whereas Time.current
produces the current time in Easter Time.
Last updated