2019-01-26 15:10:18 +01:00
|
|
|
import 'dart:core';
|
|
|
|
|
|
|
|
class TimeAgo {
|
|
|
|
static String _ceil(double n) => n.ceil().toString();
|
|
|
|
|
|
|
|
static String _pluralize(double time, String unit) {
|
|
|
|
if (time == 1) {
|
|
|
|
return '${_ceil(time)} $unit ago';
|
|
|
|
}
|
|
|
|
return '${_ceil(time)} ${unit}s ago';
|
|
|
|
}
|
|
|
|
|
2019-01-27 17:37:44 +01:00
|
|
|
static String formatFromString(String str) {
|
|
|
|
return format(DateTime.parse(str));
|
|
|
|
}
|
|
|
|
|
2019-01-26 15:10:18 +01:00
|
|
|
static String format(DateTime time) {
|
|
|
|
double diff =
|
|
|
|
(DateTime.now().millisecondsSinceEpoch - time.millisecondsSinceEpoch) /
|
|
|
|
1000;
|
2019-02-05 13:57:05 +01:00
|
|
|
if (diff < 0) {
|
|
|
|
return 'in the future';
|
|
|
|
} else if (diff < 3600) {
|
2019-01-26 15:10:18 +01:00
|
|
|
return _pluralize(diff / 60, 'minute');
|
|
|
|
} else if (diff < 86400) {
|
|
|
|
return _pluralize(diff / 3600, 'hour');
|
|
|
|
} else {
|
|
|
|
return _pluralize(diff / 86400, 'day');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|