Skip to content

Commit

Permalink
[NEW] Add -[GITDateTime compare:] for commit-time sortablity.
Browse files Browse the repository at this point in the history
Signed-off-by: Geoff Garside <geoff@geoffgarside.co.uk>
  • Loading branch information
rentzsch authored and geoffgarside committed Jul 20, 2009
1 parent 7fb78de commit bf4f0ef
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions Source/Utils/GITDateTime.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
* \return An actor object with the extracted name and email.
*/
- (id) initWithBSDTime:(unsigned long)seconds timeZoneOffset:(NSInteger)tz;
- (NSComparisonResult)compare:(GITDateTime*)object;

@end
19 changes: 19 additions & 0 deletions Source/Utils/GITDateTime.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,23 @@ - (NSString*)description
return [NSString stringWithFormat:@"%.0f %@",
[self.date timeIntervalSince1970], [self.timezone offsetString]];
}
- (NSComparisonResult)compare:(GITDateTime*)anotherGITDateTime
{
NSParameterAssert(anotherGITDateTime);

NSCalendar *selfDateCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[selfDateCalendar setTimeZone:timezone];
NSCalendar *anotherDateCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[anotherDateCalendar setTimeZone:anotherGITDateTime.timezone];

NSCalendarUnit unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|kCFCalendarUnitHour|kCFCalendarUnitMinute|kCFCalendarUnitSecond;

NSDateComponents *selfDateComponents = [selfDateCalendar components:unitFlags fromDate:date];
NSDateComponents *anotherDateComponents = [anotherDateCalendar components:unitFlags fromDate:anotherGITDateTime.date];

NSDate *selfDate = [selfDateCalendar dateFromComponents:selfDateComponents];
NSDate *anotherDate = [anotherDateCalendar dateFromComponents:anotherDateComponents];

return [selfDate compare:anotherDate];
}
@end
33 changes: 33 additions & 0 deletions UnitTests/Source/GITDateTimeTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,37 @@ - (void)testDateTimeDescription
GITDateTime * dateTime = [[[GITDateTime alloc] initWithTimestamp:1214920980 timeZoneOffset:@"+0100"] autorelease];
GHAssertEqualObjects([dateTime description], @"1214920980 +0100", @"Should format datetime with timezone correctly");
}
- (void)testCompare
{
NSTimeInterval nowTimeInterval = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval beforeTimeInterval = nowTimeInterval - 1.0;
NSTimeInterval afterTimeInterval = nowTimeInterval + 1.0;
NSString *timeZoneOffset = @"+0100";

GITDateTime *before = [[[GITDateTime alloc] initWithTimestamp:beforeTimeInterval timeZoneOffset:timeZoneOffset] autorelease];
GITDateTime *now = [[[GITDateTime alloc] initWithTimestamp:nowTimeInterval timeZoneOffset:timeZoneOffset] autorelease];
GITDateTime *after = [[[GITDateTime alloc] initWithTimestamp:afterTimeInterval timeZoneOffset:timeZoneOffset] autorelease];

GHAssertThrowsSpecificNamed([before compare:nil], NSException, NSInternalInconsistencyException, nil);

GHAssertEquals([before compare:before], NSOrderedSame, nil);
GHAssertEquals([before compare:[[before copy] autorelease]], NSOrderedSame, nil);

GHAssertEquals([before compare:now], NSOrderedAscending, nil);
GHAssertEquals([before compare:after], NSOrderedAscending, nil);

GHAssertEquals([now compare:before], NSOrderedDescending, nil);
GHAssertEquals([now compare:after], NSOrderedAscending, nil);

GHAssertEquals([after compare:before], NSOrderedDescending, nil);
GHAssertEquals([after compare:now], NSOrderedDescending, nil);

NSArray *descArray = [NSArray arrayWithObjects:after, now, before, nil];
NSArray *descArraySortedToAscArray = [descArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES] autorelease]]];
GHAssertTrue(before == [descArraySortedToAscArray objectAtIndex:0], nil);
GHAssertTrue(now == [descArraySortedToAscArray objectAtIndex:1], nil);
GHAssertTrue(after == [descArraySortedToAscArray objectAtIndex:2], nil);

// TODO: test timezones
}
@end

0 comments on commit bf4f0ef

Please sign in to comment.