-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDurationFormat.cs
More file actions
42 lines (36 loc) · 1.54 KB
/
Copy pathDurationFormat.cs
File metadata and controls
42 lines (36 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System.Globalization;
namespace RustPlusBot.Features.Commands.Formatting;
/// <summary>Formats durations compactly for in-game replies.</summary>
internal static class DurationFormat
{
/// <summary>Renders a duration as "Xd Yh" / "Yh Zm" / "Zm".</summary>
/// <param name="span">The duration to render.</param>
/// <returns>A compact duration string.</returns>
public static string Compact(TimeSpan span)
{
if (span.TotalDays >= 1)
{
return string.Create(CultureInfo.InvariantCulture, $"{(int)span.TotalDays}d {span.Hours}h");
}
if (span.TotalHours >= 1)
{
return string.Create(CultureInfo.InvariantCulture, $"{(int)span.TotalHours}h {span.Minutes}m");
}
return string.Create(CultureInfo.InvariantCulture, $"{(int)span.TotalMinutes}m");
}
/// <summary>Renders a sub-minute-aware duration: "<n>s" under a minute, else "<m>m <s>s".</summary>
/// <param name="seconds">The duration in seconds.</param>
/// <returns>A compact duration string.</returns>
public static string Seconds(double seconds)
{
if (seconds < 60)
{
return string.Create(CultureInfo.InvariantCulture, $"{seconds:0.#}s");
}
var span = TimeSpan.FromSeconds(seconds);
var minutes = (int)span.TotalMinutes;
return span.Seconds == 0
? string.Create(CultureInfo.InvariantCulture, $"{minutes}m")
: string.Create(CultureInfo.InvariantCulture, $"{minutes}m {span.Seconds}s");
}
}