First, itโs important to understand that time zones in rails are mainly related to performance. Behind the scenes, everything happens in UTC.
Q1. On the console, Rails will display the time in your default time zone, so Something.last.created_at displays this timestamp in the zone specified by Time.zone
Q2. They all return an object that represents the "now." The choice of DateTime and Time is not related to time zones. If you need to imagine time outside of the unix era, for example, use DateTime. The difference between Time.now and Time.zone.now is whether you will return a Time instance (which will be in the local time zone of the server as a controller). This means, for example, that it returns to_s , but not what is displayed in time:
SomeModel.create(:time_attribute => Time.now) SomeModel.create(:time_attribute => Time.zone.now)
will insert the same row into the database. If you just show the user the time (for example, if you show the current time in the website header), you should use Time.zone.now so that it displays in the correct time zone. If you just store it in db, then that doesn't matter - activerecord will convert it to TimeWithZone anyway.
Q3. The comparison methods on TimeWithZone implemented by comparing the version of utc date, so you can safely compare times that are in different zones - you do not need to convert them to some common time zone. You can also compare TimeWithZone instances with regular time objects.
Q4. Usually you do not need to do anything. A common way to implement this would be to have a published_at attribute for your model. Pages displaying articles in a list would add
where('published_at <= ?', Time.now)
condition for the request. When you create your article, Rails takes the date time from the form and converts it to utc, so what is stored in the database is a utc version of this published_at time. The comparison is time zone independent, so the query works no matter what time zone.
This can become more complicated with the net time of the day (for example, โ4 pmโ), because changing the time zone can only be done when you know the date too (i.e., you know the exact time), because depending on the DST (or political events, such as countries that change their time zone), the bias from changes in the UTC format. In such a situation, you usually want to store only the time of day and only convert it to full time as late as possible, when you know the date too.
Q5. Rails always stores UTC in the database. A direct consequence of this is that changing config.time_zone does not require changing the data stored in the database. You can even set Time.zone for each user so that users see the time in their time zone - config.time_zone just controls the default value of Time.zone