Skip to content

Commit

Permalink
fixing some bad assumptions
Browse files Browse the repository at this point in the history
  • Loading branch information
atomkirk committed Apr 17, 2015
1 parent 3f7f556 commit 059fe32
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 147 deletions.
182 changes: 35 additions & 147 deletions MTDates/NSDate+MTDates.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ @implementation NSDate (MTDates)
static NSString *__calendarType = nil;
static NSLocale *__locale = nil;
static NSTimeZone *__timeZone = nil;
static NSInteger __firstWeekday = 0;
static NSInteger __firstWeekday = 0;
static MTDateWeekNumberingSystem __weekNumberingSystem = 0;

static NSDateFormatterStyle __dateStyle = NSDateFormatterShortStyle;
Expand Down Expand Up @@ -452,7 +452,14 @@ - (NSTimeInterval)mt_secondsIntoDay
- (NSDateComponents *)mt_components
{
[[NSDate sharedRecursiveLock] lock];
NSCalendarUnit units = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSCalendarUnit units = (NSCalendarUnitYear |
NSCalendarUnitMonth |
NSCalendarUnitWeekOfYear |
NSCalendarUnitWeekday |
NSCalendarUnitDay |
NSCalendarUnitHour |
NSCalendarUnitMinute |
NSCalendarUnitSecond);
NSDateComponents *dateComponents = [[NSDate mt_calendar] components:units fromDate:self];
[[NSDate sharedRecursiveLock] unlock];
return dateComponents;
Expand Down Expand Up @@ -546,25 +553,15 @@ - (NSDate *)mt_endOfNextYear
- (NSDate *)mt_oneYearPrevious
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year] - 1
month:[self mt_monthOfYear]
day:[self mt_dayOfMonth]
hour:[self mt_hourOfDay]
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:-1 months:0 weeks:0 days:0 hours:0 minutes:0 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}

- (NSDate *)mt_oneYearNext
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year] + 1
month:[self mt_monthOfYear]
day:[self mt_dayOfMonth]
hour:[self mt_hourOfDay]
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:1 months:0 weeks:0 days:0 hours:0 minutes:0 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}
Expand All @@ -573,25 +570,15 @@ - (NSDate *)mt_oneYearNext
- (NSDate *)mt_dateYearsBefore:(NSInteger)years
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year] - years
month:[self mt_monthOfYear]
day:[self mt_dayOfMonth]
hour:[self mt_hourOfDay]
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:-years months:0 weeks:0 days:0 hours:0 minutes:0 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}

- (NSDate *)mt_dateYearsAfter:(NSInteger)years
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year] + years
month:[self mt_monthOfYear]
day:[self mt_dayOfMonth]
hour:[self mt_hourOfDay]
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:years months:0 weeks:0 days:0 hours:0 minutes:0 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}
Expand Down Expand Up @@ -701,25 +688,15 @@ - (NSDate *)mt_endOfNextMonth
- (NSDate *)mt_oneMonthPrevious
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year]
month:[self mt_monthOfYear] - 1
day:[self mt_dayOfMonth]
hour:[self mt_hourOfDay]
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:0 months:-1 weeks:0 days:0 hours:0 minutes:0 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}

- (NSDate *)mt_oneMonthNext
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year]
month:[self mt_monthOfYear] + 1
day:[self mt_dayOfMonth]
hour:[self mt_hourOfDay]
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:0 months:1 weeks:0 days:0 hours:0 minutes:0 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}
Expand All @@ -728,25 +705,15 @@ - (NSDate *)mt_oneMonthNext
- (NSDate *)mt_dateMonthsBefore:(NSInteger)months
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year]
month:[self mt_monthOfYear] - months
day:[self mt_dayOfMonth]
hour:[self mt_hourOfDay]
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:0 months:-months weeks:0 days:0 hours:0 minutes:0 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}

- (NSDate *)mt_dateMonthsAfter:(NSInteger)months
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year]
month:[self mt_monthOfYear] + months
day:[self mt_dayOfMonth]
hour:[self mt_hourOfDay]
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:0 months:months weeks:0 days:0 hours:0 minutes:0 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}
Expand Down Expand Up @@ -861,51 +828,31 @@ - (NSDate *)mt_endOfNextWeek
- (NSDate *)mt_oneWeekPrevious
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year]
week:[self mt_weekOfYear] - 1
weekday:[self mt_weekdayOfWeek]
hour:[self mt_hourOfDay]
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:0 months:0 weeks:-1 days:0 hours:0 minutes:0 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}

- (NSDate *)mt_oneWeekNext
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year]
week:[self mt_weekOfYear] + 1
weekday:[self mt_weekdayOfWeek]
hour:[self mt_hourOfDay]
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:0 months:0 weeks:1 days:0 hours:0 minutes:0 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}

- (NSDate *)mt_dateWeeksBefore:(NSInteger)weeks
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year]
week:[self mt_weekOfYear] - weeks
weekday:[self mt_weekdayOfWeek]
hour:[self mt_hourOfDay]
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:0 months:0 weeks:-weeks days:0 hours:0 minutes:0 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}

- (NSDate *)mt_dateWeeksAfter:(NSInteger)weeks
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year]
week:[self mt_weekOfYear] + weeks
weekday:[self mt_weekdayOfWeek]
hour:[self mt_hourOfDay]
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:0 months:0 weeks:weeks days:0 hours:0 minutes:0 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}
Expand Down Expand Up @@ -1016,25 +963,15 @@ - (NSDate *)mt_endOfNextDay
- (NSDate *)mt_oneDayPrevious
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year]
month:[self mt_monthOfYear]
day:[self mt_dayOfMonth] - 1
hour:[self mt_hourOfDay]
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:0 months:0 weeks:0 days:-1 hours:0 minutes:0 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}

- (NSDate *)mt_oneDayNext
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year]
month:[self mt_monthOfYear]
day:[self mt_dayOfMonth] + 1
hour:[self mt_hourOfDay]
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:0 months:0 weeks:0 days:1 hours:0 minutes:0 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}
Expand All @@ -1043,25 +980,15 @@ - (NSDate *)mt_oneDayNext
- (NSDate *)mt_dateDaysBefore:(NSInteger)days
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year]
month:[self mt_monthOfYear]
day:[self mt_dayOfMonth] - days
hour:[self mt_hourOfDay]
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:0 months:0 weeks:0 days:-days hours:0 minutes:0 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}

- (NSDate *)mt_dateDaysAfter:(NSInteger)days
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year]
month:[self mt_monthOfYear]
day:[self mt_dayOfMonth] + days
hour:[self mt_hourOfDay]
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:0 months:0 weeks:0 days:days hours:0 minutes:0 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}
Expand Down Expand Up @@ -1172,25 +1099,15 @@ - (NSDate *)mt_endOfNextHour
- (NSDate *)mt_oneHourPrevious
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year]
month:[self mt_monthOfYear]
day:[self mt_dayOfMonth]
hour:[self mt_hourOfDay] - 1
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:0 months:0 weeks:0 days:0 hours:-1 minutes:0 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}

- (NSDate *)mt_oneHourNext
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year]
month:[self mt_monthOfYear]
day:[self mt_dayOfMonth]
hour:[self mt_hourOfDay] + 1
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:0 months:0 weeks:0 days:0 hours:1 minutes:0 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}
Expand All @@ -1199,25 +1116,15 @@ - (NSDate *)mt_oneHourNext
- (NSDate *)mt_dateHoursBefore:(NSInteger)hours
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year]
month:[self mt_monthOfYear]
day:[self mt_dayOfMonth]
hour:[self mt_hourOfDay] - hours
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:0 months:0 weeks:0 days:0 hours:-hours minutes:0 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}

- (NSDate *)mt_dateHoursAfter:(NSInteger)hours
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year]
month:[self mt_monthOfYear]
day:[self mt_dayOfMonth]
hour:[self mt_hourOfDay] + hours
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:0 months:0 weeks:0 days:0 hours:hours minutes:0 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}
Expand Down Expand Up @@ -1328,25 +1235,15 @@ - (NSDate *)mt_endOfNextMinute
- (NSDate *)mt_oneMinutePrevious
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year]
month:[self mt_monthOfYear]
day:[self mt_dayOfMonth]
hour:[self mt_hourOfDay]
minute:[self mt_minuteOfHour] - 1
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:0 months:0 weeks:0 days:0 hours:0 minutes:-1 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}

- (NSDate *)mt_oneMinuteNext
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year]
month:[self mt_monthOfYear]
day:[self mt_dayOfMonth]
hour:[self mt_hourOfDay]
minute:[self mt_minuteOfHour] + 1
second:[self mt_secondOfMinute]];
NSDate *date = [self mt_dateByAddingYears:0 months:0 weeks:0 days:0 hours:0 minutes:1 seconds:0];
[[NSDate sharedRecursiveLock] unlock];
return date;
}
Expand Down Expand Up @@ -1410,25 +1307,15 @@ - (NSDate *)mt_startOfNextSecond
- (NSDate *)mt_oneSecondPrevious
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year]
month:[self mt_monthOfYear]
day:[self mt_dayOfMonth]
hour:[self mt_hourOfDay]
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute] - 1];
NSDate *date = [self mt_dateByAddingYears:0 months:0 weeks:0 days:0 hours:0 minutes:0 seconds:-1];
[[NSDate sharedRecursiveLock] unlock];
return date;
}

- (NSDate *)mt_oneSecondNext
{
[[NSDate sharedRecursiveLock] lock];
NSDate *date = [NSDate mt_dateFromYear:[self mt_year]
month:[self mt_monthOfYear]
day:[self mt_dayOfMonth]
hour:[self mt_hourOfDay]
minute:[self mt_minuteOfHour]
second:[self mt_secondOfMinute] + 1];
NSDate *date = [self mt_dateByAddingYears:0 months:0 weeks:0 days:0 hours:0 minutes:0 seconds:-1];
[[NSDate sharedRecursiveLock] unlock];
return date;
}
Expand Down Expand Up @@ -1814,7 +1701,8 @@ - (BOOL)mt_isInAM
- (BOOL)mt_isStartOfAnHour
{
[[NSDate sharedRecursiveLock] lock];
BOOL isStartOfAnHour = [self mt_minuteOfHour] == (NSInteger)[NSDate mt_minValueForUnit:NSCalendarUnitMinute] && [self mt_secondOfMinute] == (NSInteger)[NSDate mt_minValueForUnit:NSCalendarUnitSecond];
BOOL isStartOfAnHour = ([self mt_minuteOfHour] == (NSInteger)[NSDate mt_minValueForUnit:NSCalendarUnitMinute] &&
[self mt_secondOfMinute] == (NSInteger)[NSDate mt_minValueForUnit:NSCalendarUnitSecond]);
[[NSDate sharedRecursiveLock] unlock];
return isStartOfAnHour;
}
Expand Down
9 changes: 9 additions & 0 deletions MTDatesTests/MTDatesTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,15 @@ - (void)test_endOfCurrentHour
XCTAssertEqualObjects([date mt_endOfCurrentHour], date2);
}

//- (void)test_endOfCurrentHourDST
//{
// _formatter.dateFormat = @"MM/dd/yyyy hh:mm:ssa";
// _formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
// NSDate *date = [_formatter dateFromString:@"11/01/2015 02:30:00am"];
// NSDate *date2 = [_formatter dateFromString:@"11/01/2015 02:59:59am"];
// XCTAssertEqualObjects([date mt_endOfCurrentHour], date2);
//}

- (void)test_endOfNextHour
{
_formatter.dateFormat = @"MM/dd/yyyy hh:mm:ssa";
Expand Down

3 comments on commit 059fe32

@yas375
Copy link
Contributor

@yas375 yas375 commented on 059fe32 Jan 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@atomkirk ditto with this if you have some time to explain please. I'm updating to the latest version of the library so trying to catch up on the changes. Thanks in advance.

@atomkirk
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea. Really dumb commit message. Ive learned to write better ones.

My best guess is that my previous method didnt factor in leap years/seconds and DST

@yas375
Copy link
Contributor

@yas375 yas375 commented on 059fe32 Jan 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. Thanks :)

Please sign in to comment.