Skip to content

Instantly share code, notes, and snippets.

@vickeryj
Created March 31, 2010 18:51
Show Gist options
  • Save vickeryj/350715 to your computer and use it in GitHub Desktop.
Save vickeryj/350715 to your computer and use it in GitHub Desktop.
- (void)testDateAdditions {
int daysAgoReversed = [[NSDate dateAtMidnightWithDaysAgo:900] numDaysAgo];
STAssertTrue(daysAgoReversed == 900, @"date methods should be reversible, but 900 != %d", daysAgoReversed);
NSDate *tomorrow = [[NSDate date] dateByAddingDays:1];
int tomorrowDaysAgo = [tomorrow numDaysAgo];
STAssertTrue(tomorrowDaysAgo == -1, @"tomorrow should be negative one day ago, but it was %d, "
"and we think tomorrow is %f seconds from 1970", tomorrowDaysAgo, [tomorrow timeIntervalSince1970]);
NSDate *yesterday = [[NSDate date] dateByAddingDays:-1];
int yesterdayDaysAgo = [yesterday numDaysAgo];
STAssertTrue(yesterdayDaysAgo == 1, @"yesterday should be one day ago, but it was %d, and we "
"think yesterday is %f seconds from 1970", yesterdayDaysAgo, [yesterday timeIntervalSince1970]);
NSDate *today = [NSDate date];
int todayDaysAgo = [today numDaysAgo];
STAssertTrue(0 == todayDaysAgo, @"today should be 0 days ago, but it was %d", todayDaysAgo);
NSDate *now = [NSDate date];
NSDate *nowWithNoDaysAdded = [now dateByAddingDays:0];
STAssertTrue(fabs([now timeIntervalSince1970] - [nowWithNoDaysAdded timeIntervalSince1970]) < 1,
@"adding no days should not change , but fabs(%f - %f) >= 1", [now timeIntervalSince1970],
[nowWithNoDaysAdded timeIntervalSince1970]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment