142 lines
2.7 KiB
Dart
142 lines
2.7 KiB
Dart
import 'package:libac_flutter/nbt/Stream.dart';
|
|
|
|
class Time {
|
|
int hours;
|
|
int minutes;
|
|
int seconds;
|
|
|
|
Time({required this.hours, required this.minutes, required this.seconds}) {
|
|
autofix();
|
|
}
|
|
|
|
int getTotalSeconds() {
|
|
int current = 0;
|
|
current += seconds;
|
|
|
|
current += (minutes * 60); // 60 seconds in a minute
|
|
|
|
current += (hours * 60 * 60); // 60 seconds, 60 minutes in an hour
|
|
|
|
return current;
|
|
}
|
|
|
|
void add(Time time) {
|
|
seconds += time.getTotalSeconds();
|
|
autofix();
|
|
}
|
|
|
|
void subtract(Time time) {
|
|
int sec = getTotalSeconds();
|
|
sec -= time.getTotalSeconds();
|
|
|
|
apply(sec);
|
|
}
|
|
|
|
void tickDown() {
|
|
int sec = getTotalSeconds();
|
|
sec--;
|
|
|
|
apply(sec);
|
|
}
|
|
|
|
void tickUp() {
|
|
int sec = getTotalSeconds();
|
|
sec++;
|
|
|
|
apply(sec);
|
|
}
|
|
|
|
void apply(int seconds) {
|
|
hours = 0;
|
|
minutes = 0;
|
|
this.seconds = seconds;
|
|
if (this.seconds < 0) this.seconds = 0;
|
|
|
|
autofix();
|
|
}
|
|
|
|
void autofix() {
|
|
int totalSeconds = getTotalSeconds();
|
|
if (totalSeconds < 0) totalSeconds = 0;
|
|
|
|
int one_hour = (1 * 60 * 60);
|
|
int one_minute = (1 * 60);
|
|
|
|
int hours = (totalSeconds / 60 / 60).round();
|
|
totalSeconds -= (hours * one_hour);
|
|
|
|
int minutes = (totalSeconds / 60).round();
|
|
totalSeconds -= (minutes * one_minute);
|
|
|
|
int seconds = totalSeconds;
|
|
|
|
this.hours = hours;
|
|
this.minutes = minutes;
|
|
this.seconds = seconds;
|
|
}
|
|
|
|
factory Time.copy(Time other) {
|
|
return Time(
|
|
hours: other.hours, minutes: other.minutes, seconds: other.seconds);
|
|
}
|
|
|
|
factory Time.fromNotation(String notation) {
|
|
int hours = 0;
|
|
int minutes = 0;
|
|
int seconds = 0;
|
|
|
|
List<String> current = [];
|
|
String val = notation;
|
|
if (val.indexOf('h') == -1) {
|
|
hours = 0;
|
|
} else {
|
|
current = val.split('h');
|
|
hours = int.parse(current[0]);
|
|
|
|
if (current.length == 2)
|
|
val = current[1];
|
|
else
|
|
val = "";
|
|
}
|
|
|
|
if (val.indexOf('m') == -1) {
|
|
minutes = 0;
|
|
} else {
|
|
current = val.split('m');
|
|
minutes = int.parse(current[0]);
|
|
|
|
if (current.length == 2)
|
|
val = current[1];
|
|
else
|
|
val = "";
|
|
}
|
|
|
|
if (val.indexOf('s') == -1) {
|
|
seconds = 0;
|
|
} else {
|
|
current = val.split('s');
|
|
seconds = int.parse(current[0]);
|
|
|
|
if (current.length == 2)
|
|
val = current[1];
|
|
else
|
|
val = "";
|
|
}
|
|
|
|
if (val != "") {
|
|
seconds += int.parse(val);
|
|
}
|
|
|
|
return Time(hours: hours, minutes: minutes, seconds: seconds);
|
|
}
|
|
|
|
@override
|
|
String toString() {
|
|
StringBuilder builder = StringBuilder();
|
|
if (hours > 0) builder.append("${hours}h");
|
|
if (minutes > 0) builder.append("${minutes}m");
|
|
if (seconds > 0) builder.append("${seconds}s");
|
|
|
|
return "${builder}";
|
|
}
|
|
}
|