Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the time in user's timezone

I am using rails 3.0.5 and I have created_at and updated_at stored in UTC. Now I want to display the created_at time in users' timezone. I believe it is possible to pick user's timezone from the browser and then convert time to user's timezone.

I am sure rails will have a gem/plugin to take care of something like this. Is there something?

like image 771
Nick Vanderbilt Avatar asked Mar 10 '11 23:03

Nick Vanderbilt


People also ask

How do I get user time zones?

You can set any specific time zone using the PHP function date_default_timezone_set . This sets the specified time zone for users. Basically the users' time zone is goes to the client side, so we must use JavaScript for this. Below is the script to get users' time zone using PHP and JavaScript.

How do I get a timestamp with time zones?

CURRENT_TIMESTAMP returns the current date and time in the session time zone, in a value of datatype TIMESTAMP WITH TIME ZONE . The time zone offset reflects the current local time of the SQL session. If you omit precision, then the default is 6.

What is user time zone?

The user time zone is a client-specific time zone that can be defined for the user time and user date of each individual user in the user master record. It is contained in the system field sy-zonlo.


2 Answers

Rails by default converts every date into UTC before storing the value into the database. This means that, regardless the server timezone, you always have UTC dates in your database.

In order to convert the dates into your user's timezone you have basically two possibilities:

  1. server-side approach
  2. client-side approach

Server-side approach

If your site allows registration, you can store the user timezone as user preference. In the user table, store the user timezone. Then create a custom helper you can use to format any date/time into the proper timezone using the in_time_zone method.

> t = Time.current # => Mon, 23 Dec 2013 18:25:55 UTC +00:00  > t.zone # => "UTC"  > t.in_time_zone("CET") # => Mon, 23 Dec 2013 19:25:55 CET +01:00  

Your helper may looks like

def format_time(time, timezone)   time.in_time_zone(timezone) end 

I normally also like to output a standard format, using the I18n.l helper

def format_time(time, timezone)   I18n.l time.to_time.in_time_zone(timezone), format: :long end 

Client-side approach

If your site has no registration or you don't want to ask your users for their timezone or you simply want to use the user system timezone, then you can use JavaScript.

My suggestion is to create a custom helper that will print out every time in a proper way so that you can create a generic JavaScript function to convert the values.

def format_time(time, timezone)   time = time.to_time   content_tag(:span, I18n.l(time, format: :long), data: { timezone: timezone, time: time.iso8601 }) end 

Now, create a JavaScript function that is performed on DOM load and that will select all the HTML tags with data-time attribute. Loop them and update the value inside the span tag with the proper time in the given timezone.

A simple jQuery example would be

$(function() {     $("span[data-time]").each(function() {         // get the value from data-time and format according to data-timezone         // write the content back into the span tag     }); }); 

I'm not posting the full code here, since there are plenty of JavaScript time formatters available with a simple search. Here's a few possible solutions

  • Convert date to another timezone in JavaScript
  • Convert date in local timezone using javascript
like image 194
Simone Carletti Avatar answered Sep 24 '22 23:09

Simone Carletti


There is a nice gem by Basecamp called local_time for client side rendering - https://github.com/basecamp/local_time. It's great for applications where user is not signed in and it's caching friendly.

like image 21
Strika Avatar answered Sep 24 '22 23:09

Strika