Adds some more functionality to the TimeUtils class.
This commit is contained in:
parent
05cee1da36
commit
c25d0174ea
3 changed files with 66 additions and 2 deletions
|
@ -1,3 +1,3 @@
|
||||||
class Constants {
|
class Constants {
|
||||||
static const VERSION = "1.2.082924+1846";
|
static const VERSION = "1.2.090324+0325";
|
||||||
}
|
}
|
||||||
|
|
|
@ -144,3 +144,67 @@ class Time {
|
||||||
return "${builder}";
|
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<void> _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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
name: libac_dart
|
name: libac_dart
|
||||||
description: "Aria's Creations code library"
|
description: "Aria's Creations code library"
|
||||||
version: 1.2.082924+1846
|
version: 1.2.090324+0325
|
||||||
homepage: "https://zontreck.com"
|
homepage: "https://zontreck.com"
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue