Skip to content

Commit

Permalink
Add nullability specifiers to date parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
MosheBerman committed Apr 29, 2018
1 parent e1ce180 commit b4a7a50
Show file tree
Hide file tree
Showing 18 changed files with 262 additions and 189 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,60 +13,90 @@ @implementation NSCalendar (Components)

- (NSInteger)weekOfMonthInDate:(NSDate*)date
{
if (!date) {
return -1;
}
NSDateComponents *comps = [self components:NSCalendarUnitWeekOfMonth fromDate:date];
return [comps weekOfMonth];
}

- (NSInteger)weekOfYearInDate:(NSDate*)date
{
if (!date) {
return -1;
}
NSDateComponents *comps = [self components:NSCalendarUnitWeekOfYear fromDate:date];
return [comps weekOfYear];
}

- (NSInteger)weekdayInDate:(NSDate*)date
{
if (!date) {
return -1;
}
NSDateComponents *comps = [self components:NSCalendarUnitWeekday fromDate:date];
return [comps weekday];
}

- (NSInteger)secondsInDate:(NSDate*)date
{
if (!date) {
return -1;
}
NSDateComponents *comps = [self components:NSCalendarUnitSecond fromDate:date];
return [comps second];
}

- (NSInteger)minutesInDate:(NSDate*)date
{
if (!date) {
return -1;
}
NSDateComponents *comps = [self components:NSCalendarUnitMinute fromDate:date];
return [comps minute];
}

- (NSInteger)hoursInDate:(NSDate*)date
{
if (!date) {
return -1;
}
NSDateComponents *comps = [self components:NSCalendarUnitHour fromDate:date];
return [comps hour];
}

- (NSInteger)daysInDate:(NSDate*)date
{
if (!date) {
return -1;
}
NSDateComponents *comps = [self components:NSCalendarUnitDay fromDate:date];
return [comps day];
}

- (NSInteger)monthsInDate:(NSDate*)date
{
if (!date) {
return -1;
}
NSDateComponents *comps = [self components:NSCalendarUnitMonth fromDate:date];
return [comps month];
}

- (NSInteger)yearsInDate:(NSDate*)date
{
if (!date) {
return -1;
}
NSDateComponents *comps = [self components:NSCalendarUnitYear fromDate:date];
return [comps year];
}

- (NSInteger)eraInDate:(NSDate*)date
{
if (!date) {
return -1;
}
NSDateComponents *comps = [self components:NSCalendarUnitEra fromDate:date];
return [comps era];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ NS_ASSUME_NONNULL_BEGIN

@interface NSCalendar (DateComparison)

- (BOOL)date:(NSDate *)firstDate isSameDayAs:(NSDate *)anotherDate;
- (BOOL)date:(NSDate*)firstDate isSameWeekAs:(NSDate *)anotherDate;
- (BOOL)date:(NSDate*)firstDate isSameMonthAs:(NSDate *)anotherDate;
- (BOOL)date:(NSDate *)firstDate isSameYearAs:(NSDate *)anotherDate;
- (BOOL)date:(NSDate *)firstDate isSameEraAs:(NSDate *)anotherDate;
- (BOOL)date:(NSDate * _Nonnull)firstDate isSameDayAs:(NSDate * _Nonnull)anotherDate;
- (BOOL)date:(NSDate*)firstDate isSameWeekAs:(NSDate * _Nonnull)anotherDate;
- (BOOL)date:(NSDate*)firstDate isSameMonthAs:(NSDate * _Nonnull)anotherDate;
- (BOOL)date:(NSDate * _Nonnull)firstDate isSameYearAs:(NSDate * _Nonnull)anotherDate;
- (BOOL)date:(NSDate * _Nonnull)firstDate isSameEraAs:(NSDate * _Nonnull)anotherDate;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@implementation NSCalendar (DateComparison)

- (BOOL)date:(NSDate *)firstDate isSameDayAs:(NSDate *)anotherDate
- (BOOL)date:(NSDate * _Nonnull)firstDate isSameDayAs:(NSDate * _Nonnull)anotherDate
{
NSInteger firstDay = [self daysInDate:firstDate];
NSInteger secondDay = [self daysInDate:anotherDate];
Expand All @@ -24,7 +24,7 @@ - (BOOL)date:(NSDate *)firstDate isSameDayAs:(NSDate *)anotherDate
return sameMonth && sameDay;
}

- (BOOL)date:(NSDate*)firstDate isSameWeekAs:(NSDate *)anotherDate
- (BOOL)date:(NSDate*)firstDate isSameWeekAs:(NSDate * _Nonnull)anotherDate
{
NSInteger firstMonth = [self weekOfYearInDate:firstDate];
NSInteger secondMonth = [self weekOfYearInDate:anotherDate];
Expand All @@ -36,7 +36,7 @@ - (BOOL)date:(NSDate*)firstDate isSameWeekAs:(NSDate *)anotherDate
}


- (BOOL)date:(NSDate*)firstDate isSameMonthAs:(NSDate *)anotherDate
- (BOOL)date:(NSDate*)firstDate isSameMonthAs:(NSDate * _Nonnull)anotherDate
{
NSInteger firstMonth = [self monthsInDate:firstDate];
NSInteger secondMonth = [self monthsInDate:anotherDate];
Expand All @@ -47,7 +47,7 @@ - (BOOL)date:(NSDate*)firstDate isSameMonthAs:(NSDate *)anotherDate
return sameYear && sameMonth;
}

- (BOOL)date:(NSDate *)firstDate isSameYearAs:(NSDate *)anotherDate
- (BOOL)date:(NSDate * _Nonnull)firstDate isSameYearAs:(NSDate * _Nonnull)anotherDate
{
NSInteger firstYear = [self yearsInDate:firstDate];
NSInteger secondYear = [self yearsInDate:anotherDate];
Expand All @@ -58,7 +58,7 @@ - (BOOL)date:(NSDate *)firstDate isSameYearAs:(NSDate *)anotherDate
return sameEra && sameYear;
}

- (BOOL)date:(NSDate *)firstDate isSameEraAs:(NSDate *)anotherDate
- (BOOL)date:(NSDate * _Nonnull)firstDate isSameEraAs:(NSDate * _Nonnull)anotherDate
{
NSInteger firstEra = [self eraInDate:firstDate];
NSInteger secondEra = [self eraInDate:anotherDate];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ NS_ASSUME_NONNULL_BEGIN

#pragma mark - Add Units

- (nullable NSDate *)dateByAddingSeconds:(NSUInteger)seconds toDate:(NSDate *)date;
- (nullable NSDate *)dateByAddingMinutes:(NSUInteger)minutes toDate:(NSDate *)date;
- (nullable NSDate *)dateByAddingHours:(NSUInteger)hours toDate:(NSDate *)date;
- (nullable NSDate *)dateByAddingDays:(NSUInteger)days toDate:(NSDate *)date;
- (nullable NSDate *)dateByAddingWeeks:(NSUInteger)weeks toDate:(NSDate *)date;
- (nullable NSDate *)dateByAddingMonths:(NSUInteger)months toDate:(NSDate *)date;
- (nullable NSDate *)dateByAddingYears:(NSUInteger)years toDate:(NSDate *)date;
- (nullable NSDate *)dateByAddingSeconds:(NSUInteger)seconds toDate:(NSDate * _Nonnull)date;
- (nullable NSDate *)dateByAddingMinutes:(NSUInteger)minutes toDate:(NSDate * _Nonnull)date;
- (nullable NSDate *)dateByAddingHours:(NSUInteger)hours toDate:(NSDate * _Nonnull)date;
- (nullable NSDate *)dateByAddingDays:(NSUInteger)days toDate:(NSDate * _Nonnull)date;
- (nullable NSDate *)dateByAddingWeeks:(NSUInteger)weeks toDate:(NSDate * _Nonnull)date;
- (nullable NSDate *)dateByAddingMonths:(NSUInteger)months toDate:(NSDate * _Nonnull)date;
- (nullable NSDate *)dateByAddingYears:(NSUInteger)years toDate:(NSDate * _Nonnull)date;

#pragma mark - Subtract Units

- (nullable NSDate *)dateBySubtractingSeconds:(NSUInteger)seconds fromDate:(NSDate *)date;
- (nullable NSDate *)dateBySubtractingMinutes:(NSUInteger)minutes fromDate:(NSDate *)date;
- (nullable NSDate *)dateBySubtractingHours:(NSUInteger)hours fromDate:(NSDate *)date;
- (nullable NSDate *)dateBySubtractingDays:(NSUInteger)days fromDate:(NSDate *)date;
- (nullable NSDate *)dateBySubtractingWeeks:(NSUInteger)weeks fromDate:(NSDate *)date;
- (nullable NSDate *)dateBySubtractingMonths:(NSUInteger)months fromDate:(NSDate *)date;
- (nullable NSDate *)dateBySubtractingYears:(NSUInteger)years fromDate:(NSDate *)date;
- (nullable NSDate *)dateBySubtractingSeconds:(NSUInteger)seconds fromDate:(NSDate * _Nonnull)date;
- (nullable NSDate *)dateBySubtractingMinutes:(NSUInteger)minutes fromDate:(NSDate * _Nonnull)date;
- (nullable NSDate *)dateBySubtractingHours:(NSUInteger)hours fromDate:(NSDate * _Nonnull)date;
- (nullable NSDate *)dateBySubtractingDays:(NSUInteger)days fromDate:(NSDate * _Nonnull)date;
- (nullable NSDate *)dateBySubtractingWeeks:(NSUInteger)weeks fromDate:(NSDate * _Nonnull)date;
- (nullable NSDate *)dateBySubtractingMonths:(NSUInteger)months fromDate:(NSDate * _Nonnull)date;
- (nullable NSDate *)dateBySubtractingYears:(NSUInteger)years fromDate:(NSDate * _Nonnull)date;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,102 +14,144 @@ @implementation NSCalendar (DateManipulation)

#pragma mark - Add Units

- (NSDate *)dateByAddingSeconds:(NSUInteger)seconds toDate:(NSDate *)date
- (NSDate * _Nullable)dateByAddingSeconds:(NSUInteger)seconds toDate:(NSDate * _Nonnull)date
{
if (!date) {
return nil;
}
NSDateComponents *c = [NSDateComponents new];
[c setSecond:seconds];
return [self dateByAddingComponents:c toDate:date options:0];
}

- (NSDate *)dateByAddingMinutes:(NSUInteger)minutes toDate:(NSDate *)date
- (NSDate * _Nullable)dateByAddingMinutes:(NSUInteger)minutes toDate:(NSDate * _Nonnull)date
{
if (!date) {
return nil;
}
NSDateComponents *c = [NSDateComponents new];
[c setMinute:minutes];
return [self dateByAddingComponents:c toDate:date options:0];
}

- (NSDate *)dateByAddingHours:(NSUInteger)hours toDate:(NSDate *)date
- (NSDate * _Nullable)dateByAddingHours:(NSUInteger)hours toDate:(NSDate * _Nonnull)date
{
if (!date) {
return nil;
}
NSDateComponents *c = [NSDateComponents new];
[c setHour:hours];
return [self dateByAddingComponents:c toDate:date options:0];
}

- (NSDate *)dateByAddingDays:(NSUInteger)days toDate:(NSDate *)date
- (NSDate * _Nullable)dateByAddingDays:(NSUInteger)days toDate:(NSDate * _Nonnull)date
{
if (!date) {
return nil;
}
NSDateComponents *c = [NSDateComponents new];
[c setDay:days];
return [self dateByAddingComponents:c toDate:date options:0];
}

- (NSDate *)dateByAddingWeeks:(NSUInteger)weeks toDate:(NSDate *)date
- (NSDate * _Nullable)dateByAddingWeeks:(NSUInteger)weeks toDate:(NSDate * _Nonnull)date
{
if (!date) {
return nil;
}
NSDateComponents *c = [NSDateComponents new];
[c setWeekOfYear:weeks];
return [self dateByAddingComponents:c toDate:date options:0];
}

- (NSDate *)dateByAddingMonths:(NSUInteger)months toDate:(NSDate *)date
- (NSDate * _Nullable)dateByAddingMonths:(NSUInteger)months toDate:(NSDate * _Nonnull)date
{
if (!date) {
return nil;
}
NSDateComponents *c = [NSDateComponents new];
[c setMonth:months];
return [self dateByAddingComponents:c toDate:date options:0];
}

- (NSDate *)dateByAddingYears:(NSUInteger)years toDate:(NSDate *)date
- (NSDate * _Nullable)dateByAddingYears:(NSUInteger)years toDate:(NSDate * _Nonnull)date
{
if (!date) {
return nil;
}
NSDateComponents *c = [NSDateComponents new];
[c setYear:years];
return [self dateByAddingComponents:c toDate:date options:0];
}

#pragma mark - Subtract Units

- (NSDate *)dateBySubtractingSeconds:(NSUInteger)seconds fromDate:(NSDate *)date
- (NSDate * _Nullable)dateBySubtractingSeconds:(NSUInteger)seconds fromDate:(NSDate * _Nonnull)date
{

if (!date) {
return nil;
}
NSDateComponents *c = [NSDateComponents new];
[c setSecond:-seconds];
return [self dateByAddingComponents:c toDate:date options:0];
}
- (NSDate *)dateBySubtractingMinutes:(NSUInteger)minutes fromDate:(NSDate *)date;
- (NSDate * _Nullable)dateBySubtractingMinutes:(NSUInteger)minutes fromDate:(NSDate * _Nonnull)date;
{
if (!date) {
return nil;
}
NSDateComponents *c = [NSDateComponents new];
[c setMinute:-minutes];
return [self dateByAddingComponents:c toDate:date options:0];
}

- (NSDate *)dateBySubtractingHours:(NSUInteger)hours fromDate:(NSDate *)date
- (NSDate * _Nullable)dateBySubtractingHours:(NSUInteger)hours fromDate:(NSDate * _Nonnull)date
{
if (!date) {
return nil;
}
NSDateComponents *c = [NSDateComponents new];
[c setHour:-hours];
return [self dateByAddingComponents:c toDate:date options:0];
}

- (NSDate *)dateBySubtractingDays:(NSUInteger)days fromDate:(NSDate *)date
- (NSDate * _Nullable)dateBySubtractingDays:(NSUInteger)days fromDate:(NSDate * _Nonnull)date
{
if (!date) {
return nil;
}
NSDateComponents *c = [NSDateComponents new];
[c setDay:-days];
return [self dateByAddingComponents:c toDate:date options:0];
}

- (NSDate *)dateBySubtractingWeeks:(NSUInteger)weeks fromDate:(NSDate *)date
- (NSDate * _Nullable)dateBySubtractingWeeks:(NSUInteger)weeks fromDate:(NSDate * _Nonnull)date
{
if (!date) {
return nil;
}
NSDateComponents *c = [NSDateComponents new];
[c setWeekOfYear:-weeks];
return [self dateByAddingComponents:c toDate:date options:0];
}

- (NSDate *)dateBySubtractingMonths:(NSUInteger)months fromDate:(NSDate *)date
- (NSDate * _Nullable)dateBySubtractingMonths:(NSUInteger)months fromDate:(NSDate * _Nonnull)date
{
if (!date) {
return nil;
}
NSDateComponents *c = [NSDateComponents new];
[c setMonth:-months];
return [self dateByAddingComponents:c toDate:date options:0];
}


- (NSDate *)dateBySubtractingYears:(NSUInteger)years fromDate:(NSDate *)date
- (NSDate * _Nullable)dateBySubtractingYears:(NSUInteger)years fromDate:(NSDate * _Nonnull)date
{
if (!date) {
return nil;
}
NSDateComponents *c = [NSDateComponents new];
[c setYear:-years];
return [self dateByAddingComponents:c toDate:date options:0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ NS_ASSUME_NONNULL_BEGIN
#pragma mark - First/Last of Week

- (nullable NSDate *)firstDayOfTheWeek;
- (nullable NSDate *)firstDayOfTheWeekUsingReferenceDate:(NSDate *)date;
- (nullable NSDate *)firstDayOfTheWeekUsingReferenceDate:(NSDate * _Nonnull)date;

- (nullable NSDate *)lastDayOfTheWeek;
- (nullable NSDate *)lastDayOfTheWeekUsingReferenceDate:(NSDate *)date;
- (nullable NSDate *)lastDayOfTheWeekUsingReferenceDate:(NSDate * _Nonnull)date;

#pragma mark - First/Last of Month

- (nullable NSDate *)firstDayOfTheMonth;
- (nullable NSDate *)firstDayOfTheMonthUsingReferenceDate:(NSDate *)date;
- (nullable NSDate *)firstDayOfTheMonthUsingReferenceDate:(NSDate * _Nonnull)date;

- (nullable NSDate *)lastDayOfTheMonth;
- (nullable NSDate *)lastDayOfTheMonthUsingReferenceDate:(NSDate *)date;
- (nullable NSDate *)lastDayOfTheMonthUsingReferenceDate:(NSDate * _Nonnull)date;

@end

Expand Down
Loading

0 comments on commit b4a7a50

Please sign in to comment.