Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DistanceOfTimeInWords.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

/* Begin PBXFileReference section */
69EAD0211589E57700975F75 /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Localizable.strings"; sourceTree = "<group>"; };
948F1F1215ECBB9900AAC1C1 /* ru */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ru; path = ru.lproj/Localizable.strings; sourceTree = "<group>"; };
C701F2691359B29600468818 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
C701F26B1359B2AA00468818 /* es */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = es; path = es.lproj/Localizable.strings; sourceTree = "<group>"; };
C7A88B5D135460F2006F686D /* DistanceOfTimeInWords.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DistanceOfTimeInWords.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -309,6 +310,7 @@
C701F2691359B29600468818 /* en */,
C701F26B1359B2AA00468818 /* es */,
69EAD0211589E57700975F75 /* zh-Hans */,
948F1F1215ECBB9900AAC1C1 /* ru */,
);
name = Localizable.strings;
sourceTree = "<group>";
Expand Down
296 changes: 168 additions & 128 deletions DistanceOfTimeInWords/NSDate+Formatting.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,143 +30,183 @@
@implementation NSDate (formatting)

- (NSString *)formatWithString:(NSString *)format {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:format];
NSString *string = [formatter stringFromDate:self];
[formatter release];
return string;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:format];
NSString *string = [formatter stringFromDate:self];
return string;
}

- (NSString *)formatWithStyle:(NSDateFormatterStyle)style {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:style];
NSString *string = [formatter stringFromDate:self];
[formatter release];
return string;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:style];
NSString *string = [formatter stringFromDate:self];
return string;
}

- (NSString *)distanceOfTimeInWords {
return [self distanceOfTimeInWords:[NSDate date]];
return [self distanceOfTimeInWords:[NSDate date]];
}

- (NSString *)distanceOfTimeInWords:(NSDate *)date {
NSString *Ago = NSLocalizedString(@"ago", @"Denotes past dates");
NSString *FromNow = NSLocalizedString(@"from now", @"Denotes future dates");
NSString *LessThan = NSLocalizedString(@"Less than", @"Indicates a less-than number");
NSString *About = NSLocalizedString(@"About", @"Indicates an approximate number");
NSString *Over = NSLocalizedString(@"Over", @"Indicates an exceeding number");
NSString *Almost = NSLocalizedString(@"Almost", @"Indicates an approaching number");
//NSString *Second = NSLocalizedString(@"second", @"One second in time");
NSString *Seconds = NSLocalizedString(@"seconds", @"More than one second in time");
NSString *Minute = NSLocalizedString(@"minute", @"One minute in time");
NSString *Minutes = NSLocalizedString(@"minutes", @"More than one minute in time");
NSString *Hour = NSLocalizedString(@"hour", @"One hour in time");
NSString *Hours = NSLocalizedString(@"hours", @"More than one hour in time");
NSString *Day = NSLocalizedString(@"day", @"One day in time");
NSString *Days = NSLocalizedString(@"days", @"More than one day in time");
NSString *Month = NSLocalizedString(@"month", @"One month in time");
NSString *Months = NSLocalizedString(@"months", @"More than one month in time");
NSString *Year = NSLocalizedString(@"year", @"One year in time");
NSString *Years = NSLocalizedString(@"years", @"More than one year in time");

NSTimeInterval since = [self timeIntervalSinceDate:date];
NSString *direction = since <= 0.0 ? Ago : FromNow;
since = fabs(since);

int seconds = (int)since;
int minutes = (int)round(since / SECONDS_PER_MINUTE);
int hours = (int)round(since / SECONDS_PER_HOUR);
int days = (int)round(since / SECONDS_PER_DAY);
int months = (int)round(since / SECONDS_PER_MONTH);
int years = (int)floor(since / SECONDS_PER_YEAR);
int offset = (int)round(floor((float)years / 4.0) * 1440.0);
int remainder = (minutes - offset) % 525600;

int number;
NSString *measure;
NSString *modifier = @"";

switch (minutes) {
case 0 ... 1:
measure = Seconds;
switch (seconds) {
case 0 ... 4:
number = 5;
modifier = LessThan;
break;
case 5 ... 9:
number = 10;
modifier = LessThan;
break;
case 10 ... 19:
number = 20;
modifier = LessThan;
break;
case 20 ... 39:
number = 30;
modifier = About;
break;
case 40 ... 59:
number = 1;
measure = Minute;
modifier = LessThan;
break;
NSString *Ago = NSLocalizedString(@"ago", @"Denotes past dates");
NSString *FromNow = NSLocalizedString(@"from now", @"Denotes future dates");
NSString *LessThan = NSLocalizedString(@"Less than", @"Indicates a less-than number");
NSString *About = NSLocalizedString(@"About", @"Indicates an approximate number");
NSString *Over = NSLocalizedString(@"Over", @"Indicates an exceeding number");
NSString *Almost = NSLocalizedString(@"Almost", @"Indicates an approaching number");
//NSString *Second = NSLocalizedString(@"second", @"One second in time");
NSString *Seconds = NSLocalizedString(@"seconds", @"More than one second in time");
NSString *Minute = NSLocalizedString(@"minute", @"One minute in time");
NSString *MinutesSecondary = NSLocalizedString(@"minutes secondary", @"More than one minute in time");
NSString *Minutes = NSLocalizedString(@"minutes", @"More than one minute in time");
NSString *Hour = NSLocalizedString(@"hour", @"One hour in time");
NSString *HoursSecondary = NSLocalizedString(@"hours secondary", @"More than one hour in time");
NSString *Hours = NSLocalizedString(@"hours", @"More than one hour in time");
NSString *Day = NSLocalizedString(@"day", @"One day in time");
NSString *DaysSecondary = NSLocalizedString(@"days secondary", @"More than one day in time");
NSString *Days = NSLocalizedString(@"days", @"More than one day in time");
NSString *Month = NSLocalizedString(@"month", @"One month in time");
NSString *MonthsSecondary = NSLocalizedString(@"months secondary", @"More than one month in time");
NSString *Months = NSLocalizedString(@"months", @"More than one month in time");
NSString *Year = NSLocalizedString(@"year", @"One year in time");
NSString *YearsSecondary = NSLocalizedString(@"years secondary", @"More than one year in time");
NSString *Years = NSLocalizedString(@"years", @"More than one year in time");

NSTimeInterval since = [self timeIntervalSinceDate:date];
NSString *direction = since <= 0.0 ? Ago : FromNow;
since = fabs(since);

int seconds = (int)since;
int minutes = (int)round(since / SECONDS_PER_MINUTE);
int hours = (int)round(since / SECONDS_PER_HOUR);
int days = (int)round(since / SECONDS_PER_DAY);
int months = (int)round(since / SECONDS_PER_MONTH);
int years = (int)floor(since / SECONDS_PER_YEAR);
int offset = (int)round(floor((float)years / 4.0) * 1440.0);
int remainder = (minutes - offset) % 525600;

int number;
NSString *measure;
NSString *modifier = @"";

switch (minutes) {
case 0 ... 1:
measure = Seconds;
switch (seconds) {
case 0 ... 4:
number = 5;
modifier = LessThan;
break;
case 5 ... 9:
number = 10;
modifier = LessThan;
break;
case 10 ... 19:
number = 20;
modifier = LessThan;
break;
case 20 ... 39:
number = 30;
modifier = About;
break;
case 40 ... 59:
number = 1;
measure = Minute;
modifier = LessThan;
break;
default:
number = 1;
measure = Minute;
modifier = About;
break;
}
break;
case 2 ... 44:
switch (minutes) {
case 2 ... 4:
measure = MinutesSecondary;
break;
default:
measure = Minutes;
break;
}
number = minutes;
break;
case 45 ... 89:
number = 1;
measure = Hour;
modifier = About;
break;
case 90 ... 1439:
switch (minutes) {
case 90 ... 239:
measure = HoursSecondary;
break;
default:
measure = Hours;
break;
}
number = hours;
modifier = About;
break;
case 1440 ... 2529:
number = 1;
measure = Day;
break;
case 2530 ... 43199:
switch (minutes) {
case 2880 ... 7199:
measure = DaysSecondary;
break;
default:
measure = Days;
break;
}

number = days;
break;
case 43200 ... 86399:
number = 1;
measure = Month;
modifier = About;
break;
case 86400 ... 525599:
switch (minutes) {
case 86400 ... 215999:
measure = MonthsSecondary;
break;
default:
measure = Months;
break;
}

number = months;
break;
default:
number = 1;
measure = Minute;
modifier = About;
break;
}
break;
case 2 ... 44:
number = minutes;
measure = Minutes;
break;
case 45 ... 89:
number = 1;
measure = Hour;
modifier = About;
break;
case 90 ... 1439:
number = hours;
measure = Hours;
modifier = About;
break;
case 1440 ... 2529:
number = 1;
measure = Day;
break;
case 2530 ... 43199:
number = days;
measure = Days;
break;
case 43200 ... 86399:
number = 1;
measure = Month;
modifier = About;
break;
case 86400 ... 525599:
number = months;
measure = Months;
break;
default:
number = years;
measure = number == 1 ? Year : Years;
if (remainder < 131400) {
modifier = About;
} else if (remainder < 394200) {
modifier = Over;
} else {
++number;
measure = Years;
modifier = Almost;
}
break;
}
if ([modifier length] > 0) {
modifier = [modifier stringByAppendingString:@" "];
}
return [NSString stringWithFormat:@"%@%d %@ %@", modifier, number, measure, direction];
number = years;
if (number == 1) {
measure = Year;
} else if (number > 1 && number < 5) {
measure = YearsSecondary;
} else {
measure = Years;
}

if (remainder < 131400) {
modifier = About;
} else if (remainder < 394200) {
modifier = Over;
} else {
++number;
measure = Years;
modifier = Almost;
}
break;
}
if ([modifier length] > 0) {
modifier = [modifier stringByAppendingString:@" "];
}
return [NSString stringWithFormat:@"%@%d %@ %@", modifier, number, measure, direction];
}

@end
@end
Binary file modified DistanceOfTimeInWords/en.lproj/Localizable.strings
Binary file not shown.
Binary file modified DistanceOfTimeInWords/es.lproj/Localizable.strings
Binary file not shown.
Binary file not shown.
Binary file modified DistanceOfTimeInWords/zh-Hans.lproj/Localizable.strings
Binary file not shown.