feat!: Add MustIncludeMinutes & MustIncludeSeconds
This commit is contained in:
parent
bc8eb1ac0d
commit
3af891f809
3 changed files with 48 additions and 29 deletions
|
|
@ -7,7 +7,7 @@ using System.Threading.Tasks;
|
||||||
namespace Xorog.UniversalExtensions.Enums;
|
namespace Xorog.UniversalExtensions.Enums;
|
||||||
public enum TimeFormat
|
public enum TimeFormat
|
||||||
{
|
{
|
||||||
MINUTES,
|
Minutes = 0,
|
||||||
HOURS,
|
Hours = 1,
|
||||||
DAYS
|
Days = 2
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,7 @@ public static class TimeExtensions
|
||||||
/// <param name="seconds"></param>
|
/// <param name="seconds"></param>
|
||||||
/// <param name="timeFormat"></param>
|
/// <param name="timeFormat"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string GetShortHumanReadable(this int seconds, TimeFormat timeFormat = TimeFormat.DAYS) =>
|
public static string GetShortHumanReadable(this int seconds, TimeFormat timeFormat = TimeFormat.Days) =>
|
||||||
TimeSpan.FromSeconds(seconds).GetShortTimeFormat(timeFormat);
|
TimeSpan.FromSeconds(seconds).GetShortTimeFormat(timeFormat);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -81,7 +81,7 @@ public static class TimeExtensions
|
||||||
/// <param name="seconds"></param>
|
/// <param name="seconds"></param>
|
||||||
/// <param name="timeFormat"></param>
|
/// <param name="timeFormat"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string GetShortHumanReadable(this long seconds, TimeFormat timeFormat = TimeFormat.DAYS) =>
|
public static string GetShortHumanReadable(this long seconds, TimeFormat timeFormat = TimeFormat.Days) =>
|
||||||
TimeSpan.FromSeconds(seconds).GetShortTimeFormat(timeFormat);
|
TimeSpan.FromSeconds(seconds).GetShortTimeFormat(timeFormat);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -90,7 +90,7 @@ public static class TimeExtensions
|
||||||
/// <param name="timespan"></param>
|
/// <param name="timespan"></param>
|
||||||
/// <param name="timeFormat"></param>
|
/// <param name="timeFormat"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string GetShortHumanReadable(this TimeSpan timespan, TimeFormat timeFormat = TimeFormat.DAYS) =>
|
public static string GetShortHumanReadable(this TimeSpan timespan, TimeFormat timeFormat = TimeFormat.Days) =>
|
||||||
timespan.GetShortTimeFormat(timeFormat);
|
timespan.GetShortTimeFormat(timeFormat);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -99,14 +99,14 @@ public static class TimeExtensions
|
||||||
/// <param name="seconds"></param>
|
/// <param name="seconds"></param>
|
||||||
/// <param name="timeFormat"></param>
|
/// <param name="timeFormat"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string GetHumanReadable(this int seconds, TimeFormat timeFormat = TimeFormat.DAYS, HumanReadableTimeFormatConfig? config = null) =>
|
public static string GetHumanReadable(this int seconds, TimeFormat timeFormat = TimeFormat.Days, HumanReadableTimeFormatConfig? config = null) =>
|
||||||
TimeSpan.FromSeconds(seconds).GetTimeFormat(timeFormat, config);
|
TimeSpan.FromSeconds(seconds).GetTimeFormat(timeFormat, config);
|
||||||
|
|
||||||
/// <inheritdoc cref="UniversalExtensions.GetHumanReadable(int, TimeFormat)"/>
|
/// <inheritdoc cref="UniversalExtensions.GetHumanReadable(int, TimeFormat)"/>
|
||||||
public static string GetHumanReadable(this long seconds, TimeFormat timeFormat = TimeFormat.DAYS, HumanReadableTimeFormatConfig? config = null) =>
|
public static string GetHumanReadable(this long seconds, TimeFormat timeFormat = TimeFormat.Days, HumanReadableTimeFormatConfig? config = null) =>
|
||||||
TimeSpan.FromSeconds(seconds).GetTimeFormat(timeFormat, config);
|
TimeSpan.FromSeconds(seconds).GetTimeFormat(timeFormat, config);
|
||||||
|
|
||||||
/// <inheritdoc cref="UniversalExtensions.GetHumanReadable(int, TimeFormat)"/>
|
/// <inheritdoc cref="UniversalExtensions.GetHumanReadable(int, TimeFormat)"/>
|
||||||
public static string GetHumanReadable(this TimeSpan timeSpan, TimeFormat timeFormat = TimeFormat.DAYS, HumanReadableTimeFormatConfig? config = null) =>
|
public static string GetHumanReadable(this TimeSpan timeSpan, TimeFormat timeFormat = TimeFormat.Days, HumanReadableTimeFormatConfig? config = null) =>
|
||||||
timeSpan.GetTimeFormat(timeFormat, config);
|
timeSpan.GetTimeFormat(timeFormat, config);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ internal static class Internal
|
||||||
{
|
{
|
||||||
switch (timeFormat)
|
switch (timeFormat)
|
||||||
{
|
{
|
||||||
case TimeFormat.DAYS:
|
case TimeFormat.Days:
|
||||||
if (_timespan.TotalDays >= 1)
|
if (_timespan.TotalDays >= 1)
|
||||||
return $"{Math.Floor(_timespan.TotalDays).ToString().PadLeft(2, '0')}:{_timespan.Hours.ToString().PadLeft(2, '0')}:{_timespan.Minutes.ToString().PadLeft(2, '0')}:{_timespan.Seconds.ToString().PadLeft(2, '0')}";
|
return $"{Math.Floor(_timespan.TotalDays).ToString().PadLeft(2, '0')}:{_timespan.Hours.ToString().PadLeft(2, '0')}:{_timespan.Minutes.ToString().PadLeft(2, '0')}:{_timespan.Seconds.ToString().PadLeft(2, '0')}";
|
||||||
|
|
||||||
|
|
@ -17,7 +17,7 @@ internal static class Internal
|
||||||
|
|
||||||
return $"{_timespan.Minutes.ToString().PadLeft(2, '0')}:{_timespan.Seconds.ToString().PadLeft(2, '0')}";
|
return $"{_timespan.Minutes.ToString().PadLeft(2, '0')}:{_timespan.Seconds.ToString().PadLeft(2, '0')}";
|
||||||
|
|
||||||
case TimeFormat.HOURS:
|
case TimeFormat.Hours:
|
||||||
if (_timespan.TotalDays >= 1)
|
if (_timespan.TotalDays >= 1)
|
||||||
return $"{Math.Floor(_timespan.TotalHours).ToString().PadLeft(2, '0')}:" +
|
return $"{Math.Floor(_timespan.TotalHours).ToString().PadLeft(2, '0')}:" +
|
||||||
$"{_timespan.Minutes.ToString().PadLeft(2, '0')}:{_timespan.Seconds.ToString().PadLeft(2, '0')}";
|
$"{_timespan.Minutes.ToString().PadLeft(2, '0')}:{_timespan.Seconds.ToString().PadLeft(2, '0')}";
|
||||||
|
|
@ -28,7 +28,7 @@ internal static class Internal
|
||||||
|
|
||||||
return $"{_timespan.Minutes.ToString().PadLeft(2, '0')}:{_timespan.Seconds.ToString().PadLeft(2, '0')}";
|
return $"{_timespan.Minutes.ToString().PadLeft(2, '0')}:{_timespan.Seconds.ToString().PadLeft(2, '0')}";
|
||||||
|
|
||||||
case TimeFormat.MINUTES:
|
case TimeFormat.Minutes:
|
||||||
if (_timespan.TotalHours >= 1)
|
if (_timespan.TotalHours >= 1)
|
||||||
return $"{Math.Floor(_timespan.TotalMinutes).ToString().PadLeft(2, '0')}:{_timespan.Seconds.ToString().PadLeft(2, '0')}";
|
return $"{Math.Floor(_timespan.TotalMinutes).ToString().PadLeft(2, '0')}:{_timespan.Seconds.ToString().PadLeft(2, '0')}";
|
||||||
|
|
||||||
|
|
@ -42,30 +42,37 @@ internal static class Internal
|
||||||
{
|
{
|
||||||
config ??= new();
|
config ??= new();
|
||||||
|
|
||||||
|
string returningString = string.Empty;
|
||||||
|
Dictionary<string, bool> humanReadable = new();
|
||||||
|
|
||||||
switch (timeFormat)
|
switch (timeFormat)
|
||||||
{
|
{
|
||||||
case TimeFormat.DAYS:
|
case TimeFormat.Days:
|
||||||
if (_timespan.TotalDays >= 1)
|
{
|
||||||
return $"{Math.Floor(_timespan.TotalDays)} {config.DaysString}, {_timespan.Hours} {config.HoursString}";
|
humanReadable.Add($"{Math.Floor(_timespan.TotalDays)} {config.DaysString}", _timespan.TotalDays >= 1);
|
||||||
|
humanReadable.Add($"{_timespan.Hours} {config.HoursString}", _timespan.TotalHours >= 1);
|
||||||
|
humanReadable.Add($"{_timespan.Minutes} {config.MinutesString}", (config.MustIncludeMinutes && _timespan.TotalMinutes >= 1) || (_timespan.TotalMinutes >= 1 && _timespan.TotalDays < 1));
|
||||||
|
humanReadable.Add($"{_timespan.Seconds} {config.SecondsString}", config.MustIncludeSeconds || _timespan.TotalHours < 1);
|
||||||
|
|
||||||
if (_timespan.TotalHours >= 1)
|
return string.Join(", ", humanReadable.Where(x => x.Value).Select(x => x.Key));
|
||||||
return $"{_timespan.Hours} {config.HoursString}, {_timespan.Minutes} {config.MinutesString}";
|
}
|
||||||
|
|
||||||
return $"{_timespan.Minutes} {config.MinutesString}, {_timespan.Seconds} {config.SecondsString}";
|
case TimeFormat.Hours:
|
||||||
|
{
|
||||||
|
humanReadable.Add($"{Math.Floor(_timespan.TotalHours)} {config.HoursString}", _timespan.TotalHours >= 1);
|
||||||
|
humanReadable.Add($"{_timespan.Minutes} {config.MinutesString}", (config.MustIncludeMinutes && _timespan.TotalMinutes >= 1) || _timespan.TotalHours >= 1);
|
||||||
|
humanReadable.Add($"{_timespan.Seconds} {config.SecondsString}", config.MustIncludeSeconds || _timespan.TotalHours < 1);
|
||||||
|
|
||||||
case TimeFormat.HOURS:
|
return string.Join(", ", humanReadable.Where(x => x.Value).Select(x => x.Key));
|
||||||
if (_timespan.TotalDays >= 1)
|
}
|
||||||
return $"{Math.Floor(_timespan.TotalHours)} {config.HoursString}, {_timespan.Minutes} {config.MinutesString}";
|
|
||||||
|
|
||||||
if (_timespan.TotalHours >= 1)
|
case TimeFormat.Minutes:
|
||||||
return $"{_timespan.Hours} {config.HoursString}, {_timespan.Minutes} {config.MinutesString}";
|
{
|
||||||
|
humanReadable.Add($"{Math.Floor(_timespan.TotalMinutes)} {config.MinutesString}", (config.MustIncludeMinutes && _timespan.TotalMinutes >= 1) || _timespan.TotalMinutes >= 1);
|
||||||
|
humanReadable.Add($"{_timespan.Seconds} {config.SecondsString}", true);
|
||||||
|
|
||||||
return $"{_timespan.Minutes} {config.MinutesString}, {_timespan.Seconds} {config.SecondsString}";
|
return string.Join(", ", humanReadable.Where(x => x.Value).Select(x => x.Key));
|
||||||
|
}
|
||||||
case TimeFormat.MINUTES:
|
|
||||||
if (_timespan.TotalHours >= 1)
|
|
||||||
return $"{Math.Floor(_timespan.TotalMinutes)} {config.MinutesString}, {_timespan.Seconds} {config.SecondsString}";
|
|
||||||
return $"{_timespan.Minutes} {config.MinutesString}, {_timespan.Seconds} {config.MinutesString}";
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return _timespan.ToString();
|
return _timespan.ToString();
|
||||||
|
|
@ -111,4 +118,16 @@ public class HumanReadableTimeFormatConfig
|
||||||
/// Defaults to: "seconds".
|
/// Defaults to: "seconds".
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string SecondsString { get; set; } = "seconds";
|
public string SecondsString { get; set; } = "seconds";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Must include minutes if timestamp is >= 1 day.
|
||||||
|
/// Defaults to: false.
|
||||||
|
/// </summary>
|
||||||
|
public bool MustIncludeMinutes { get; set; } = false;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Must include seconds if timestamp is >= 1 hour.
|
||||||
|
/// Defaults to: false.
|
||||||
|
/// </summary>
|
||||||
|
public bool MustIncludeSeconds { get; set; } = false;
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue