对不同时区的 NSDate 进行比较?
我有一个 NSDates
数组,我使用 [NSDate dateFromString]
从字符串构建它 在我解析以获取字符串的 xml 中,还有一个时区字符串。据我在手册中看到的 NSDate
本身并不处理时区。我是否需要始终将此时区值存储在某处,并在每次需要时将其与所属的 NSDate
配对?
我还需要弄清楚,如果活动于 10:00 在伦敦开始,但我在丹麦,将 iPhone 设置为丹麦时间,我的“活动在伦敦开始”应该显示在 09:00 点。
同样,如果一个活动于 10:00 在伦敦开始,并于 12:00 在丹麦结束,如果我使用具有丹麦设置的 iPhone 来比较开始时间和结束时间,我会得到该活动的持续时间活动时间为 02:00,但英国的 10:00 点和丹麦的 12:00 点仅相差 1 小时。
NSdate
在一个时区范围内非常适合这些事情,但引入时区部分只会让一切对我来说变得复杂。有没有办法抽象/隐藏所有这些计算,因为我认为可能会犯很多错误。
我已经阅读了 Apple 的 NSDateformatter 和 NSDate 指南,但它们确实很模糊,并且包含大量已弃用的代码:/
感谢您提供的任何帮助。
I have an array of NSDates
which I build from strings using [NSDate dateFromString]
In the xml I parsed to get the string there was also a timezone string. As far as I can see in the manual NSDate
does not in it self deal with timezones. Do I need to always store this timezone value somewhere and pair it with the belonging NSDate
each time I need it?
I also need to figure out that if an event starts in London at 10:00, but I am in Denmark having my iPhone set to danish time my "event started in London" should display at 09:00 o'clock.
Again if an event starts in London at 10:00 o'clock and ends in Denmark at 12:00 o'clock, If I were to compare start time and end time using an iPhone with danish settings I would get that the duration of the event was 02:00 event though 10:00 o'clock in UK and 12:00 o'clock in Denmark is only 1 hour apart.
NSdate
works really well for these things in the scope of one timezone, but introducing the timezone part just made everything complicated to me. Is there a way to abstract/hide all these calculations, as I see potential for making a lot of mistakes.
I have been through the NSDateformatter and NSDate guides from Apple, but they are really vague and sports a substantial amount of deprecated code :/
Thanks for any help given.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该采用一种标准时区(例如 UTC/GMT 格式)进行所有计算。
You should take one standard timezone like UTC/GMT format for all calculation.
根据 NSDate 参考< /a>,
dateWithString:
将 GMT 的偏移量作为最后一个组成部分;虽然不是时区,但足以进行计算或比较)。查看 NSTimeZone 参考< /a>,您可以使用
abbreviationForDate:
和timeZoneWithAbbreviation:
从NSDate
获取NSTimeZone
对象实例。一旦您获得时区,您就拥有了所需的一切。According to the NSDate reference,
dateWithString:
takes an offset to GMT as last component; while it is not a time zone, it is sufficient to perform computation or comparison).Looking at the NSTimeZone reference, you can use the
abbreviationForDate:
and thetimeZoneWithAbbreviation:
to get aNSTimeZone
object from aNSDate
instance. Once you get the time zone, you have everything you need.我将当前日期和我想知道是否接近的日期转换为 GMT,然后返回差值。所以我改变了一切来处理差异而不是实际的时间和日期。有点像乐谱移调到不同的调:)
I convert the present date and the date I would like to know if is close, to GMT and then returning the difference. So I changed every thing to deal with differences instead of actual times and dates. A bit like a music score transposed to a different key:)
一旦您分配了 NSDate,它们就不再具有时区信息。 NSDate 是“无时区”的并且始终采用 GMT。您应该确保 NSDate 在分配时正确理解您的格式。
一旦你有了 NSDate,你就可以进行正常的计算并忽略时区。
您只需要在将字符串读入 NSDates 以及打印它们时注意时区。
As soon as you have allocated an NSDate, these do not have timezone information any longer. NSDate is "timezone-less" and is always in GMT. You should make sure that NSDate understand your format correctly when allocating it.
Once you have an NSDate you can make normal calculations and ignore the timezones.
You only need to take care of timezones when reading strings into NSDates and when printing them out.