2022-03-07 14:59:11 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
|
|
namespace VDownload.Core.Services
|
|
|
|
|
|
{
|
2022-05-05 15:06:10 +02:00
|
|
|
|
public static class TimeSpanCustomFormat
|
2022-03-07 14:59:11 +01:00
|
|
|
|
{
|
|
|
|
|
|
// (TH:)MM:SS
|
|
|
|
|
|
public static string ToOptTHBaseMMSS(TimeSpan timeSpan, params TimeSpan[] formatBase)
|
|
|
|
|
|
{
|
|
|
|
|
|
string formattedTimeSpan = string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
int maxTHLength = 0;
|
2022-03-09 23:43:51 +01:00
|
|
|
|
foreach (TimeSpan format in formatBase.Concat(new TimeSpan[] { timeSpan }))
|
2022-03-07 14:59:11 +01:00
|
|
|
|
{
|
2022-03-09 23:43:51 +01:00
|
|
|
|
int THLength = Math.Floor(format.TotalHours) > 0 ? Math.Floor(timeSpan.TotalHours).ToString().Length : 0;
|
|
|
|
|
|
if (THLength > maxTHLength) maxTHLength = THLength;
|
2022-03-07 14:59:11 +01:00
|
|
|
|
}
|
2022-03-09 23:43:51 +01:00
|
|
|
|
formattedTimeSpan += $"{((int)Math.Floor(timeSpan.TotalHours)).ToString($"D{maxTHLength}")}:";
|
|
|
|
|
|
|
2022-03-07 14:59:11 +01:00
|
|
|
|
formattedTimeSpan += maxTHLength == 0 ? $"{timeSpan.Minutes}:" : $"{timeSpan.Minutes:00}:";
|
2022-03-09 23:43:51 +01:00
|
|
|
|
|
2022-03-07 14:59:11 +01:00
|
|
|
|
formattedTimeSpan += $"{timeSpan.Seconds:00}";
|
|
|
|
|
|
|
|
|
|
|
|
return formattedTimeSpan;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ((TH:)MM:)SS
|
|
|
|
|
|
public static string ToOptTHMMBaseSS(TimeSpan timeSpan, params TimeSpan[] formatBase)
|
|
|
|
|
|
{
|
|
|
|
|
|
string formattedTimeSpan = string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
int maxTHLength = 0;
|
2022-03-09 23:43:51 +01:00
|
|
|
|
foreach (TimeSpan format in formatBase.Concat(new TimeSpan[] { timeSpan }))
|
2022-03-07 14:59:11 +01:00
|
|
|
|
{
|
2022-03-09 23:43:51 +01:00
|
|
|
|
int THLength = Math.Floor(format.TotalHours) > 0 ? Math.Floor(timeSpan.TotalHours).ToString().Length : 0;
|
|
|
|
|
|
if (THLength > maxTHLength) maxTHLength = THLength;
|
2022-03-07 14:59:11 +01:00
|
|
|
|
}
|
2022-03-09 23:43:51 +01:00
|
|
|
|
formattedTimeSpan += $"{((int)Math.Floor(timeSpan.TotalHours)).ToString($"D{maxTHLength}")}:";
|
|
|
|
|
|
|
2022-03-07 14:59:11 +01:00
|
|
|
|
bool MM = false;
|
2022-03-09 23:43:51 +01:00
|
|
|
|
if (Math.Floor(timeSpan.TotalMinutes) > 0 || maxTHLength > 0)
|
2022-03-07 14:59:11 +01:00
|
|
|
|
{
|
|
|
|
|
|
formattedTimeSpan += maxTHLength > 0 ? $"{timeSpan.Minutes:00}:" : $"{timeSpan.Minutes}:";
|
|
|
|
|
|
MM = true;
|
|
|
|
|
|
}
|
2022-03-09 23:43:51 +01:00
|
|
|
|
|
2022-03-07 14:59:11 +01:00
|
|
|
|
formattedTimeSpan += MM ? $"{timeSpan.Seconds:00}:" : $"{timeSpan.Seconds}:";
|
|
|
|
|
|
|
|
|
|
|
|
return formattedTimeSpan;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|