Skip to main content

Forum Series Part 7: Time Zones using the local_time Gem Discussion

General • Asked by Chris Oliver
4154ca8c77effcafcb549518bb85c22f

strfti.me is the time zone website recommended in the video.

foragoodstrftime.com is another one to check out.


Bf93c0e0e07432803cd8ece19bd5fa73

How to establish Russian?

91c11f0640e132006aaddce32c727fe7

As @excid3:disqus said before, see Rails/I18n for local_time gem documentation here: https://github.com/basecamp...


77c2287165badfe2c0df265cd9ccf94d

How to use Russian lang? help

Ce795239ba5dd2384fc2f88ffaff5451

Check out the gem documentation for some more info on I18n https://github.com/basecamp...

77c2287165badfe2c0df265cd9ccf94d

2365845904523adecbf146a17a48f509

Is this the last one of the series?

Ce795239ba5dd2384fc2f88ffaff5451

Yeah it was not a very good series. I didn't show the whole thing and just emphasized certain parts. Are you interested in seeing the whole thing? I might do a revised series on it or something similar.

B71ee9aa757eb04452ddd2ba2ca3b393

I thought it was a very good series, you gave out all of the pieces necessary to build a decent forum. With a solid base like this anyone can expand on it through their own research and add extra features as necessary.

B4f51215efb2ab4fa5dfb71705c351c8

Hi am very interested in this series I am building something similar.
Thanks!


A8239a0b67a6ea7b3a98d467e6bdfe6e

Nice tutorial Chris! Could you tell me how you would use this gem in a rails form where the date is selected? So let's say I wanna see and choose the datetime in PST in the form (w/ datetime_select or bootstrap datetimepicker), but wanna have it saved to db in UTC.

Ce795239ba5dd2384fc2f88ffaff5451

ActiveRecord should always save UTC to the database unless you changed that. You can set the default time_zone in Rails to be PST and keep the activerecord timezone as UTC in order to have all form submitted times process in PST and then get saved as UTC. http://stackoverflow.com/qu...

The other thing is you could add some code either to your controller or model to set the timezone of the submitted attributes. Either approach should do the trick for you. Any form datetimes that you submit have to get cast to an actual Time object in Rails in anyways so you'd just be adjusting that process.

A8239a0b67a6ea7b3a98d467e6bdfe6e

Thanks Chris! I will try the 2nd approach since everybody says it's better to keep (server,app,db) everything in UTC.


9feed97ff0feecf9551909af719e3107

My use case is not to ask users about their location, but automatically set timezone based on their GeoLocation. Any suggestions wrto gem out there will be really helpful ?

Ce795239ba5dd2384fc2f88ffaff5451

That's what local_time does. :)

9feed97ff0feecf9551909af719e3107

Interesting, Let me play with it...


Eb9f122d9886ef39e8f42b480028490b

Hey Chris! This is great. One question: How would you go about inputting time? Lets say the user is making an event or appointment. They are presented with date/time input fields. How would you handle that if different users are in different time zones? Would that require storing a time zone on the user/account?

Eb9f122d9886ef39e8f42b480028490b

Looks like this question was answered below. My use case is a multi-tenant app. All the users would be in the same place. So maybe I store the time zone on the account. Wonder if there is a way to specify a dynamic time zone like you would in application.rb based on what the account has selected.

Eb9f122d9886ef39e8f42b480028490b

So in the end, still a PITA, haha. I think I'd prefer your first approach in the answer below...but somehow set the time zone based on the account. The feel like the best solution would be to not have to litter models/controllers with time parsing zone parsing code and to just allow the account to set their time zone and have it just work.

Eb9f122d9886ef39e8f42b480028490b

Ab5f83de6f83e465d9d8f606b0b048c0

Hi Chris,

How do I add the local-time.js to my app using the local_time gem, but not using the asset pipeline? I see an option to add it using the npm package (see below) but I am not sure how this works and where to put it.

import LocalTime from "local-time"
LocalTime.start()

Please advise.

MDR


71805a7a241676432f7827e393c7bff0
I can't seem to get this to work. I just get: undefined method 'local_time'.

Do you know if there's a problem with this gem and Rails 5.2? I can see the Gem is installed and in the asset pipeline. 

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 18,000+ developers who get early access to new screencasts, articles, guides, updates, and more.

By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

More of a social being? We're also on Twitter and YouTube.