CoreData 中的 NSDate 是否封装了时区?
我将日期和时间保存在 CoreData 存储中,并且需要准确地向用户呈现最初输入的时间。问题是,如果他们在东海岸输入 4:00,然后查看西海岸记录的时间,就会显示为 1:00,因为 iPhone 将世界时间转换为当地时间。我需要它显示 4:00——可能是 4:00 (+3h00)。
在我去重构我的核心数据模型之前,我想确保没有办法从存储的 NSDate 对象中派生出在创建时处于活动状态的 NSTimeZone 。有没有?如果没有,有什么建议如何最好地在创建时捕获 NSTimeZone 吗?一个 NSNumber 代表创建时间 NSTimeZone 与 GMT 的时差?
谢谢。
I keep dates and times in my CoreData store and need to accurately present to users the time as originally entered. The problem is that if they entered 4:00 on the East Coast, then later look at the recorded time on the West Coast, it appears as 1:00, since the iPhone translates universal times to local times. I need it to show 4:00 -- probably 4:00 (+3h00).
Before I go and restructure my Core Data model, I want to be sure there's no way to derive the NSTimeZone that was active at creation-time from a stored NSDate object. Is there? If not, any recommendation how best to capture NSTimeZone at creation time myself? An NSNumber representing the creation-time NSTimeZone time difference from GMT?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所发现的,NSDate 表示通用时间点,不包含特定时区信息。
我建议不要存储偏移量,而是存储 NSTimeZone 的
name
属性。然后您可以使用+[NSTimeZone timeZoneWithName:]
获取时区并将其转换为当地时间。这为您向用户显示信息的方式提供了最大的灵活性。As you've discovered, NSDate represents a universal point in time and does not include specific time zone information.
I'd suggest instead of storing the offset, store the NSTimeZone's
name
property. Then you can use+[NSTimeZone timeZoneWithName:]
to get the time zone and thence convert it to local time. This gives you maximum flexibility in how you display the information to the user.