RosettaCodeData/Task/Convert-seconds-to-compound-duration/C-sharp/convert-seconds-to-compound-duration-2.cs
2023-07-01 13:44:08 -04:00

14 lines
543 B
C#

private static string ConvertToCompoundDuration(int seconds)
{
if (seconds < 0) throw new ArgumentOutOfRangeException(nameof(seconds));
if (seconds == 0) return "0 sec";
TimeSpan span = TimeSpan.FromSeconds(seconds);
int[] parts = {span.Days / 7, span.Days % 7, span.Hours, span.Minutes, span.Seconds};
string[] units = {" wk", " d", " hr", " min", " sec"};
return string.Join(", ",
from index in Enumerable.Range(0, units.Length)
where parts[index] > 0
select parts[index] + units[index]);
}