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

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

问题描述

我正在寻找正确的方法来获取给定地点/时区的实际日期和时间,并能够将其与同一地点的给定日期/时间进行比较.p>

假设,例如,巴黎,即 GMT +1.就像我们现在一样,由于夏令时,巴黎也可以是 GMT+2,但据我所知,它是例外的一部分,因此必须将其考虑到答案中,而不是作为一般参数.这里重要的词是给定的地方".

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

我已经阅读了一页又一页的带有评论的问题和答案,即使是在接受的答案上,经验丰富的用户也会说:不是一个好的答案 -1,错误,不是正确的做法,绝对错误,...

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

在一个完美的世界中,即使用户的手机不在正确的时间和/或日期和/或时区,或者任何可以接近完美而无需连接的任何事物,该日期/时间都是绝对的日期/时间服务器.

解决方案

在头疼不已,开始了解 NSDate 是什么之后,我想到了这样的解决方案.您如何看待这种做法?

//现在,一个代表世界各地现在的绝对日期和时间,可以用来玩NSDate *now = [NSDate 日期];//世界上特定地点的特定日历NSCalendar* parisCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];[parisCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Paris"]];//现在从巴黎看到的组件NSDateComponents* componentsNowInParis = [parisCalendar 组件:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now];//欺骗现在从巴黎看到的组件"的副本以强制 15:00:00,在巴黎NSDateComponents* componentsInParisAt15 = [[componentsNowInParis copy] autorelease];[componentsInParisAt15 setHour:15];[componentsInParisAt15 setMinute:0];[componentsInParisAt15 setSecond:0];//获取一个通用日期参考,代表从巴黎看到的 15:00:00,或从 GMT+4 看到的 19:00:00NSDate* dateAt15 = [parisCalendar dateFromComponents:componentsInParisAt15];//我们现在有两个可以相互比较的通用日期//如果现在"是 16:00:00,那么这些日期将显示世界各地 60 分钟的差异NSLog(@"%@", 现在);NSLog(@"%@", dateAt15);

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

但是,据我所知和测试,那一天/时间不可能是绝对的.它基于 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.

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".

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 ?

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.

解决方案

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

Some reference : http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtTimeZones.html#//apple_ref/doc/uid/20000185-SW4

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天全站免登陆