diff --git a/Foundation/CPDate.j b/Foundation/CPDate.j index 86bf25deb..736ca9ff2 100644 --- a/Foundation/CPDate.j +++ b/Foundation/CPDate.j @@ -146,6 +146,15 @@ var CPDateReferenceDate = new Date(Date.UTC(2001, 0, 1, 0, 0, 0, 0)); return [[CPDate date] timeIntervalSinceReferenceDate]; } +/** + Return a new date representing the receiver's time plus the given interval into the future, + or into the past for a negative interval. +*/ +- (id)dateByAddingTimeInterval:(CPTimeInterval)seconds +{ + return [[CPDate alloc] initWithTimeInterval:seconds sinceDate:self]; +} + - (BOOL)isEqual:(CPDate)aDate { if (self === aDate) diff --git a/Tests/Foundation/CPDateTest.j b/Tests/Foundation/CPDateTest.j index 996d05787..4223e2168 100644 --- a/Tests/Foundation/CPDateTest.j +++ b/Tests/Foundation/CPDateTest.j @@ -35,6 +35,30 @@ [self assert:middle equals:[middle laterDate:past] message:"laterDate incorrect"]; } +- (void)testDateByAddingTimeIntervalZeroShouldReturnSameDate +{ + var a = [CPDate dateWithTimeIntervalSince1970:1231889490.0], + b = [a dateByAddingTimeInterval:0]; + + [self assert:[b timeIntervalSince1970] equals:1231889490]; +} + +- (void)testDateByAddingTimeIntervalPositiveShouldReturnNewerDate +{ + var a = [CPDate dateWithTimeIntervalSince1970:1231889490.0], + b = [a dateByAddingTimeInterval:789]; + + [self assert:[b timeIntervalSince1970] equals:1231889490 + 789]; +} + +- (void)testDateByAddingTimeIntervalNegativeShouldReturnOlderDate +{ + var a = [CPDate dateWithTimeIntervalSince1970:1231889490.0], + b = [a dateByAddingTimeInterval:-789]; + + [self assert:[b timeIntervalSince1970] equals:1231889490 - 789]; +} + - (void)testEquals { var now = [CPDate date];