より明確な解決策は、時間/分のコンポーネントのみが存在する NSDate オブジェクトを使用することだと思います。
基本的に、アプリのどこかに、ショップの開店時間と閉店時間を次のように保存する必要があります。
NSCalendar *calendar = [[NSCalendar alloc]
initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *openTime = [[NSDateComponents alloc] init];
[openTime setHour: 12];
[openTime setMinute: 30];
NSDate *openDate = [calendar dateFromComponents: openTime];
[calendar release];
また、現在の時刻がそのような 2 つの NSDate オブジェクトの間にあるかどうかを確認する必要がある場合は、次のようなメソッドを使用できます。
- (BOOL)currentTimeIsInBetween: (NSDate *)date1 andDate: (NSDate *)date2 {
NSCalendar *calendar = [[NSCalendar alloc]
initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *currentComponents = [calendar components:
(NSMinuteCalendarUnit | NSHourCalendarUnit)
fromDate: [NSDate date]];
NSDate *currentAdjusted = [calendar dateFromComponents: currentComponents];
[calendar release];
if ([currentAdjusted compare: date1] == NSOrderedAscending)
return NO;
if ([currentAdjusted compare: date2] == NSOrderedDescending)
return NO;
return YES;
}
編集: ユーザー rbrown は私よりも少し速かったようです。同じアプローチを提案しています。