Yet Another Time Zone Question

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

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

Thanks Jeremy!

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")