When objects are created in our Rails app, it's saving in the Postgres DB 7 hours ahead of our time (PDT). Looks like it's saving everything to UTC. This really screws up analytic graphs, ect.
I have these set in application.rb
config.time_zone = 'Pacific Time (US & Canada)'
config.active_record.default_timezone = :local
How can I get everything to save/timestamp to the PDT timezone?
It is (probably) being saved at the timezone of your server, and is independent of your code. IE, it's your database setting the created_at/updated_at date time (which is defaulting to UTC)
What you need to do is make your analytics work with UTC and translate to PDT, not vice versa. That gives you a solution that is totally independent of where you are hosted, daylight savings time changes, where your users are, etc.
Consider the simple case of if you have users in multiple time zones. Someone in EST will hate you west coast PDT people 'cause they always have to translate it ;)
All in all, use and work with UTC - it's... Universal!
Related
My model has (by default) generated a "created_at" field in the database that stores when the row was inserted into the database in UTC.
In my controller, I retrieve the data from the database and try to convert the value to Eastern time with the following:
data["created_at"] = data["created_at"].in_time_zone("Eastern Time (US & Canada)")
When I view the output of the above code, it looks like my "created_at" value is still in UTC instead of Eastern time. Any thoughts? Thanks.
EDIT:
To answer a comment, when I inspect the value of created_at coming from the database, it's value is: 2015-01-31 18:34:51
In your config in application.rb add timezone
config.time_zone = 'Eastern Time (US & Canada)'
it will keep your dates in db in utc, but display with specified timezone
The timestamp is still the timestamp. You're simply specifying a possible way to display it.
What might make more sense is to leave it in UTC in the database (a good standard) and just localize it upon display in whatever way makes sense.
For example, with a logging app we wrote, hovering over the UTC timestamp gives you the localized timestamp for yourself based on your user profile in the system.
I upgraded to Rails 3.2 and Ruby 1.9.3 and I get a very strange behavior
I have a Model called Entry. when a new Entry is created it get inserted into the DB with the current date and time.
But when I run Entry.find_by_id(THE_ENTRY_ID).created_at I get the "correct" created at datetime but In a different TimeZone for example:
in the db:
2013-03-24 00:05:29
while in the Rails console and in the application:
Sat, 23 Mar 2013 20:05:29 EDT -04:00
Why is it suddenly returning the wrong time zone?
OK after reading the DOCS and seeing this rails casts:
http://railscasts.com/episodes/106-time-zones-revised
everything is now clear.
config.time_zone = 'Eastern Time (US & Canada)'
this setting is the actual setting that's in charged of converting all time objects to this current time zone. that is way i saw it as -4
this setting:
config.active_record.default_timezone
is the one that decides on how to save the time in the DB, but... it only gets 2 possible values: :local and :UTC and the default is :UTC
when i used the rails console to see my entry the time presented is after the conversion to 'Eastern Time' , in order to see the actual date stored in the DB use:
created_at_before_type_cast
which returned the UTC time as it is the default
and the reason i thought that in the DB its stored in my time zone +2
is because i used MySQL Workbench and its GUI just showed me the datetime values in my local machine time.
summary:
the setting that actually matters:
config.time_zone
it works just fine.
going through the DOCS is important
Check the configuration in the application.rb. If no timezone is set, rails will take the default timezone of your operating system. To fix that, set UTC as the timezone.
I am using Rails 2.3.3 with Postgresql.
In my application environment.rb file I have set config.time_zone = 'UTC' and in my before_filter I set Time.zone = current_user.time_zone.
The problem I face is the Time that gets saved is in the users time_zone but gets saved as UTC in the database.
For example if I select 12:00 am IST (i.e +530 IST) it gets stored as 12:00 am UTC. However locally the configurations seem to work as expected and while fetching the data the time gets converted to the users time_zone so it gains 530 hours.
Would appreciate some help on this.
Thanks
Do you use time or datetime as the database column type for your object?
This recent (and very similar) question to yours could be resolved by changing the type from time to datetime.
Here is my current situation:
I have a user class that has an attribute time zone.
When the user creates a Lecture with start_time (3pm) and end_time (5pm), I want to ensure that in the database the start_time and end_time are actually 3pm and 5pm, in the user's time zone.
In my application controller I'm doing the following:
def set_timezone
if current_user
Time.zone = current_user.time_zone or "Eastern Time (US & Canada)"
end
end
When the above lecture is saved to the database it seems to add 4 hours. Is it converting it back to UTC? The odd thing is that when I display the time in the view it is correct (I'm guessing it's converted from the UTC time back to the EST time).
I want it so that that if a user selects 4pm it is saved in the database as 4pm AND when I display that time in the view it is 4pm. What am I currently doing wrong?
EDIT: it appears that rails also converts all times to UTC when storing them in the database. Which is ok until I do a query that involves time (I'll have to manually convert it to UTC).
MySQL also has a time zone.
Notably (emphasis mine):
The current session time zone setting affects display and storage of time values that are zone-sensitive. This includes the values displayed by functions such as NOW() or CURTIME(), and values stored in and retrieved from TIMESTAMP columns. Values for TIMESTAMP columns are converted from the current time zone to UTC for storage, and from UTC to the current time zone for retrieval.
Whether or not it's worth doing anything about it... not sure. I'm skeptical.
I have a database that is written to by a non-rails application and read from by a rails application. There is a datetime field in the database and the data stored in this field is stored in Eastern Time. In my rails app, I understand I can set the application's timezone in my environment.rb file by doing config.time_zone = 'Eastern Time (US & Canada)'. However, rails assumes the data stored in the database is stored in UTC and converts from config.time_zone to UTC as it pulls information in and out of the database.
Is there a way to tell rails the data in this particular field, or even all my datetime fields, is Eastern Time and not UTC?
On Rails 3.0.5 in application.rb, this works:
config.time_zone = 'Central Time (US & Canada)'
config.active_record.default_timezone = :local
config.active_record.default_timezone - Tells ActiveRecord to interpret database times as being in the specified time zone, and to use this timezone for working with the created_at & updated_at attributes.
my first suggestion is to fix the other application. The other application should be storing a UTC offset with the date. If you try working around a problem, you are just going to create more. For instance, what happens when daylight savings time goes, then are the offsets going to change?
That being said and having no other choice - I would do something along these lines, for a field created_at:
def created_at_in_eastern
DateTime.parse(self.created_at.strftime("%D %T EST")) if self.created_at
end
or you can use EDT depending on how the data is saved, just read the disclaimer at the top - this is a hack!