Displaying timstamp in rails view

In my view I’m displaying a timestamp of when an IPS event occurred <%= event[:timestamp] %>. The timestamp in the database is correct, but what is displayed is four hours in the future… Why is this being changed, and how can I just have it display the actual timestamp?

Time zones. What db are you using? What is the type on the column you use to save the timestamp? How sure are you that it is correct in the database? (IOW, do you understand whether the db treats it as UTC or not tied to any time zone, and do you understand how whatever software you're using to look at it is interpreting it?)

PostgreSQL, and it’s in UTC. I’m just looking to display what is in the database, something is adding 4 hours. Using select * from event; in the database shows the right info.

What do you mean "it's in UTC"? Is the column type timestamp with time zone, or timestamp without time zone?

By simply displaying it like that you are letting the system work out
how to display it. If you want to display it in the format you want
then use the appropriate formatting helper.


Sorry, looks like timestamp with TZ.

OK. So it' timestamp with TZ, and when you select from the db using psql, you see the correct time? Postgres is translating back and forth between the time zone configured on the server on which it is running and UTC. So if that server is at UTC-400, then the values stored can appear to be 4 hours ahead if they're not correctly offset to local time or formatted correctly. (I wanted confirmation from you about that, because although I suspected you just needed to get a value from UTC into the local timezone, I've seen cases where things were configured such that there were multiple fubar's relating to timezone handling, with the timestamps stored incorrectly, and multiple offsets, so that just adding an offset from UTC to local might appear to be a fix, but would in fact just be one more offset piled on top of others that were incorrect.)

So, first puts mytimestamp.inspect, just to make 100% sure what RoR sees as the underlying value. My working theory is that it's the correct moment in time, but UTC.

Then, assuming that *is* actually the case:


And note that in /config/application.rb you can set the default time zone to something other than UTC:

config.time_zone = 'Central Time (US & Canada)'

(Use ActiveSupport::TimeZone.zones_map to get a list of available time zones.)

The config options is useful for in-house type of application where all users can reasonably be assumed to be in the same time zone (or at least, want to view all times in the same time zone). For an app which will be used across time zones, you'd have to track preferred time zones by user, develop some helper methods or filters or whatever to use the custom time zones per user, and even worse, potentially display timestamps based on criteria beyond just the user's time zone...