diff --git a/lib/consts.dart b/lib/consts.dart index 0c2066c..4d3223e 100644 --- a/lib/consts.dart +++ b/lib/consts.dart @@ -1,3 +1,3 @@ class Constants { - static const VERSION = "1.2.082924+1846"; + static const VERSION = "1.2.090324+0325"; } diff --git a/lib/utils/TimeUtils.dart b/lib/utils/TimeUtils.dart index 5b715b2..2fac648 100644 --- a/lib/utils/TimeUtils.dart +++ b/lib/utils/TimeUtils.dart @@ -144,3 +144,67 @@ class Time { return "${builder}"; } } + +/// Provides some functionality dart lacks internally. +/// +/// WARNING: A potential race condition can be met by using the nano time here. +/// +/// Long run times may eventually reach a maximum possible value. To prevent this, code should make use of the resetNanoTime function. +class TimeUtils { + /// This locks the internal timer and stops execution + bool _tslock = false; + int _ns = 0; + + TimeUtils._(); + static TimeUtils? _inst; + + factory TimeUtils() { + _inst ??= TimeUtils._(); + return _inst!; + } + + /// Returns a unix timestamp + static int getUnixTimestamp() { + return (DateTime.now().millisecondsSinceEpoch / 1000).round(); + } + + /// Converts a unix timestamp back into DateTime format. + static DateTime parseTimestamp(int unixTimestamp) { + return DateTime.fromMillisecondsSinceEpoch(unixTimestamp * 1000); + } + + /// Starts the time tracker. + static void initTimeTracker() { + TimeUtils tu = TimeUtils(); + tu._tslock = false; + tu._timeLoop(); + } + + /// Stops the time tracker + static void stopTimeTracker() { + TimeUtils tu = TimeUtils(); + tu._tslock = true; + } + + /// Resets the currently tracked nanoTime. + static void resetNanoTime() { + TimeUtils tu = TimeUtils(); + tu._ns = 0; + } + + /// This is the loop which will keep a facsimile of nanoseconds since start. + Future _timeLoop() async { + _ns = 0; + while (!_tslock) { + await Future.delayed( + Duration(microseconds: 1)); // Simulate 1 microsecond delay + _ns += 1000; // Increment by 1000 nanoseconds (1 microsecond) + } + } + + /// Returns the current simulated nanotime. + static int getNanoTime() { + TimeUtils tu = TimeUtils(); + return tu._ns; + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 755ffaf..9778621 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: libac_dart description: "Aria's Creations code library" -version: 1.2.082924+1846 +version: 1.2.090324+0325 homepage: "https://zontreck.com"