1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-16 10:20:55 +01:00
git-touch-android-ios-app/lib/utils/timeago.dart

32 lines
786 B
Dart
Raw Normal View History

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;
2019-02-05 13:57:05 +01:00
if (diff < 0) {
return 'in the future';
} else if (diff < 3600) {
return _pluralize(diff / 60, 'minute');
} else if (diff < 86400) {
return _pluralize(diff / 3600, 'hour');
} else {
return _pluralize(diff / 86400, 'day');
}
}
}