开发者

How can I capture the original timezone of an iso8601 datetime?

My rails 3 app needs to interact with an external webservice. This webservice provides information about raining events across the entire US (ultimately the country doesn't matter). The pertinent information is start/end time formatted as ISO8601 including timezone. The server will run on the east coast of the US (so EST, soon to be EDT). All database entries will be stored as UTC.

I need to be able to store the timezone(TZ) in which the event takes place so I present the user with options to view the events in their own TZ or in the originating TZ. Once they reach the location they won't care if the event starts based on their HOME TZ but TZ in which the event will happen.

input data:

start = "2011-04-08T10:00:00-06:00"
  end = "2011-04-08T16:00:00-06:00"

#fyi
Time.zone => GMT-05:00 Eastern Time US  Canada

# Server time zone, original time zone lost !!!!
Time.parse(start) => 2011-04-08 12:00:00 -0400

# UTC, time zone lost
Time.zone.parse("2011-04-08T10:00:00-06:00") => Fri, 08 Apr 2011 16:00:00 UTC 00开发者_如何学Go:00

I don't see any way (minus manual string manipulation) to get the original, -06:00 TZ (MST). I was really hoping there would be a RoR way get this info ... seems like there should be a way!


If you use Time you will lose information, but if you use DateTime you will be fine:

DateTime.parse("2011-04-08T10:00:00-06:00")
# => Fri, 08 Apr 2011 10:00:00 -0600

The DateTime class is much more robust than Time and can encode a lot more information, though is slower and more memory hungry. Fortunately, performance will not be an issue unless you are spending most of your time manipulating values like this in large batch operations.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜