ios - Split NSDate that stretches over several days into several spans -
i have 2 nsdates define span, 1 set them this
spana: 12:00 today spanb: 12:00 tomorrow
this equals 24 hours, need create 2 ranges instead of one, no spans stretch across midnight. outcome of example above this:
spana: 12:00 today spanb: 23:59 today spanc: 00:01 tomorrow spand: 12:00 tomorrow
i'm not sure how this. came idea use function this:
- (nsinteger)daysbetweendate:(nsdate *)fromdatetime anddate:(nsdate *)todatetime { nsdate *fromdate; nsdate *todate; nscalendar *calendar = [nscalendar currentcalendar]; [calendar rangeofunit:nscalendarunitday startdate:&fromdate interval:null fordate:fromdatetime]; [calendar rangeofunit:nscalendarunitday startdate:&todate interval:null fordate:todatetime]; nsdatecomponents *difference = [calendar components:nscalendarunitday fromdate:fromdate todate:todate options:0]; return [difference day]; }
which returns how many days span stretches across, or in other words: number of spans required. instance for-loop number of days , add logic create necessary spans.
i'm using structure spans:
typedef struct { nstimeinterval start; nstimeinterval end; } span;
what best approach this?
since spans except possibly first , last 1 identical day-long spans, can use algorithm:
- check if initial time midnight. if not midnight, add opening span start midnight of same day
- for each day after first midnight add day-long range midnight of next day
- check if last time midnight. if not midnight, add closing span result.
Comments
Post a Comment