nicer display of elapsed time

This commit is contained in:
in0finite 2022-04-22 23:24:25 +02:00
parent 38449b1a7e
commit ca44775812
5 changed files with 12 additions and 35 deletions

View file

@ -71,7 +71,7 @@ namespace SanAndreasUnity.Commands
}
else if (words[0] == "uptime")
{
response += F.FormatElapsedTime(Time.realtimeSinceStartup);
response += F.FormatElapsedTime(Time.realtimeSinceStartupAsDouble);
}
else if (words[0] == "players")
{

View file

@ -229,7 +229,7 @@ namespace SanAndreasUnity.Stats
private void AddTimeSpan(System.Text.StringBuilder sb, string text, double seconds)
{
AddNesting(sb);
sb.AppendLine($"{text}: {TimeSpan.FromSeconds(seconds)}");
sb.AppendLine($"{text}: {F.FormatElapsedTime(seconds)}");
}
private void AddAsMs(System.Text.StringBuilder sb, string text, double seconds)

View file

@ -4,6 +4,7 @@ using Mirror;
using SanAndreasUnity.Behaviours.Peds;
using SanAndreasUnity.Net;
using System;
using SanAndreasUnity.Utilities;
namespace SanAndreasUnity.Stats
{
@ -46,7 +47,7 @@ namespace SanAndreasUnity.Stats
private static void AddTimeSpan(System.Text.StringBuilder sb, string text, double seconds)
{
sb.AppendLine($"{text}: {TimeSpan.FromSeconds(seconds)}");
sb.AppendLine($"{text}: {F.FormatElapsedTime(seconds)}");
}
private static void AddAsMs(System.Text.StringBuilder sb, string text, double seconds)

View file

@ -44,7 +44,7 @@ namespace SanAndreasUnity.Utilities
double percLeft = 1.0 - newProgressPerc;
double secondsLeft = percLeft / percPerSecond;
this.ETA = F.FormatElapsedTime((float)secondsLeft);
this.ETA = F.FormatElapsedTime(secondsLeft);
m_lastProgressPerc = newProgressPerc;
}

View file

@ -120,38 +120,14 @@ namespace SanAndreasUnity.Utilities
}
/// <summary>
/// Formats the elapsed time (in seconds) in format hh:mm:ss, but removes the unnecessary prefix values which are zero.
/// For example, 40 seconds will just return 40, 70 will return 01:10, 3700 will return 01:01:40.
/// Formats the elapsed time (in seconds) in format [d].[hh]:mm:ss.[fff].
/// For example, 40 seconds will return 00:40, 70 will return 01:10, 3700 will return 01:01:40.
/// </summary>
public static string FormatElapsedTime( float elapsedTime ) {
int elapsedTimeInteger = Mathf.CeilToInt (elapsedTime);
int hours = elapsedTimeInteger / 3600;
int minutes = elapsedTimeInteger % 3600 / 60;
int seconds = elapsedTimeInteger % 3600 % 60 ;
var sb = new System.Text.StringBuilder (10);
if (hours > 0) {
if (hours < 10)
sb.Append ("0");
sb.Append (hours);
sb.Append (":");
}
if (hours > 0 || minutes > 0) {
if (minutes < 10)
sb.Append ("0");
sb.Append (minutes);
sb.Append (":");
}
if (seconds < 10)
sb.Append ("0");
sb.Append (seconds);
return sb.ToString ();
public static string FormatElapsedTime(double elapsedTimeSeconds, bool useMilliseconds = false)
{
TimeSpan timeSpan = TimeSpan.FromSeconds(elapsedTimeSeconds);
string format = (timeSpan.Days > 0 ? "d\\." : "") + (timeSpan.Hours > 0 ? "hh\\:" : "") + "mm\\:ss" + (useMilliseconds ? "\\.fff" : "");
return timeSpan.ToString(format, CultureInfo.InvariantCulture);
}
public static string Nl2Br(this string str)