2024-06-14 20:47:36 +00:00
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
import 'package:tetra_stats/gen/strings.g.dart';
|
2024-06-11 16:30:13 +00:00
|
|
|
import 'package:tetra_stats/utils/numers_formats.dart';
|
|
|
|
|
2024-06-14 20:47:36 +00:00
|
|
|
final NumberFormat secs = NumberFormat("00.###", LocaleSettings.currentLocale.languageCode);
|
|
|
|
final NumberFormat _timeInSec = NumberFormat("#,###.###s.", LocaleSettings.currentLocale.languageCode);
|
|
|
|
|
2024-06-11 16:30:13 +00:00
|
|
|
/// Returns string, that represents time difference between [dateTime] and now
|
|
|
|
String relativeDateTime(DateTime dateTime){
|
|
|
|
Duration difference = dateTime.difference(DateTime.now());
|
|
|
|
bool inPast = difference.isNegative;
|
|
|
|
Duration absDifference = difference.abs();
|
|
|
|
double timeInterval;
|
|
|
|
|
|
|
|
// years
|
|
|
|
timeInterval = absDifference.inSeconds / 31536000;
|
|
|
|
if (timeInterval >= 100.0) {
|
|
|
|
return inPast ? "${timeInterval.truncate()} years ago" : "in ${timeInterval.truncate()} years";
|
|
|
|
} else if (timeInterval >= 10.0) {
|
|
|
|
return inPast ? "${f1.format(timeInterval)} years ago" : "in ${f1.format(timeInterval)} years";
|
|
|
|
} else if (timeInterval >= 1.0) {
|
|
|
|
return inPast ? "${f2.format(timeInterval)} years ago" : "in ${f2.format(timeInterval)} years";
|
|
|
|
}
|
|
|
|
|
|
|
|
// months
|
|
|
|
timeInterval = absDifference.inSeconds / 2592000;
|
|
|
|
if (timeInterval >= 10.0) {
|
|
|
|
return inPast ? "${timeInterval.truncate()} months ago" : "in ${timeInterval.truncate()} months";
|
|
|
|
} else if (timeInterval >= 1.0) {
|
|
|
|
return inPast ? "${f1.format(timeInterval)} months ago" : "in ${f1.format(timeInterval)} months";
|
|
|
|
}
|
|
|
|
|
|
|
|
// days
|
|
|
|
timeInterval = absDifference.inSeconds / 86400;
|
|
|
|
if (timeInterval >= 10.0) {
|
|
|
|
return inPast ? "${timeInterval.truncate()} days ago" : "in ${timeInterval.truncate()} days";
|
|
|
|
} else if (timeInterval >= 1.0) {
|
|
|
|
return inPast ? "${f1.format(timeInterval)} days ago" : "in ${f1.format(timeInterval)} days";
|
|
|
|
}
|
|
|
|
|
|
|
|
// hours
|
|
|
|
timeInterval = absDifference.inSeconds / 3600;
|
|
|
|
if (timeInterval >= 10.0) {
|
|
|
|
return inPast ? "${timeInterval.truncate()} hours ago" : "in ${timeInterval.truncate()} hours";
|
|
|
|
} else if (timeInterval >= 1.0) {
|
|
|
|
return inPast ? "${f1.format(timeInterval)} hours ago" : "in ${f1.format(timeInterval)} hours";
|
|
|
|
}
|
|
|
|
|
|
|
|
// minutes
|
|
|
|
timeInterval = absDifference.inSeconds / 60;
|
|
|
|
if (timeInterval >= 10.0) {
|
|
|
|
return inPast ? "${timeInterval.truncate()} minutes ago" : "in ${timeInterval.truncate()} minutes";
|
|
|
|
} else if (timeInterval >= 1.0) {
|
|
|
|
return inPast ? "${f1.format(timeInterval)} minutes ago" : "in ${f1.format(timeInterval)} minutes";
|
|
|
|
}
|
|
|
|
|
|
|
|
// seconds
|
|
|
|
timeInterval = absDifference.inMilliseconds / 1000;
|
|
|
|
if (timeInterval >= 10.0) {
|
|
|
|
return inPast ? "${timeInterval.truncate()} seconds ago" : "in ${timeInterval.truncate()} seconds";
|
|
|
|
} else {
|
|
|
|
return inPast ? "${f1.format(timeInterval)} seconds ago" : "in ${f1.format(timeInterval)} seconds";
|
|
|
|
}
|
2024-06-14 20:47:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Takes number of [microseconds] and returns readable 40 lines time
|
|
|
|
String get40lTime(int microseconds){
|
|
|
|
return microseconds > 60000000 ? "${(microseconds/1000000/60).floor()}:${(secs.format(microseconds /1000000 % 60))}" : _timeInSec.format(microseconds / 1000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Readable [a] - [b], without sign
|
|
|
|
String readableTimeDifference(Duration a, Duration b){
|
|
|
|
Duration result = a - b;
|
|
|
|
|
|
|
|
return NumberFormat("0.000s;0.000s", LocaleSettings.currentLocale.languageCode).format(result.inMilliseconds/1000);
|
2024-06-11 16:30:13 +00:00
|
|
|
}
|