Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 本地时区的当前NSDate是否在另一时区的开始和结束NSDate的范围内?_Objective C_Nsdate_Nsdateformatter_Nscalendar_Nsdatecomponents - Fatal编程技术网

Objective c 本地时区的当前NSDate是否在另一时区的开始和结束NSDate的范围内?

Objective c 本地时区的当前NSDate是否在另一时区的开始和结束NSDate的范围内?,objective-c,nsdate,nsdateformatter,nscalendar,nsdatecomponents,Objective C,Nsdate,Nsdateformatter,Nscalendar,Nsdatecomponents,我需要确定用户当前的NSDate本地时区是否在属于其他时区的两个NSDate的范围内 例如: 我在加利福尼亚,现在是太平洋标准时间20点。 纽约的一家咖啡馆在美国东部时间08:00到00:00之间营业。咖啡店存储为这些精确值,并与表示美国/纽约的tz字段一起以XML格式发送 我需要能够确定咖啡店目前是否在世界上营业。在这个例子中,因为纽约东部时间只有23:00,所以它仍然开放 我曾尝试使用NSDate、NSTimeZone、NSCalendar、NSDateComponents来构建一个要转换的

我需要确定用户当前的NSDate本地时区是否在属于其他时区的两个NSDate的范围内

例如:

我在加利福尼亚,现在是太平洋标准时间20点。 纽约的一家咖啡馆在美国东部时间08:00到00:00之间营业。咖啡店存储为这些精确值,并与表示美国/纽约的tz字段一起以XML格式发送

我需要能够确定咖啡店目前是否在世界上营业。在这个例子中,因为纽约东部时间只有23:00,所以它仍然开放

我曾尝试使用NSDate、NSTimeZone、NSCalendar、NSDateComponents来构建一个要转换的算法,但我没有成功,我只是在我认为我理解它的时候,又一次感到困惑。NSDate没有时区的概念,所以我无法确定其中存在什么真正的值,因为您需要将时区传递给NSDateFormatter才能查看


您如何创建一种方法来确定[NSTimeZone localTimeZone]中的[NSDate date]是否在另一个时区的两个NSDate之间?

NSDate表示时间上的单个实例,而不考虑时区、日历等

你所需要做的就是将一个日期与另外两个日期进行比较,看看它是否介于两者之间

听起来你的问题实际上是比较实际日期,这并不难。这只是我头脑中的一个样本

NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
calendar.timeZone = [NSTimeZone timeZoneWithName:@"America/New_York"];

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
components.year = 2011;
...
components.hour = 8;
NSDate *opening = [calendar dateFromComponents:components];
components.day = components.day + 1;
components.hour = 0;
NSDate *closing = [calendar dateFromComponents:components];
NSDate *now = [NSDate date];

if ([opening compare:now] == NSOrderedAscending && [now compare:closing] == NSOrderedAscending) {
  // do stuff
}
或者,如果将当前时间转换为目标时区的日期组件并检查小时组件,则可能会更容易。这样,您就不需要确保日期组件中的值不超出范围。此外,在一般情况下,计算开盘和收盘日期组件并不简单

NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
calendar.timeZone = [NSTimeZone timeZoneWithName:@"America/New_York"];
NSDate *now = [NSDate date];
NSDateComponents *components = [calendar components:NSHourCalendarUnit fromDate:now];

if (components.hour > 0 && components.hour < 8) {
  // do stuff
}

更具体地说,我们需要首先使用今天的日期将时间和时区从XML引入一个新的日期对象。然后与移动用户的当前日期/时间和时区进行比较。