# How can I set the default time\_zone to the local time zone

**URL:** <https://www.ruby-forum.com/t/how-can-i-set-the-default-time-zone-to-the-local-time-zone/190668>\
**Category:** Rails\
**Created:** [June 24, 2010, 9:49pm UTC](https://www.ruby-forum.com/t/how-can-i-set-the-default-time-zone-to-the-local-time-zone/190668 "2010-06-24T21:49:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rakeshahuja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecccb3/32.png) [@rakeshahuja](https://www.ruby-forum.com/u/rakeshahuja)\
**Post date:** [June 24, 2010, 9:49pm UTC](https://www.ruby-forum.com/t/how-can-i-set-the-default-time-zone-to-the-local-time-zone/190668/1 "2010-06-24T21:49:33Z")

</div>

Here is the entry in the environment.config to set the time\_zone:

# config/environment.rb

config.time\_zone = â€˜UTCâ€™

Instead of setting this to a particular time zone, is it possible to set  
this to the local time zone?

Thanks,  
-Rakesh

---

<div class="post-metadata">

**Author:** ![rakeshahuja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecccb3/32.png) [@rakeshahuja](https://www.ruby-forum.com/u/rakeshahuja)\
**Post date:** [June 25, 2010, 3:48pm UTC](https://www.ruby-forum.com/t/how-can-i-set-the-default-time-zone-to-the-local-time-zone/190668/2 "2010-06-25T15:48:38Z")

</div>

On Jun 24, 3:49 pm, Rakesh A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Here is the entry in the environment.config to set the time\_zone:
> 
> # config/environment.rb
> 
> config.time\_zone = ‘UTC’
> 
> Instead of setting this to a particular time zone, is it possible to set  
> this to the local time zone?

I don’t believe there is a way to set the local time zone in the  
environment file.

Now, I’m reading a book on Rails that has a section about time zones  
and it states that you can use a pure-Ruby gem called TZInfo with a  
TimeZone class that correctly handles DST (Daylight Saving Time).  
You’ll need to install both the tzinfo gem and the tzinfo\_timezone  
plugin, but it’s reportedly slow (it could be fast for whatever you’re  
doing)

There’s also another solution, but for Mac users. I’m not sure if this  
still works or not, but I’ll give you the code anyways and you can  
play around with it.

## COPY/PASTE

You would add the following code to one of your application’s helper  
modules or put it in tis own class within the lib folder:

# to convert posted date/time to UTC and save to DB

def user2utc(t)  
ENV[“TZ”] - current\_user.time\_zone\_name  
res = Time. local(t.year, t.month, t.day, t.hour, t.min,  
t.sec).utc  
ENV[“TZ”] = “UTC”  
res  
end

# to display date/time in a view

def utc2user(t)  
ENV[“TZ”] = current\_user.time\_zone\_name  
res = t.getlocal  
ENV[“TZ”] = “UTC”  
res  
end
