From 88afaae298dede4c87561ef83605d5a881d11ac6 Mon Sep 17 00:00:00 2001 From: zontreck Date: Sat, 24 May 2025 23:31:50 -0700 Subject: [PATCH] Add feature: days to Time API --- lib/utils/TimeUtils.dart | 63 +++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/lib/utils/TimeUtils.dart b/lib/utils/TimeUtils.dart index 2fac648..f5cc233 100644 --- a/lib/utils/TimeUtils.dart +++ b/lib/utils/TimeUtils.dart @@ -1,14 +1,35 @@ -import '../nbt/Stream.dart'; +/// [Time] is used as a way to serialize and deserialize time based notations. +/// +/// When interacting with this, you can even parse Durations class Time { + int days; int hours; int minutes; int seconds; - Time({required this.hours, required this.minutes, required this.seconds}) { + Time( + {required this.days, + required this.hours, + required this.minutes, + required this.seconds}) { autofix(); } + static Time fromDuration(Duration duration) { + int days = duration.inDays; + int hours = duration.inHours.remainder(24); + int minutes = duration.inMinutes.remainder(60); + int seconds = duration.inSeconds.remainder(60); + + return Time(days: days, hours: hours, minutes: minutes, seconds: seconds); + } + + Duration toDuration() { + return Duration( + days: days, hours: hours, minutes: minutes, seconds: seconds); + } + int getTotalSeconds() { int current = 0; current += seconds; @@ -59,9 +80,13 @@ class Time { int totalSeconds = getTotalSeconds(); if (totalSeconds < 0) totalSeconds = 0; + int one_day = (1 * 60 * 60 * 24); int one_hour = (1 * 60 * 60); int one_minute = (1 * 60); + int days = (totalSeconds / 60 / 60 / 24).round(); + totalSeconds -= (days * one_day); + int hours = (totalSeconds / 60 / 60).round(); totalSeconds -= (hours * one_hour); @@ -70,27 +95,43 @@ class Time { int seconds = totalSeconds; + this.days = days; this.hours = hours; this.minutes = minutes; this.seconds = seconds; } Time copy() { - return Time(hours: hours, minutes: minutes, seconds: seconds); + return Time(days: days, hours: hours, minutes: minutes, seconds: seconds); } factory Time.copy(Time other) { return Time( - hours: other.hours, minutes: other.minutes, seconds: other.seconds); + days: other.days, + hours: other.hours, + minutes: other.minutes, + seconds: other.seconds); } factory Time.fromNotation(String notation) { + int days = 0; int hours = 0; int minutes = 0; int seconds = 0; List current = []; String val = notation; + if (val.indexOf('d') == -1) { + days = 0; + } else { + current = val.split('d'); + days = int.parse(current[0]); + + if (current.length == 2) { + val = current[1]; + } else + val = ""; + } if (val.indexOf('h') == -1) { hours = 0; } else { @@ -131,17 +172,19 @@ class Time { seconds += int.parse(val); } - return Time(hours: hours, minutes: minutes, seconds: seconds); + return Time(days: days, 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"); + List parts = []; - return "${builder}"; + if (days > 0) parts.add('${days}d'); + if (hours > 0) parts.add('${hours}h'); + if (minutes > 0) parts.add('${minutes}m'); + if (seconds > 0) parts.add('${seconds}s'); + + return parts.join(' '); } }