Offsetting Ruby Time

On , by

Managing local time is such a huge challenge when making webapps. There’s always going to be users from all over the world clocking in on different timezones. Basecamp’s local_time solves this problem in a elegant way – it doesn’t care how you store timezone, it interprets the users local time and prints it out on the view accordingly.

But, all hell breaks loose when you try to recreate this on the server side. Here there’s no information on what user is from what timezone.

And even if you persist the users timezone information, surprisingly Rails doesn’t come with an out of the box solution to offseting time(s).

Here’s a super simple way to convert a UTC time to a zone of your choice, by just inputting the offset.

  t = Time.now.utc
  => Fri, 22 Jul 2016 0738 UTC +00:00
  t.offset("+05:30")
  => Fri, 22 Jul 2016 1338 UTC +00:00

{% gist 4d20c8989172351268173c4f16421208 %}

Share this article on Twitter or LinkedIn
Previous Blog Post: Five things to consider when choosing enterprise productivity applications
Subscribe to Sudo vs Root

Our newsletter rolls out every month. No fluff. Pure content.

Comments and Discussions
Skcript https://www.skcript.com/svr/offsetting-ruby-time/ https://www.skcript.com/svrmedia/heroes/offsetting-ruby.jpg