Timezone problems in rails3 - ruby-on-rails

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.

Related

Time module in Rails

I'm trying to create an app and I have a question about Time in rails.
My app has two models, a user and a post.
I want to limit the user to only create one post per day.
I did a validation on Time.now but I'm not sure if it's the right thing to do because a user might change the date on his machine and bypass the validation.
How do I proceed ?
Time is not taken from user's machine, but rather from the server you run your website on. So you have nothing to worry about. On a side note: it's better to use Time.current instead of Time.now as it also considers the timezones.

rails - where to store writeable system-wide variables

I've had a read of similar questions and not found a simple answer. I need to store a date value that has nothing to do with my models.
For example, let's say I have a screen which displays a graph of user activity based on all records being created after a specific date. I need to store that date somewhere. An admin user will change this initial date depending on whether they need to look at the last six months, or the last year etc. That setting will then be the same for all admin users. So it's not an admin user setting.
I thought about a config file, but then it needs to be writable and these seem idea for values that are being looked up but not edited on a regular basis?
Any advice appreciated..

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.

Get User timezone to show the correct time

I want to be able to show the correct time for every user depending on their timezones, and also be able to store all time records in my database in UTC.
Any thoughts of the best way to do this?
I wrote a blog post on timezones with rails, you can read it here: http://jessehouse.com/blog/2013/11/15/working-with-timezones-and-ruby-on-rails/
also be sure to watch the rails cast on timezones: http://railscasts.com/episodes/106-time-zones-revised

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?

Resources