Rails 4 local Time shown according to local time zone - ruby-on-rails

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.

Related

Where Ruby get date, time from?

I am working in ruby, I have an object containing today's date, time from DB and only want time truncating the date. I can get,
DateTime.now.strftime("%H:%M")
I'm curious about where Ruby get date and time from? From internet, device or browser?
https://ruby-doc.org/core-2.2.0/Time.html#method-c-new
Unless given a time during initialization of a Time instance, Ruby uses the type from the system it is running on.
If you want the current time on a user's device in a Rails project, you'll need to capture it with Javascript and pass it back to your Rails application.

How to get time adjusted to user timezone in Sitecore CMS

I'm using custom command to import data for newly created item in Sitecore. Since there is a lot of data importing, this process is in the background. I send an email with import status once it is completed. I am able to get email from UserProfile:
User currentUser = Sitecore.Context.User;
UserProfile currentProfile = currentUser.Profile;
The problem is that we have import starting time in the email. Since I haven't find where the user time zone is stored, I can't adjust it to match the user time zone.
Do you happen to know if there is any setting to define user time zone from the C# code?
P.S. In the bottom right corner there is a correct time adjusted for the current user, but I didn't find any information on how and where it stored
I'm not sure it's stored (in fact I believe Sitecore stores DateTime relative to the CM server and not the client).
For you to have an accurate timezone for the visitor/customer, you'd need to either ask for it directly (registration prompt or user profile option) or look it up using Geo data from the originating ip (though with VPNs and public access points this becomes increasingly less reliable).
My suggestion would be to store the start time in UTC and (if necessary) let the user know it was started at that time. If a time elapsed is required, perform the calculation before sending the email off--since start time and end time are relative to the server anyways, just let them know it took "5min 30sec" to complete. If they receive the email at 5:08pm (email timestamp) they can do some simple match and assume it was started at 5:03 (or thereabouts).
I believe the time in the bottom right of the Desktop is using the users local system time (most likely using JavaScript) so it is not dependent on their Sitecore user profile.
There doesn't appear to be any timezone information stored against the user profile, the best you can get the is the CultureInfo based on the Regional ISO Code, although this still is not truly accurate for countries with multiple timezones (USA, Canada, Russia, Australia etc).
You could extend the User Profile to add custom properties and store a timezone field, but it is not available by default.
My suggestion would be to list the start time in UTC time, and let the user mentally adjust to their own timezone. This is what I have done previously, esp since servers themselves could be located in any location.

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

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.

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