NSDate は、タイムゾーンやカレンダーなどに関係なく、時間内の単一のインスタンスを表します。
日付を他の 2 つの日付と比較して、それらの間にあるかどうかを確認するだけです。
あなたの問題は実際に比較する実際の日付を取得しているように思えますが、これはそれほど難しくありません。これは私の頭のサンプルのほんの一部です。
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
}