"time ago" 标签使用什么分辨率
What resolution to use with "time ago" labels
我有一个显示特定硬件状态的应用程序。我知道硬件进入所述状态(失败、活动、空闲、未连接等)的时间。我一直在尝试用 "ago" 时间显示状态。但是我一直在纠结如何调整分辨率。
不到一个小时很容易,只需显示 "minutes ago"。但是,当您进入数小时前时,分钟变得越来越不重要。我不确定在 61 分钟时突然跳到“1 小时前”是否正确。以及何时切换到“2 小时前”。你四舍五入,还是截断了那个计算。同样的困境也存在于 24 小时点。你是在 25 小时前展示,还是只说 1 天前。或者应该有一个时间段,我显示“1 小时 13 分钟前”,然后在某个时候删除分钟数。
我知道这些 "ago" 标签不是原创的。我很好奇其他人是如何实现的。
Moment.js 是一个非常流行的用于 web 开发的库,它可以按照你的要求输出 time from now。下面列出了他们如何将间隔分解为更易于理解的格式。
来自他们的文档:
- 0 到 45 秒
seconds ago
- 45 到 90 秒
a minute ago
- 90 秒到 45 分钟
2 minutes ago ... 45 minutes ago
- 45 到 90 分钟
an hour ago
- 90 分钟到 22 小时
2 hours ago ... 22 hours ago
- 22 到 36 小时
a day ago
- 36 小时到 25 天
2 days ago ... 25 days ago
- 25 到 45 天
a month ago
- 45 到 345 天
2 months ago ... 11 months ago
- 345 到 547 天(1.5 年)
a year ago
- 548 天+
2 years ago ... 20 years ago
作为对@JimmyBoh 回答的补充,我想我应该添加我使用的 ObjectiveC 代码:
#import "NSDate+AgoPrint.h"
#define SECONDS(s) (s)
#define MINUTES(m) ((m) * 60)
#define HOURS(h) ((h) * 3600)
#define DAYS(d) ((d) * 3600 * 24)
#define YEARS(y) ((y) * 3600 * 24 * 365)
@implementation NSDate (AgoPrint)
- (NSString*) simpleAgoString {
NSTimeInterval ago = -self.timeIntervalSinceNow;
if (ago <= SECONDS(45)) { // 0 to 45 seconds seconds ago
return [NSString stringWithFormat: @"%u seconds ago", (unsigned int)round(ago)];
}
else if (ago <= SECONDS(90)) { //45 to 90 seconds a minute ago
return @"a minute ago";
}
else if (ago <= MINUTES(45)) { //90 seconds to 45 minutes 2 minutes ago ... 45 minutes ago
return [NSString stringWithFormat: @"%u minutes ago", (unsigned int)round(ago / MINUTES(1))];
}
else if (ago <= MINUTES(90)) { // 45 to 90 minutes an hour ago
return @"an hour ago";
}
else if (ago <= HOURS(22)) { // 90 minutes to 22 hours 2 hours ago ... 22 hours ago
return [NSString stringWithFormat: @"%u hours ago", (unsigned int)round(ago / HOURS(1))];
}
else if (ago <= HOURS(36)) { // 22 to 36 hours a day ago
return @"a day ago";
}
else if (ago <= DAYS(25)) { // 36 hours to 25 days 2 days ago ... 25 days ago
return [NSString stringWithFormat: @"%u days ago", (unsigned int)round(ago / DAYS(1))];
}
else if (ago <= DAYS(45)) { // 25 to 45 days a month ago
return @"a month ago";
}
else if (ago <= DAYS(345)) { // 45 to 345 days 2 months ago ... 11 months ago
return [NSString stringWithFormat: @"%u months ago", (unsigned int)round(ago / DAYS(30))];
}
else if (ago < DAYS(547)) { // 345 to 547 days (1.5 years) a year ago
return @"a year ago";
}
else { // 548 days+ 2 years ago ... 20 years ago
return [NSString stringWithFormat: @"%u years ago", (unsigned int)round(ago / YEARS(1))];
}
}
@end
我最终可能会增加几周或替换几个月或几周,但一般方法似乎很清楚我将如何插入它。
我有一个显示特定硬件状态的应用程序。我知道硬件进入所述状态(失败、活动、空闲、未连接等)的时间。我一直在尝试用 "ago" 时间显示状态。但是我一直在纠结如何调整分辨率。
不到一个小时很容易,只需显示 "minutes ago"。但是,当您进入数小时前时,分钟变得越来越不重要。我不确定在 61 分钟时突然跳到“1 小时前”是否正确。以及何时切换到“2 小时前”。你四舍五入,还是截断了那个计算。同样的困境也存在于 24 小时点。你是在 25 小时前展示,还是只说 1 天前。或者应该有一个时间段,我显示“1 小时 13 分钟前”,然后在某个时候删除分钟数。
我知道这些 "ago" 标签不是原创的。我很好奇其他人是如何实现的。
Moment.js 是一个非常流行的用于 web 开发的库,它可以按照你的要求输出 time from now。下面列出了他们如何将间隔分解为更易于理解的格式。
来自他们的文档:
- 0 到 45 秒
seconds ago
- 45 到 90 秒
a minute ago
- 90 秒到 45 分钟
2 minutes ago ... 45 minutes ago
- 45 到 90 分钟
an hour ago
- 90 分钟到 22 小时
2 hours ago ... 22 hours ago
- 22 到 36 小时
a day ago
- 36 小时到 25 天
2 days ago ... 25 days ago
- 25 到 45 天
a month ago
- 45 到 345 天
2 months ago ... 11 months ago
- 345 到 547 天(1.5 年)
a year ago
- 548 天+
2 years ago ... 20 years ago
作为对@JimmyBoh 回答的补充,我想我应该添加我使用的 ObjectiveC 代码:
#import "NSDate+AgoPrint.h"
#define SECONDS(s) (s)
#define MINUTES(m) ((m) * 60)
#define HOURS(h) ((h) * 3600)
#define DAYS(d) ((d) * 3600 * 24)
#define YEARS(y) ((y) * 3600 * 24 * 365)
@implementation NSDate (AgoPrint)
- (NSString*) simpleAgoString {
NSTimeInterval ago = -self.timeIntervalSinceNow;
if (ago <= SECONDS(45)) { // 0 to 45 seconds seconds ago
return [NSString stringWithFormat: @"%u seconds ago", (unsigned int)round(ago)];
}
else if (ago <= SECONDS(90)) { //45 to 90 seconds a minute ago
return @"a minute ago";
}
else if (ago <= MINUTES(45)) { //90 seconds to 45 minutes 2 minutes ago ... 45 minutes ago
return [NSString stringWithFormat: @"%u minutes ago", (unsigned int)round(ago / MINUTES(1))];
}
else if (ago <= MINUTES(90)) { // 45 to 90 minutes an hour ago
return @"an hour ago";
}
else if (ago <= HOURS(22)) { // 90 minutes to 22 hours 2 hours ago ... 22 hours ago
return [NSString stringWithFormat: @"%u hours ago", (unsigned int)round(ago / HOURS(1))];
}
else if (ago <= HOURS(36)) { // 22 to 36 hours a day ago
return @"a day ago";
}
else if (ago <= DAYS(25)) { // 36 hours to 25 days 2 days ago ... 25 days ago
return [NSString stringWithFormat: @"%u days ago", (unsigned int)round(ago / DAYS(1))];
}
else if (ago <= DAYS(45)) { // 25 to 45 days a month ago
return @"a month ago";
}
else if (ago <= DAYS(345)) { // 45 to 345 days 2 months ago ... 11 months ago
return [NSString stringWithFormat: @"%u months ago", (unsigned int)round(ago / DAYS(30))];
}
else if (ago < DAYS(547)) { // 345 to 547 days (1.5 years) a year ago
return @"a year ago";
}
else { // 548 days+ 2 years ago ... 20 years ago
return [NSString stringWithFormat: @"%u years ago", (unsigned int)round(ago / YEARS(1))];
}
}
@end
我最终可能会增加几周或替换几个月或几周,但一般方法似乎很清楚我将如何插入它。