mirror of
https://github.com/samsonjs/Marshmallows.git
synced 2026-04-27 15:07:42 +00:00
add future dates to -[NSDate relativeToNow]
This commit is contained in:
parent
f401375380
commit
e48868e253
1 changed files with 50 additions and 4 deletions
|
|
@ -13,15 +13,58 @@
|
||||||
#define DAY (24.0 * HOUR)
|
#define DAY (24.0 * HOUR)
|
||||||
#define WEEK (7.0 * DAY)
|
#define WEEK (7.0 * DAY)
|
||||||
#define MONTH (30.0 * DAY)
|
#define MONTH (30.0 * DAY)
|
||||||
|
#define YEAR (365.25 * DAY)
|
||||||
|
|
||||||
@implementation NSDate (NSDate_relative)
|
@implementation NSDate (NSDate_relative)
|
||||||
|
|
||||||
- (NSString *) relativeToNow
|
- (NSString *) relativeToNow
|
||||||
{
|
{
|
||||||
double diff = [[NSDate date] timeIntervalSinceDate: self];
|
double diff = [[NSDate date] timeIntervalSinceDate: self];
|
||||||
if (diff < MINUTE) {
|
|
||||||
|
// future
|
||||||
|
if (diff < -2 * YEAR) {
|
||||||
|
return [NSString stringWithFormat: @"in %d years", abs(diff / YEAR)];
|
||||||
|
}
|
||||||
|
else if (diff < -YEAR) {
|
||||||
|
return @"next year";
|
||||||
|
}
|
||||||
|
else if (diff < -8 * WEEK) {
|
||||||
|
return [NSString stringWithFormat: @"in %d months", abs(diff / MONTH)];
|
||||||
|
}
|
||||||
|
else if (diff < -4 * WEEK) {
|
||||||
|
return @"next month";
|
||||||
|
}
|
||||||
|
else if (diff < -2 * WEEK) {
|
||||||
|
return [NSString stringWithFormat: @"in %d weeks", abs(diff / WEEK)];
|
||||||
|
}
|
||||||
|
else if (diff < -WEEK) {
|
||||||
|
return @"next week";
|
||||||
|
}
|
||||||
|
else if (diff < -2 * DAY) {
|
||||||
|
return [NSString stringWithFormat: @"in %d days", abs(diff / DAY)];
|
||||||
|
}
|
||||||
|
else if (diff < -DAY) {
|
||||||
|
return @"tomorrow";
|
||||||
|
}
|
||||||
|
else if (diff < -2 * HOUR) {
|
||||||
|
return [NSString stringWithFormat: @"in %d hours", abs(diff / HOUR)];
|
||||||
|
}
|
||||||
|
else if (diff < -HOUR) {
|
||||||
|
return @"in an hour";
|
||||||
|
}
|
||||||
|
else if (diff < -2 * MINUTE) {
|
||||||
|
return [NSString stringWithFormat: @"in %d minutes", abs(diff / MINUTE)];
|
||||||
|
}
|
||||||
|
else if (diff < -MINUTE) {
|
||||||
|
return @"in a minute";
|
||||||
|
}
|
||||||
|
|
||||||
|
// present
|
||||||
|
else if (diff < MINUTE) {
|
||||||
return @"right now";
|
return @"right now";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// past
|
||||||
else if (diff < 2 * MINUTE) {
|
else if (diff < 2 * MINUTE) {
|
||||||
return @"a minute ago";
|
return @"a minute ago";
|
||||||
}
|
}
|
||||||
|
|
@ -49,11 +92,14 @@
|
||||||
else if (diff < 8 * WEEK) {
|
else if (diff < 8 * WEEK) {
|
||||||
return @"last month";
|
return @"last month";
|
||||||
}
|
}
|
||||||
else if (diff < 12 * MONTH) {
|
else if (diff < YEAR) {
|
||||||
return [NSString stringWithFormat: @"%d months ago", (int)(diff / MONTH)];
|
return [NSString stringWithFormat: @"%d months ago", (int)(diff / MONTH)];
|
||||||
}
|
}
|
||||||
|
else if (diff < 2 * YEAR) {
|
||||||
|
return @"last year";
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
return @"a long time ago";
|
return [NSString stringWithFormat: @"%d years ago", (int)(diff / YEAR)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue