Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update for latest iOS sdk #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions NSDate+Utilities.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#import "NSDate+Utilities.h"

// Thanks, AshFurrow
static const unsigned componentFlags = (NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit);
static const unsigned componentFlags = (NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekOfYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal);

@implementation NSDate (Utilities)

Expand Down Expand Up @@ -177,10 +177,10 @@ - (BOOL) isSameWeekAsDate: (NSDate *) aDate
NSDateComponents *components2 = [[NSDate currentCalendar] components:componentFlags fromDate:aDate];

// Must be same week. 12/31 and 1/1 will both be week "1" if they are in the same week
if (components1.week != components2.week) return NO;
if (components1.weekOfYear != components2.weekOfYear) return NO;

// Must have a time interval under 1 week. Thanks @aclark
return (abs([self timeIntervalSinceDate:aDate]) < D_WEEK);
return (fabs([self timeIntervalSinceDate:aDate]) < D_WEEK);
}

- (BOOL) isThisWeek
Expand All @@ -205,8 +205,8 @@ - (BOOL) isLastWeek
// Thanks, mspasov
- (BOOL) isSameMonthAsDate: (NSDate *) aDate
{
NSDateComponents *components1 = [[NSDate currentCalendar] components:NSYearCalendarUnit | NSMonthCalendarUnit fromDate:self];
NSDateComponents *components2 = [[NSDate currentCalendar] components:NSYearCalendarUnit | NSMonthCalendarUnit fromDate:aDate];
NSDateComponents *components1 = [[NSDate currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth fromDate:self];
NSDateComponents *components2 = [[NSDate currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth fromDate:aDate];
return ((components1.month == components2.month) &&
(components1.year == components2.year));
}
Expand All @@ -229,8 +229,8 @@ - (BOOL) isNextMonth

- (BOOL) isSameYearAsDate: (NSDate *) aDate
{
NSDateComponents *components1 = [[NSDate currentCalendar] components:NSYearCalendarUnit fromDate:self];
NSDateComponents *components2 = [[NSDate currentCalendar] components:NSYearCalendarUnit fromDate:aDate];
NSDateComponents *components1 = [[NSDate currentCalendar] components:NSCalendarUnitYear fromDate:self];
NSDateComponents *components2 = [[NSDate currentCalendar] components:NSCalendarUnitYear fromDate:aDate];
return (components1.year == components2.year);
}

Expand All @@ -242,16 +242,16 @@ - (BOOL) isThisYear

- (BOOL) isNextYear
{
NSDateComponents *components1 = [[NSDate currentCalendar] components:NSYearCalendarUnit fromDate:self];
NSDateComponents *components2 = [[NSDate currentCalendar] components:NSYearCalendarUnit fromDate:[NSDate date]];
NSDateComponents *components1 = [[NSDate currentCalendar] components:NSCalendarUnitYear fromDate:self];
NSDateComponents *components2 = [[NSDate currentCalendar] components:NSCalendarUnitYear fromDate:[NSDate date]];

return (components1.year == (components2.year + 1));
}

- (BOOL) isLastYear
{
NSDateComponents *components1 = [[NSDate currentCalendar] components:NSYearCalendarUnit fromDate:self];
NSDateComponents *components2 = [[NSDate currentCalendar] components:NSYearCalendarUnit fromDate:[NSDate date]];
NSDateComponents *components1 = [[NSDate currentCalendar] components:NSCalendarUnitYear fromDate:self];
NSDateComponents *components2 = [[NSDate currentCalendar] components:NSCalendarUnitYear fromDate:[NSDate date]];

return (components1.year == (components2.year - 1));
}
Expand Down Expand Up @@ -282,7 +282,7 @@ - (BOOL) isInPast
#pragma mark - Roles
- (BOOL) isTypicallyWeekend
{
NSDateComponents *components = [[NSDate currentCalendar] components:NSWeekdayCalendarUnit fromDate:self];
NSDateComponents *components = [[NSDate currentCalendar] components:NSCalendarUnitWeekday fromDate:self];
if ((components.weekday == 1) ||
(components.weekday == 7))
return YES;
Expand Down Expand Up @@ -430,8 +430,8 @@ - (NSInteger) daysBeforeDate: (NSDate *) aDate
// I have not yet thoroughly tested this
- (NSInteger)distanceInDaysToDate:(NSDate *)anotherDate
{
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit fromDate:self toDate:anotherDate options:0];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitDay fromDate:self toDate:anotherDate options:0];
return components.day;
}

Expand All @@ -441,7 +441,7 @@ - (NSInteger) nearestHour
{
NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] + D_MINUTE * 30;
NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
NSDateComponents *components = [[NSDate currentCalendar] components:NSHourCalendarUnit fromDate:newDate];
NSDateComponents *components = [[NSDate currentCalendar] components:NSCalendarUnitHour fromDate:newDate];
return components.hour;
}

Expand Down Expand Up @@ -478,7 +478,7 @@ - (NSInteger) month
- (NSInteger) week
{
NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self];
return components.week;
return components.weekOfYear;
}

- (NSInteger) weekday
Expand Down