对不同时区的 NSDate 进行比较?

发布于 2024-08-18 05:10:43 字数 623 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

栖迟 2024-08-25 05:10:43

您应该采用一种标准时区(例如 UTC/GMT 格式)进行所有计算。

You should take one standard timezone like UTC/GMT format for all calculation.

寂寞清仓 2024-08-25 05:10:43

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 the timeZoneWithAbbreviation: to get a NSTimeZone object from a NSDate instance. Once you get the time zone, you have everything you need.

情仇皆在手 2024-08-25 05:10:43

我将当前日期和我想知道是否接近的日期转换为 GMT,然后返回差值。所以我改变了一切来处理差异而不是实际的时间和日期。有点像乐谱移调到不同的调:)

+ (NSInteger) minutesUntilDate:(NSDate*) date withTimezoneOffset:(NSInteger) GMTOffset     
{

 NSDate *now = [NSDate date];
 NSTimeInterval localTimeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];

 now = [now addTimeInterval:(localTimeZoneOffset * -1)];

 date = [date addTimeInterval:(GMTOffset * 60 * 60) * −1];

 return ((NSInteger)[now timeIntervalSinceDate:date] / 60 ) * -1;
}

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

+ (NSInteger) minutesUntilDate:(NSDate*) date withTimezoneOffset:(NSInteger) GMTOffset     
{

 NSDate *now = [NSDate date];
 NSTimeInterval localTimeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];

 now = [now addTimeInterval:(localTimeZoneOffset * -1)];

 date = [date addTimeInterval:(GMTOffset * 60 * 60) * −1];

 return ((NSInteger)[now timeIntervalSinceDate:date] / 60 ) * -1;
}
归属感 2024-08-25 05:10:43

一旦您分配了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文