Adds a time utility for keeping track of time
This commit is contained in:
parent
302595d45d
commit
6ba00044af
2 changed files with 123 additions and 1 deletions
122
lib/utils/TimeUtils.dart
Normal file
122
lib/utils/TimeUtils.dart
Normal file
|
@ -0,0 +1,122 @@
|
|||
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;
|
||||
autofix();
|
||||
}
|
||||
|
||||
void autofix() {
|
||||
int totalSeconds = getTotalSeconds();
|
||||
|
||||
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.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);
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
name: libac_flutter
|
||||
description: "Aria's Creations code library"
|
||||
version: 1.0.17
|
||||
version: 1.0.18
|
||||
homepage: "https://zontreck.com"
|
||||
|
||||
environment:
|
||||
|
|
Loading…
Reference in a new issue