Dynamic timezone UTC - ruby-on-rails

I have a system that user when registering a task determining the time that it should be done. By default rails works with UTC at the time, but I'd like to be changing the time zone according to the user's locale.
Anyone can help?

Related

What is the reference for Time.localtime

When using the localtime method on a UTC Time object, is the time returned the local time as specified in the app configuration or the local time of the user's location?
localtime will give you the time in the current time zone of the machine running the code.
Apidock(localtime): for ruby, ruby on rails
.localtime in your app will show your app's time. To display localtime to different users you will need to add some Javascript.
This answer might help you.
How to display the time in user's timezone

How can I fix iOS objective-c timezone issue?

I am in the process of developing SMS based app. When my app launches I select the last-message-date-time from database and send it to the server and I get back all the messages with date-time > last-message-date-time but because the app and server may not be in the same timezone I can't get the correct result. What is the best way to fix this issue? Please help. Thank you.
This question is the follow up of update SQLite with content of push notification when the app is not running.
One of the options is to store the time stamp in UTC/GMT on server, and when you get the date from server, convert it to local date object using GMT as formate, then show the user to their local timezone using formatters. ie.
Store the date in UTC/GMT on server.
Get the date string in app, use NSDateFormatter with GMT/UTC as timezone to convert it to NSDate object in Objective-C
Use NSDateFormatter with local timezone to show it to user.
Keep in mind that NSDate holds the date as independent of timezones so when showing it to user, use any NSDateFormatter to convert it to local or any timezone.
Here's apple's guide to managing timezones: Using Time Zones
A useful discussion here: A Fast, Accurate Way to Handle Dates from Servers
Please first convert your system's timezone to your server's timezone when you select date-time from database then send it to server.
To figure out this issue. you need to follow the following steps.
Step 1 : Always keep server time zone in UTC.
Step 2 : once you need to send Date-time to server. first need to convert it to UTC and then send to server so server stored it.
Step 3 : While fetching Date-Time from server, you need convert it with respected Time Zone. where you currently are. like if i am using app from INDIA then i will add +5:30 in receiving date from server. so all country manage same.
Hope this help you

Correcting a column in Rails Postgresql database (timezone issue)

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.

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")

Resources