Core/Server: improve timestamp format output for large time values

Port From (https://github.com/TrinityCore/TrinityCore/commit/8c6974bc9f1de4b5fc1e1002d5797dcff62b3cc8)
This commit is contained in:
hondacrx
2022-01-07 11:00:30 -05:00
parent 131e2c7815
commit 6dde0557c6
+66 -45
View File
@@ -148,77 +148,98 @@ public static class Time
ulong hours = timeInSecs % Day / Hour; ulong hours = timeInSecs % Day / Hour;
ulong days = timeInSecs / Day; ulong days = timeInSecs / Day;
if (timeFormat == TimeFormat.Numeric)
{
if (days != 0)
return $"{days}:{hours}:{minutes}:{secs:2}";
else if (hours != 0)
return $"{hours}:{minutes}:{secs:2}";
else if (minutes != 0)
return $"{minutes}:{secs:2}";
else
return $"0:{secs:2}";
}
StringBuilder ss = new(); StringBuilder ss = new();
if (days != 0) if (days != 0)
{ {
ss.Append(days); ss.Append(days);
if (timeFormat == TimeFormat.Numeric) switch (timeFormat)
ss.Append(":");
else if (timeFormat == TimeFormat.ShortText)
ss.Append("d");
else // if (timeFormat == TimeFormat::FullText)
{ {
if (days == 1) case TimeFormat.ShortText:
ss.Append(" Day "); ss.Append("d");
else break;
ss.Append(" Days "); case TimeFormat.FullText:
if (days == 1)
ss.Append(" Day ");
else
ss.Append(" Days ");
break;
default:
return "<Unknown time format>";
} }
} }
if (hours != 0 || hoursOnly) if (hours != 0 || hoursOnly)
{ {
ss.Append(hours); ss.Append(hours);
if (timeFormat == TimeFormat.Numeric) switch (timeFormat)
ss.Append(":");
else if (timeFormat == TimeFormat.ShortText)
ss.Append("h");
else // if (timeFormat == TimeFormat::FullText)
{ {
if (hours <= 1)
ss.Append(" Hour "); case TimeFormat.ShortText:
else ss.Append("h");
ss.Append(" Hours "); break;
case TimeFormat.FullText:
if (hours <= 1)
ss.Append(" Hour ");
else
ss.Append(" Hours ");
break;
default:
return "<Unknown time format>";
} }
} }
if (!hoursOnly) if (!hoursOnly)
{ {
if (minutes != 0) if (minutes != 0)
{ {
ss.Append(minutes); ss.Append(minutes);
if (timeFormat == TimeFormat.Numeric) switch (timeFormat)
ss.Append(":");
else if (timeFormat == TimeFormat.ShortText)
ss.Append("m");
else // if (timeFormat == TimeFormat::FullText)
{ {
if (minutes == 1) case TimeFormat.ShortText:
ss.Append(" Minute "); ss.Append("m");
else break;
ss.Append(" Minutes "); case TimeFormat.FullText:
if (minutes == 1)
ss.Append(" Minute ");
else
ss.Append(" Minutes ");
break;
default:
return "<Unknown time format>";
} }
} }
else
{
if (timeFormat == TimeFormat.Numeric)
ss.Append("0:");
}
if (secs != 0 || (days == 0 && hours == 0 && minutes == 0)) if (secs != 0 || (days == 0 && hours == 0 && minutes == 0))
{ {
ss.Append($"{secs:2}"); ss.Append(secs);
if (timeFormat == TimeFormat.ShortText) switch (timeFormat)
ss.Append("s");
else if (timeFormat == TimeFormat.FullText)
{ {
if (secs <= 1) case TimeFormat.ShortText:
ss.Append(" Second."); ss.Append("s");
else break;
ss.Append(" Seconds."); case TimeFormat.FullText:
if (secs <= 1)
ss.Append(" Second.");
else
ss.Append(" Seconds.");
break;
default:
return "<Unknown time format>";
} }
} }
else
{
if (timeFormat == TimeFormat.Numeric)
ss.Append("00");
}
} }
return ss.ToString(); return ss.ToString();