iPhone - 获取指定地点/时区的当前日期和时间的正确方法,并将其与同一地点的其他日期/时间进行比较 [英] iPhone - Correct way for getting current date and time for a given place / timezone and compare it with another date/time in the same place

查看:126
本文介绍了iPhone - 获取指定地点/时区的当前日期和时间的正确方法,并将其与同一地点的其他日期/时间进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索正确的方式来获取给定地点/时区的实际日期和时间,并且可以将其与同一地点的给定日期/时间进行比较。

I'm searching the correct way to get the actual date and time for a given place / timezone, and being able to compare it to a given date/time for that same place.

让我们举个例子,Paris,即GMT +1。因为我们现在,巴黎也可以是GMT + 2因为夏令时,但它是一部分例外,据我所知,所以它必须考虑到帐户中的答案,但不作为一个一般的参数。这里的重要词是给定的地方。

Let's say, for the example, Paris, that is GMT +1. As we now, Paris can also be GMT+2 because of daylight saving, but it's part of an exception as far as I know so it must be taken in account into the answer, but not taken as a general param. The important words here are "given place".

因此,如果我想知道在悉尼澳大利亚或巴黎法国的日期和时间,并得到那个日期NSDate中,以便能够与另一个NSDate进行比较,该NSDate将在同一个地方表示另一个日期/时间,我该怎么办?

So, if I want to know what date and time it is at Sidney Australia, or Paris France, and get that date into an NSDate for being able to compare it with another NSDate that would represent another date/time in the same place, how may I do ?

页面的问题和答案与评论,甚至在接受的答案,从有经验的用户说:不是一个好的回答-1,错误,不正确的做法,绝对错误... ...

I've read pages and pages of questions and answers with comments, even on accepted answer, that says from experienced users : not a good answer -1, wrong, not the correct way of doing this, absolutely wrong, ...

那么,你知道正确的方法吗?

So, do you know the correct real way to do that ?

在一个完美的世界里,即使日期/用户的电话不处于良好的时间和/或日期和/或时区,或任何可以接近完美的东西,而不需要连接到日期/时间服务器。

In a perfect world, that date/time would be absolute even if the user's phone is not at the good time and/or date and/or timezone, or anything that can be near that perfection without needing for that to connect to a date/time server.

推荐答案

经过一个大头痛,并开始理解NSDate是什么,我想象了那种解决方案。你对这种做法有什么看法?

After a big headache and starting to understand what NSDate is, I imagined that kind of solution. What do you think about that way of doing ?

// Now, an absolute date and time that represent now all around the world, that is made to play with
NSDate *now = [NSDate date];

// A specific calendar for a specific place in the world
NSCalendar* parisCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[parisCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Paris"]];

// Now components seen from Paris
NSDateComponents* componentsNowInParis = [parisCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now];

// Tricking a copy of "Now components seen from Paris" to force 15:00:00, in Paris
NSDateComponents* componentsInParisAt15 = [[componentsNowInParis copy] autorelease];
[componentsInParisAt15 setHour:15];
[componentsInParisAt15 setMinute:0];
[componentsInParisAt15 setSecond:0];

// Getting an universal date reference that represent what could be 15:00:00 seen from paris, Or 19:00:00 from GMT+4
NSDate* dateAt15 = [parisCalendar dateFromComponents:componentsInParisAt15];

// We now have two universal dates that can be compared each other
// If "now" is 16:00:00, those date will show a 60 minutes difference all around the world
NSLog(@"%@", now);
NSLog(@"%@", dateAt15);

有些参考资料:http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DatesAndTimes/ Articles / dtTimeZones.html#// apple_ref / doc / uid / 20000185-SW4

但是,就我所知和测试而言,那一天/时间不能真的是绝对的。它是基于iPhone日期/时间/时区,可能是错误的。

But, as far as I know and tested, that day/time cannot be really absolute. It is based on the iPhone date/time/timezone, that can be wrong.

这篇关于iPhone - 获取指定地点/时区的当前日期和时间的正确方法,并将其与同一地点的其他日期/时间进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆