On Tue, May 12, 2009 at 6:31 PM, Jeremy Olliver
<jeremy.olli...@gmail.com> wrote:
> On May 12, 10:57 am, Randall <callmenanner...@gmail.com> wrote:
>> 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.
...
>>
>> 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/
>
> 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/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to