. : - , , , .
, , , - , . , , , , , , .
, , - , - , ..
? , , , , - , ( ), , 24 60 , , . , , , .
, NSCalendar , NSDate.
, , , , , , :
#define TO_MINUTES(hour, min) (hour * 60 + min)
?
, NSArray, , NSDictionary, / - . - , NSDictionary NSNumber. ( , - !)
C- - , .
C :
typedef struct
{
NSInteger openTime;
NSInteger closeTime;
} ShopHours;
, :
ShopHours WeekSchedule[] =
{
{0, 0},
{ TO_MINUTES(9, 0), TO_MINUTES(24, 0) },
{ TO_MINUTES(7, 30), TO_MINUTES(24, 0) },
...
{ TO_MINUTES(9, 0), TO_MINUTES(22, 0) },
};
, .
( , 0 - NSDateComponents , 1 , ( C-, NSArray) 0, , do - 1 .)
, NSDate NSDateComponents, , :
NSInteger weekday = comps.weekday;
NSInteger minutes = TO_MINUTES(comps.hour, comps.minute);
weekday WeekSchedule minutes , , . :
if (minutes >= WeekSchedule[weekday].openTime && minutes <= WeekSchedule[weekday].closeTime)
{
// shop is open...
}
else
{
// shop is closed...
}
, , :
- (NSString *) shopState:(NSDate *)dateAndTime
{
NSInteger weekday = comps.weekday;
NSInteger minutes = TO_MINUTES(comps.hour, comps.minute);
if (minutes >= WeekSchedule[weekday].openTime && minutes <= WeekSchedule[weekday].closeTime)
{
}
else
{
return @"Closed";
}
}
, , .
-
, , , - , . , , , :
: , .. / , . , .
: (1), ...
. , / . , , , , , , .
: , . DST . , , / - , , . NSCalendar / , /. , , 2 , DST 2 1 , 1:30? , , , ? .
, .
, ( , - , ..) -
, , , , .
:
, . . TO_MINUTES, , 0000 . , , , - , , , , , ( / ? )
(1), , Sat β Sun, 30 .
β . , ( ) ( ). , , , , (, TO_MINUTES(7, 24, 0)), , , 00:00, , 30 ( ).
. DST, .. - , . DST NSTimeZone, , ( 1 ), "" "" , , , / .
; -)
, Objective-C , , , .
, - -, , .
- C- - Objective-C ? - , NSRect et al - Objective-C. , , , , , . , /:
#define TO_MINUTES(day, hour, min) ((day * 24 + hour) * 60 + min)
#define WEEK_START TO_MINUTES(1, 0, 0)
#define WEEK_FINISH TO_MINUTES(7, 24, 0)
typedef struct
{ NSInteger openTime;
NSInteger closeTime;
} ShopHours;
ShopHours WeekSchedule[] =
{ { TO_MINUTES(1, 0, 0), TO_MINUTES(1, 0, 15) },
{ TO_MINUTES(1, 9, 0), TO_MINUTES(1, 24, 0) },
{ TO_MINUTES(2, 7, 30), TO_MINUTES(2, 24, 0) },
{ TO_MINUTES(3, 7, 30), TO_MINUTES(3, 24, 0) },
{ TO_MINUTES(4, 7, 30), TO_MINUTES(5, 2, 0) },
{ TO_MINUTES(5, 7, 30), TO_MINUTES(5, 24, 0) },
{ TO_MINUTES(6, 7, 30), TO_MINUTES(6, 22, 0) },
{ TO_MINUTES(7, 9, 0), TO_MINUTES(7, 22, 0) },
{ TO_MINUTES(7, 23, 30),TO_MINUTES(7, 24, 0) },
};
- (NSString *) shopState:(NSDate *)dateAndTime
{ NSCalendar *calendar = [NSCalendar currentCalendar];
const NSCalendarUnit units = NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
NSDateComponents *comps = [calendar components:units fromDate:dateAndTime];
NSInteger minutes = TO_MINUTES(comps.weekday, comps.hour, comps.minute);
NSLog(@"%ld (%ld, %ld, %ld)", minutes, comps.weekday, comps.hour, comps.minute);
unsigned periods = sizeof(WeekSchedule)/sizeof(ShopHours);
for (unsigned ix = 0; ix < periods; ix++)
{ if (minutes >= WeekSchedule[ix].openTime)
{ if (minutes < WeekSchedule[ix].closeTime)
{
NSInteger closeTime = WeekSchedule[ix].closeTime;
if (closeTime == WEEK_FINISH && WeekSchedule[0].openTime == WEEK_START)
closeTime += WeekSchedule[0].closeTime - WEEK_START;
NSInteger closingIn = closeTime - minutes;
if (closingIn <= 30)
return [NSString stringWithFormat:@"Closes in %ld min", closingIn];
else
return @"Open";
}
}
else
break;
}
return @"Closed";
}
DST
, , , , , , .
, NSDate , Rob .
NSDate , , NSDateComponents. , 2am 1am - DST, 1:30 . NSDate 1 , , 10 , components:fromDate: 2, , : 00:50, 01:00, 01:10,..., 01:50, 01:00, 01:10,..., 01:50, 02:00, 02:10. 30 , 01:30, 30 , !
, NSDate, // , . ( ) , , , .