# Yet Another Time Zone Question

**URL:** <https://www.ruby-forum.com/t/yet-another-time-zone-question/167757>\
**Category:** Rails\
**Created:** [May 12, 2009, 9:56am UTC](https://www.ruby-forum.com/t/yet-another-time-zone-question/167757 "2009-05-12T09:56:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![randall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@randall](https://www.ruby-forum.com/u/randall)\
**Post date:** [May 12, 2009, 9:56am UTC](https://www.ruby-forum.com/t/yet-another-time-zone-question/167757/1 "2009-05-12T09:56:32Z")

</div>

Seems like there should be an easy solution to this problem, yet I  
can’t seem to find one.

Here is the issue:  
How do you assign a time zone to a Time object that results from a  
parsed date/time string?

Example:

- I receive a date time string from a date picker widget that does not  
include time zone info (i.e. “Mon May 11 18:24:37 2009”).
- Time zone for this date is known and may be different than what’s  
local to the user
- date/time is stored in the DB as unix time (i.e. 1242075962)
- time zone is stored in the DB in tz format i(i.e. ‘America/  
New\_York’)
- I would like to be able to take a given date with its known time  
zone and calculate the equivalent unix time value.

> > now = Time.now  
> > =\> Mon May 11 18:24:37 -0400 2009
> 
> > now.to\_i  
> > =\> 1242080677

> > parsed = DateTime.parse(“Mon May 11 18:24:37 2009”)  
> > =\> Mon, 11 May 2009 18:24:37 +0000
> 
> > parsed.to\_time  
> > =\> Mon May 11 18:24:37 UTC 2009
> 
> > parsed.to\_time.to\_i  
> > =\> 1242066277
> 
> > Time.at 1242066277  
> > =\> Mon May 11 14:24:37 -0400 2009

It seems there should be a way to append the time zone to the string  
before it is parsed or to associate the parsed time with a time zone  
(without converting the time to the zone), however I can’t seem to  
find an answer.

Any help would be greatly appreciated

---

<div class="post-metadata">

**Author:** ![randall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@randall](https://www.ruby-forum.com/u/randall)\
**Post date:** [May 13, 2009, 12:32am UTC](https://www.ruby-forum.com/t/yet-another-time-zone-question/167757/2 "2009-05-13T00:32:07Z")

</div>

Hi,

The time zone is encoded in the string at the end e.g

Mon, 11 May 2009 18:24:37 +1200  
Is in a time zone that’s +12 (12 hours ahead of utc)  
DateTime.parse or Time.parse will read this correctly.

So what you’re doing is using a js date picker that gives you a string  
like “Mon May 11 18:24:37 2009”  
You just need to append the time zone difference to “Mon May 11  
18:24:37 2009 +1200” etc before parsing.

I handle storing dates in the db a little differently than you, but if  
you know what you’re doing don’t worry about changing that part.  
I usually store dates in the db in utc format (I think you can  
configure this in environment.rb from memory), and you can easily set  
the user’s timezone in a filter in application controller  
Time.zone = user.time\_zone  
which will handle all of the conversions for you.

Cheers,  
Jeremy

---

<div class="post-metadata">

**Author:** ![randall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@randall](https://www.ruby-forum.com/u/randall)\
**Post date:** [May 13, 2009, 9:34pm UTC](https://www.ruby-forum.com/t/yet-another-time-zone-question/167757/3 "2009-05-13T21:34:38Z")

</div>

Thanks Jeremy!

---

<div class="post-metadata">

**Author:** ![randall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@randall](https://www.ruby-forum.com/u/randall)\
**Post date:** [May 18, 2009, 9:12pm UTC](https://www.ruby-forum.com/t/yet-another-time-zone-question/167757/4 "2009-05-18T21:12:20Z")

</div>

On Tue, May 12, 2009 at 6:31 PM, Jeremy Olliver  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > include time zone info (i.e. “Mon May 11 18:24:37 2009”).
> > 
> > - Time zone for this date is known and may be different than what’s  
> > local to the user
> > - date/time is stored in the DB as unix time (i.e. 1242075962)
> > - time zone is stored in the DB in tz format i(i.e. ‘America/  
> > New\_York’)
> > - I would like to be able to take a given date with its known time  
> > zone and calculate the equivalent unix time value.  
> > …
> > 
> > It seems there should be a way to append the time zone to the string  
> > before it is parsed or to associate the parsed time with a time zone  
> > (without converting the time to the zone), however I can’t seem to  
> > find an answer.
> > 
> > >

> The time zone is encoded in the string at the end e.g
> 
> Mon, 11 May 2009 18:24:37 +1200  
> Is in a time zone that’s +12 (12 hours ahead of utc)  
> DateTime.parse or Time.parse will read this correctly.
> 
> So what you’re doing is using a js date picker that gives you a string  
> like “Mon May 11 18:24:37 2009”  
> You just need to append the time zone difference to “Mon May 11  
> 18:24:37 2009 +1200” etc before parsing.

Careful here, there’s a diference between a time zone like  
“America/New\_York” and a time zone offset  
[http://www.w3.org/TR/timezone/](http://www.w3.org/TR/timezone/)

> I handle storing dates in the db a little differently than you, but if  
> you know what you’re doing don’t worry about changing that part.  
> I usually store dates in the db in utc format (I think you can  
> configure this in environment.rb from memory), and you can easily set  
> the user’s timezone in a filter in application controller  
> Time.zone = user.time\_zone  
> which will handle all of the conversions for you.

Or to convert to an arbitrary timezone

Time.at(unix\_time).in\_timezone('America/New\_York")

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
