Better time formatting

This commit is contained in:
N00MKRAD 2023-12-21 18:15:14 +01:00
parent 829b37ca6a
commit 89d5cc08a8
1 changed files with 9 additions and 11 deletions

View File

@ -27,28 +27,26 @@ namespace Flowframes.MiscUtils
}
}
public static string Time(long milliseconds)
public static string Time(long milliseconds, bool allowMs = true)
{
double secs = (milliseconds / 1000f);
if (milliseconds <= 1000)
{
return milliseconds + "ms";
}
return secs.ToString("0.00") + "s";
return Time(TimeSpan.FromMilliseconds(milliseconds), allowMs);
}
public static string Time (TimeSpan span, bool allowMs = true)
public static string Time(TimeSpan span, bool allowMs = true)
{
if(span.TotalHours >= 1f)
if (span.TotalHours >= 1f)
return span.ToString(@"hh\:mm\:ss");
if (span.TotalMinutes >= 1f)
return span.ToString(@"mm\:ss");
if (span.TotalSeconds >= 1f || !allowMs)
return span.ToString(@"ss".TrimStart('0').PadLeft(1, '0')) + "s";
{
string format = span.TotalSeconds < 10f ? @"%s\.f" : @"%s";
return span.ToString(format) + "s";
}
return span.ToString(@"fff").TrimStart('0').PadLeft(1, '0') + "ms";
return span.ToString(@"fff") + "ms";
}
public static string TimeSw(Stopwatch sw)