Skip to content

Commit

Permalink
Closes Issue #108: Can't add tasks or open lists with custom region s…
Browse files Browse the repository at this point in the history
…ettings.

- Changed NSDateFormatter locale in TTMDateUtility class to work around bugs related to setting the "high" date in some locales (namely Russia with 12 hour clock).
- Integration tests were performed with several locales. Unit tests all passed.
  • Loading branch information
mjdescy committed Apr 25, 2016
1 parent 4740a4c commit 966325e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
19 changes: 13 additions & 6 deletions TodoTxtMac/TTMDateUtility.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,26 @@ + (NSDate*)convertStringToDate:(NSString*)dateString {
NSString *dateTimeString = [dateString stringByAppendingString:@" 00:00:00"];

// Convert dateString to NSDate.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDateFormatter *dateFormatter = [self dateFormatter];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
return [dateFormatter dateFromString:dateTimeString];
}

+ (NSDateFormatter*)dateFormatter {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[dateFormatter setCalendar:gregorian];
[dateFormatter setLocale:[NSLocale systemLocale]];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
return dateFormatter;
}

+ (NSString*)convertDateToString:(NSDate*)date {
if (!date) {
return nil;
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
return [dateFormatter stringFromDate:date];
NSDateFormatter *dateFormatter = [self dateFormatter];
return [dateFormatter stringFromDate:date];
}

+ (NSDate*)today {
Expand Down Expand Up @@ -175,8 +183,7 @@ + (NSDate*)relativeDateFromWeekdayName:(NSString*)weekdayName
return nil;
}

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDateFormatter *dateFormatter = [self dateFormatter];
[dateFormatter setDateFormat:dateFormat];

NSDate *todaysDate = [self today];
Expand Down
22 changes: 21 additions & 1 deletion TodoTxtMacTests/TTMDateUtility_UnitTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ - (void)tearDown {
[super tearDown];
}

- (void)testDateConversions {
- (void)testDateConversions_WithNormalDate {
NSDate *firstDate = [TTMDateUtility convertStringToDate:@"2014-01-01"];
NSLog(@"firstDate: %@", firstDate);
NSString *firstDateString = [TTMDateUtility convertDateToString:firstDate];
Expand All @@ -74,6 +74,26 @@ - (void)testDateConversions {
XCTAssertEqualObjects(firstDate, secondDate);
}

- (void)testDateConversions_WithHighDate {
NSDate *firstDate = [TTMDateUtility convertStringToDate:@"9999-12-31"];
NSLog(@"firstDate: %@", firstDate);
NSString *firstDateString = [TTMDateUtility convertDateToString:firstDate];
NSLog(@"firstDateString: %@", firstDateString);
NSDate *secondDate = [TTMDateUtility convertStringToDate:firstDateString];
NSLog(@"secondDate: %@", secondDate);
XCTAssertEqualObjects(firstDate, secondDate);
}

- (void)testDateConversions_WithLowDate {
NSDate *firstDate = [TTMDateUtility convertStringToDate:@"1900-01-01"];
NSLog(@"firstDate: %@", firstDate);
NSString *firstDateString = [TTMDateUtility convertDateToString:firstDate];
NSLog(@"firstDateString: %@", firstDateString);
NSDate *secondDate = [TTMDateUtility convertStringToDate:firstDateString];
NSLog(@"secondDate: %@", secondDate);
XCTAssertEqualObjects(firstDate, secondDate);
}

- (void)testAddDaysToDate {
NSDate *firstDate = [TTMDateUtility convertStringToDate:@"2014-01-01"];
NSDate *secondDate = [TTMDateUtility convertStringToDate:@"2014-01-02"];
Expand Down

0 comments on commit 966325e

Please sign in to comment.