Correcting a column in Rails Postgresql database (timezone issue) - ruby-on-rails

When I built my web app it was, unknowingly to me, set to use UTC by default. One of my model's attributes is a DateTime called event_date which is manually set by the user. The users thought they we're selecting CST while behind the scenes everything was still in UTC.
Now I am pulling events from Facebook Graph API, and they display the "wrong" time (what I recently found out is actually the correct UTC time). When I changed my application.rb file to include
config.time_zone = 'Central Time (US & Canada)'
It solves the issue of the Facebook times being wrong. However, all of my previous database entries are now incorrect due to their event_date's being entered as incorrect UTC times.
What is the best way to go about resolving this? I assume the best method would be to convert or offset the entire event_date column of my database to the correct UTC time, but I am unsure how to go about doing this.
The app is built with Ruby on Rails, I'm using Postgresql, and I am deploying through Heroku.

Related

Rails 4 local Time shown according to local time zone

I am working on rails 4 application. I want to show the time for comment I created. If I open the site in india then time should be shown in IST (according to indian standard) and If I am in USA so for the same comment that i made in india time should be shown according to USA time zone.
What do I need to do in my config file for development and production?
Do I need to change anything in database?
Please help me.
Rails always saves times in UTC (universal time), and the server has a setting which tells it which timezone it (the server) is running in.
To show different times to the client, Rails (which runs on the server) will need to know which time zone the client is in. This isn't in a standard request header so you will need to get them to submit the information somehow. Once you know their timezone you can ensure that you always show times to the user using their timezone - there are helpers for this.
Getting their timezone can be done explicitly, eg by giving them a timezone dropdown in their "My Account" page, and then saving that in their user record, and/or by making it more upfront and forcing them to choose one in a popup, if you don't know it.
Or, you can do it for them using Javascript, passing it through in a cookie. See this article for an example of how to do it.
http://thisbythem.com/blog/clientside-timezone-detection/
Well one solution can be to store the time zone of the user in the database, write a filter
around_action in your ApplicationController which would set the Time.zone to the time_zone from the database field.
You might want to look at Time.zone and TimeZone in the rails api
Here is a railscast , you can figure it out from the comments and the github link.

Mutliple timezones in Postgres database rails

I think I want to store two different datetime attributes in my model, one client local time one UTC in my postgres database. From what I understand, rails prefers that everything be written as one time zone making my job frustrating.
How do I do this with rails and/or why shouldn't I do this with rails?
The reason why I think this needs to be done is because I can display the local time to the user and use the UTC for sorting and charting.
you can convert UTC time to local time using. For eg, if the local timezone is 'Alaska'
time_from_database.in_time_zone("Alaska")

Timezone problems in rails3

In my rails application,After login a user has to create a timesheet entry.The time of creation of the entry is currently my server time.Whereas i want it to be the time of the timezone from where the entry is made i.e if entry is made from any other country.I'm using rails 3 and after searching the web also exact solution cannot be achieved.
Thanx
You can't automatically determine the user's timezone, but you can allow them to choose their own timezone. Then you can set Time.zone = ActiveSupport::TimeZone(offset) before calling update and everything should work correctly. You could also save each user's preference for time zone in your user model.
I'm not sure if it's possible to do this in Rails as the issue is getting the physical location of a user.
It may be worth using JavaScript to populate a (hidden?) field with the current time, which should be taken from user's local machine. You could then explicitly set the created_at field to be this value.
Intrigued to know if Rails can get around this somehow...
If you want, take a look to this question : transform time into local time in Ruby on Rails
And : http://www.wetware.co.nz/blog/2009/07/rails-date-formats-strftime/
Maybe it can helps you.

Grails application serving multiple timezones

What is the correct way to deal with timezones in a grails application that has users in multiple timezones? Is there a standard aproach to muilt timezone Grails applications? Maybe there is something simlar to the way Rails handles multi timezone support? http://mad.ly/2008/04/09/rails-21-time-zone-support-an-overview/
Is there a way to set the current timezone per user session? This would mean you could have multiple active user sessions with different timezones being used for dealing with ”Date”.
Im trying to avoid having to manually deal with timezone conversions.
Thanks
First, it is often a good idea to present the user with a choice - which timezone he is in. You can try to infer it from his settings, but that's not reliable. In grails you can let the user select his timezone by:
<g:timeZoneSelect name="myTimeZone" />
Note that the value attribute defaults to the current Locale. So I'd guess grails' default localeresolver will do a fine job guessing the locale of the user.
For that to work you need to store all times in the DB in UTC (or another timezone which is fixed for the whole application)
The documentation of <g:formatDate> claims it has only 3 attributes, but it seems since at least version 1.2 it supports a timeZone attribute. So you'd have to put timeZone="${currentUser.timeZone}"
You could try using JodaTime and DateTime class, it has timezone inside. Still, I believe you'll need to store times in UTС. For that, you'll have to provide own mapping to GORM that applies UTC, own date formatting taglib that takes into account user timezone, and some more things.
OTOH, if you have some common functionality, like background jobs (I believe you will), what time zone is it going to use?

How can I display the correct created and modified times in my webapp?

I'm working on a Rails application that's kind of like a blog. Users create Entries. I'm trying to work out how to handle time storage and display. I've read this writeup about Rails timezone support.
It's great but it doesn't cover what my app needs to do. It only looks at cases where you want to convert stored time to the current logged in user's time zone. In contrast, the effect I want is...
A user creates an entry in California at 10:00 a.m.
A couple years later he moves to New York and then at some point looks at his old entry. The "created" date should say "10:00 a.m." He doesn't care about time zones. He just wants to know what time of day he felt like it was when he wrote the entry.
If he then edits the Entry in New York the displayed "modified" date is, again, his subjective time of day when he made the edit. (Let's assume he went to "preferences" and changed his time zone setting when he moved.)
Also, for the sake of thoroughness, the app should be able to report the "real" absolute time when an Entry was created or updated.
(Note -- my imaginary user is a guy, but for women it should work roughly the same way.)
The way I'm thinking of implementing it is...
Have the attributes User#time_zone, Entry#created_at_utc, and Entry#updated_at_utc in addition to the standard created_at and updated_at.
The user selects their time zone from a menu when they sign up. (They can change it later if they want.)
The app uses User#time_zone to store created_at and updated_at in the user's subjective local time. If it's 10:00 a.m. for them, the app writes "10:00 a.m." to the DB.
The app also saves the current UTC time in the aforementioned _utc fields to deal with the last requirement above.
Is that a good way to do it? Is there a better way?
The two roads you can take are:
Store a timezone (UTC) in the user account as well as in every post - update the post's timezone along with the updated_at field whenever the user changes the post (if he or she has changed timezones).
Store the timezone only in the user account. When the user changes timezones, update every post that belongs to the user and add/subtract to the created_at/updated_at dates.
The first option seems like the cleanest option to take. For this you would only have to create a new method in your post record:
def locational_updated_at
updated_at + timezone.seconds
end
Where timezone is an integer containing the seconds since UTC.
If you can, you should avoid storing two different sets of timestamps, and you should avoid storing any non-UTC dates. Both of these things will lead to confusion. I'm not completely sure I understand what you're doing (though I like your idea of subjective time), but wouldn't it be enough to just attach a time zone to every post, and always use that zone to display the times? It would default to the time zone set in the author's account, so he could change it when he moved cross-country without affecting previous posts.
I think that's all you need--to attach a time zone to every post. Is that sufficient? Or am I missing some part of this?

Resources