RoomService v2.3.1
Loading...
Searching...
No Matches
RoomService.SecondsToTime Class Reference

Lua function to convert seconds into a time string.

Member Function Documentation

◆ Implementation()

string RoomService.SecondsToTime.Implementation ( float time,
int precision )
inlineprivate

Usage:

Lua function to convert seconds into a time string.
Definition RoomServiceLuaFunctions.cs:1060
Definition MyPluginInfo.cs:2
Parameters
timeThe time in seconds.
precisionThe amount of decimals.
Returns
A time string in the format hour:minutes:seconds.milliseconds Example 00:42.133
Only returns hour if the time is more than 59 minutes.
Return values
string

Source Code

1081 {
1082 if (!RoomServiceUtils.IsOnlineHost())
1083 {
1084 return "";
1085 }
1086
1087 // Ensure precision is not negative
1088 if (precision < 0)
1089 {
1090 precision = 0;
1091 }
1092
1093 if (float.IsNegativeInfinity(time) || float.IsPositiveInfinity(time) || float.IsNaN(time))
1094 time = 0.0f;
1095 if (time < 0.0f)
1096 time = 0.0f;
1097
1098 TimeSpan timeSpan = time != 0.0f ? TimeSpan.FromSeconds(time) : TimeSpan.Zero;
1099
1100 // Calculate fractional seconds and format milliseconds with precision
1101 double fractionalSeconds = time - Math.Floor(time);
1102 // If it rounds to 1000, reset it to 0 to avoid overflow
1103 if (Math.Round(fractionalSeconds * Math.Pow(10, precision)) >= Math.Pow(10, precision))
1104 fractionalSeconds = 0;
1105 string milliseconds = Math.Round(fractionalSeconds * Math.Pow(10, precision))
1106 .ToString(CultureInfo.InvariantCulture).PadLeft(precision, '0');
1107
1108 if (time < 3600.0f)
1109 return string.Format(CultureInfo.InvariantCulture, "{0:D2}:{1:D2}.{2}",
1110 timeSpan.Minutes, timeSpan.Seconds, milliseconds);
1111 else
1112 return string.Format(CultureInfo.InvariantCulture, "{0:D2}:{1:D2}:{2:D2}.{3}",
1113 timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds, milliseconds);
1114 }