Skip to content

Instantly share code, notes, and snippets.

@azu
Created June 22, 2013 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azu/5841513 to your computer and use it in GitHub Desktop.
Save azu/5841513 to your computer and use it in GitHub Desktop.
dateByAddingComponents:toDate:options: vs dateByAddingTimeInterval:
//
// Created by azu on 2013/06/23.
// License MIT
//
#import <SenTestingKit/SenTestingKit.h>
#define SECONDS_IN_MINUTE 60
#define MINUTES_IN_HOUR 60
#define DAYS_IN_WEEK 7
#define SECONDS_IN_HOUR (SECONDS_IN_MINUTE * MINUTES_IN_HOUR)
#define HOURS_IN_DAY 24
#define SECONDS_IN_DAY (HOURS_IN_DAY * SECONDS_IN_HOUR)
// Test large Adding days
#define kAddingDays 10000
@interface dateByAddingTimeIntervalTests : SenTestCase
@end
@implementation dateByAddingTimeIntervalTests {
}
- (void)testDateByAddingTimeInterval {
NSDate *leapYearDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *aHundredDays = [leapYearDate dateByAddingTimeInterval:(SECONDS_IN_DAY * kAddingDays)];
NSDateComponents *diffComponents = [calendar components:NSDayCalendarUnit fromDate:leapYearDate toDate:aHundredDays options:0];
STAssertEquals([diffComponents day], kAddingDays, @"diff is %ddays", kAddingDays);
}
- (void)testDateByAddingComponents {
NSDate *leapYearDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *aHundredDaysComponents = [[NSDateComponents alloc] init];
aHundredDaysComponents.day = kAddingDays;
NSDate *aHundredDays = [calendar dateByAddingComponents:aHundredDaysComponents toDate:leapYearDate options:0];
NSDateComponents *diffComponents = [calendar components:NSDayCalendarUnit fromDate:leapYearDate toDate:aHundredDays options:0];
STAssertEquals([diffComponents day], kAddingDays, @"diff is %ddays", kAddingDays);
}
@end
@azu
Copy link
Author

azu commented Jun 22, 2013

dateByAddingTimeInterval: is fail

-[dateByAddingTimeIntervalTests testDateByAddingComponents] : '9999' should be equal to '10000': diff is 10000days

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment