1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-16 02:11:11 +01:00
git-touch-android-ios-app/lib/utils/timeago.dart
2019-01-28 00:37:44 +08:00

30 lines
729 B
Dart

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';
}
static String formatFromString(String str) {
return format(DateTime.parse(str));
}
static String format(DateTime time) {
double diff =
(DateTime.now().millisecondsSinceEpoch - time.millisecondsSinceEpoch) /
1000;
if (diff < 3600) {
return _pluralize(diff / 60, 'minute');
} else if (diff < 86400) {
return _pluralize(diff / 3600, 'hour');
} else {
return _pluralize(diff / 86400, 'day');
}
}
}