|
| 1 | +// Copyright (C) 2014 by Matthew York |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any |
| 4 | +// person obtaining a copy of this software and |
| 5 | +// associated documentation files (the "Software"), to |
| 6 | +// deal in the Software without restriction, including |
| 7 | +// without limitation the rights to use, copy, modify, merge, |
| 8 | +// publish, distribute, sublicense, and/or sell copies of the |
| 9 | +// Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall |
| 13 | +// be included in all copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 17 | +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 19 | +// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 20 | +// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | + |
| 23 | +#import <Foundation/Foundation.h> |
| 24 | + |
| 25 | + |
| 26 | +typedef NS_ENUM(NSUInteger, DTTimePeriodRelation){ |
| 27 | + DTTimePeriodRelationAfter, |
| 28 | + DTTimePeriodRelationStartTouching, |
| 29 | + DTTimePeriodRelationStartInside, |
| 30 | + DTTimePeriodRelationInsideStartTouching, |
| 31 | + DTTimePeriodRelationEnclosingStartTouching, |
| 32 | + DTTimePeriodRelationEnclosing, |
| 33 | + DTTimePeriodRelationEnclosingEndTouching, |
| 34 | + DTTimePeriodRelationExactMatch, |
| 35 | + DTTimePeriodRelationInside, |
| 36 | + DTTimePeriodRelationInsideEndTouching, |
| 37 | + DTTimePeriodRelationEndInside, |
| 38 | + DTTimePeriodRelationEndTouching, |
| 39 | + DTTimePeriodRelationBefore, |
| 40 | + DTTimePeriodRelationNone //One or more of the dates does not exist |
| 41 | +}; |
| 42 | + |
| 43 | +typedef NS_ENUM(NSUInteger, DTTimePeriodSize) { |
| 44 | + DTTimePeriodSizeSecond, |
| 45 | + DTTimePeriodSizeMinute, |
| 46 | + DTTimePeriodSizeHour, |
| 47 | + DTTimePeriodSizeDay, |
| 48 | + DTTimePeriodSizeWeek, |
| 49 | + DTTimePeriodSizeMonth, |
| 50 | + DTTimePeriodSizeYear |
| 51 | +}; |
| 52 | + |
| 53 | +typedef NS_ENUM(NSUInteger, DTTimePeriodInterval) { |
| 54 | + DTTimePeriodIntervalOpen, |
| 55 | + DTTimePeriodIntervalClosed |
| 56 | +}; |
| 57 | + |
| 58 | +typedef NS_ENUM(NSUInteger, DTTimePeriodAnchor) { |
| 59 | + DTTimePeriodAnchorStart, |
| 60 | + DTTimePeriodAnchorCenter, |
| 61 | + DTTimePeriodAnchorEnd |
| 62 | +}; |
| 63 | + |
| 64 | +@interface DTTimePeriod : NSObject |
| 65 | + |
| 66 | +/** |
| 67 | + * The start date for a DTTimePeriod representing the starting boundary of the time period |
| 68 | + */ |
| 69 | +@property (nonatomic,strong) NSDate *StartDate; |
| 70 | + |
| 71 | +/** |
| 72 | + * The end date for a DTTimePeriod representing the ending boundary of the time period |
| 73 | + */ |
| 74 | +@property (nonatomic,strong) NSDate *EndDate; |
| 75 | + |
| 76 | +#pragma mark - Custom Init / Factory Methods |
| 77 | +-(instancetype)initWithStartDate:(NSDate *)startDate endDate:(NSDate *)endDate; |
| 78 | ++(instancetype)timePeriodWithStartDate:(NSDate *)startDate endDate:(NSDate *)endDate; |
| 79 | ++(instancetype)timePeriodWithSize:(DTTimePeriodSize)size startingAt:(NSDate *)date; |
| 80 | ++(instancetype)timePeriodWithSize:(DTTimePeriodSize)size amount:(NSInteger)amount startingAt:(NSDate *)date; |
| 81 | ++(instancetype)timePeriodWithSize:(DTTimePeriodSize)size endingAt:(NSDate *)date; |
| 82 | ++(instancetype)timePeriodWithSize:(DTTimePeriodSize)size amount:(NSInteger)amount endingAt:(NSDate *)date; |
| 83 | ++(instancetype)timePeriodWithAllTime; |
| 84 | + |
| 85 | +#pragma mark - Time Period Information |
| 86 | +-(BOOL)hasStartDate; |
| 87 | +-(BOOL)hasEndDate; |
| 88 | +-(BOOL)isMoment; |
| 89 | +-(double)durationInYears; |
| 90 | +-(double)durationInWeeks; |
| 91 | +-(double)durationInDays; |
| 92 | +-(double)durationInHours; |
| 93 | +-(double)durationInMinutes; |
| 94 | +-(double)durationInSeconds; |
| 95 | + |
| 96 | +#pragma mark - Time Period Relationship |
| 97 | +-(BOOL)isEqualToPeriod:(DTTimePeriod *)period; |
| 98 | +-(BOOL)isInside:(DTTimePeriod *)period; |
| 99 | +-(BOOL)contains:(DTTimePeriod *)period; |
| 100 | +-(BOOL)overlapsWith:(DTTimePeriod *)period; |
| 101 | +-(BOOL)intersects:(DTTimePeriod *)period; |
| 102 | +-(DTTimePeriodRelation)relationToPeriod:(DTTimePeriod *)period; |
| 103 | +-(NSTimeInterval)gapBetween:(DTTimePeriod *)period; |
| 104 | + |
| 105 | +#pragma mark - Date Relationships |
| 106 | +-(BOOL)containsDate:(NSDate *)date interval:(DTTimePeriodInterval)interval; |
| 107 | + |
| 108 | +#pragma mark - Period Manipulation |
| 109 | +#pragma mark Shifts |
| 110 | +-(void)shiftEarlierWithSize:(DTTimePeriodSize)size; |
| 111 | +-(void)shiftEarlierWithSize:(DTTimePeriodSize)size amount:(NSInteger)amount; |
| 112 | +-(void)shiftLaterWithSize:(DTTimePeriodSize)size; |
| 113 | +-(void)shiftLaterWithSize:(DTTimePeriodSize)size amount:(NSInteger)amount; |
| 114 | + |
| 115 | +#pragma mark Lengthen / Shorten |
| 116 | +-(void)lengthenWithAnchorDate:(DTTimePeriodAnchor)anchor size:(DTTimePeriodSize)size; |
| 117 | +-(void)lengthenWithAnchorDate:(DTTimePeriodAnchor)anchor size:(DTTimePeriodSize)size amount:(NSInteger)amount; |
| 118 | +-(void)shortenWithAnchorDate:(DTTimePeriodAnchor)anchor size:(DTTimePeriodSize)size; |
| 119 | +-(void)shortenWithAnchorDate:(DTTimePeriodAnchor)anchor size:(DTTimePeriodSize)size amount:(NSInteger)amount; |
| 120 | + |
| 121 | +#pragma mark - Helper Methods |
| 122 | +-(DTTimePeriod *)copy; |
| 123 | +@end |
0 commit comments