From 3af891f80935c38f2116884a3efde4b367f143ca Mon Sep 17 00:00:00 2001
From: Mira <56395159+TheXorog@users.noreply.github.com>
Date: Mon, 28 Aug 2023 12:23:21 +0200
Subject: [PATCH] feat!: Add MustIncludeMinutes & MustIncludeSeconds
---
Enums/TimeFormat.cs | 6 ++--
Extensions/TimeExtensions.cs | 12 ++++----
Internal/Internal.cs | 59 ++++++++++++++++++++++++------------
3 files changed, 48 insertions(+), 29 deletions(-)
diff --git a/Enums/TimeFormat.cs b/Enums/TimeFormat.cs
index 832f4be..f5db582 100644
--- a/Enums/TimeFormat.cs
+++ b/Enums/TimeFormat.cs
@@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Xorog.UniversalExtensions.Enums;
public enum TimeFormat
{
- MINUTES,
- HOURS,
- DAYS
+ Minutes = 0,
+ Hours = 1,
+ Days = 2
}
diff --git a/Extensions/TimeExtensions.cs b/Extensions/TimeExtensions.cs
index 7f2e88d..b47af8e 100644
--- a/Extensions/TimeExtensions.cs
+++ b/Extensions/TimeExtensions.cs
@@ -72,7 +72,7 @@ public static class TimeExtensions
///
///
///
- 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);
///
@@ -81,7 +81,7 @@ public static class TimeExtensions
///
///
///
- 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);
///
@@ -90,7 +90,7 @@ public static class TimeExtensions
///
///
///
- 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);
///
@@ -99,14 +99,14 @@ public static class TimeExtensions
///
///
///
- 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);
///
- 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);
///
- 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);
}
diff --git a/Internal/Internal.cs b/Internal/Internal.cs
index da5267d..60422ee 100644
--- a/Internal/Internal.cs
+++ b/Internal/Internal.cs
@@ -8,7 +8,7 @@ internal static class Internal
{
switch (timeFormat)
{
- case TimeFormat.DAYS:
+ case TimeFormat.Days:
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')}";
@@ -17,7 +17,7 @@ internal static class Internal
return $"{_timespan.Minutes.ToString().PadLeft(2, '0')}:{_timespan.Seconds.ToString().PadLeft(2, '0')}";
- case TimeFormat.HOURS:
+ case TimeFormat.Hours:
if (_timespan.TotalDays >= 1)
return $"{Math.Floor(_timespan.TotalHours).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')}";
- case TimeFormat.MINUTES:
+ case TimeFormat.Minutes:
if (_timespan.TotalHours >= 1)
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();
+ string returningString = string.Empty;
+ Dictionary humanReadable = new();
+
switch (timeFormat)
{
- case TimeFormat.DAYS:
- if (_timespan.TotalDays >= 1)
- return $"{Math.Floor(_timespan.TotalDays)} {config.DaysString}, {_timespan.Hours} {config.HoursString}";
+ case TimeFormat.Days:
+ {
+ 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 $"{_timespan.Hours} {config.HoursString}, {_timespan.Minutes} {config.MinutesString}";
+ return string.Join(", ", humanReadable.Where(x => x.Value).Select(x => x.Key));
+ }
- 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:
- if (_timespan.TotalDays >= 1)
- return $"{Math.Floor(_timespan.TotalHours)} {config.HoursString}, {_timespan.Minutes} {config.MinutesString}";
+ return string.Join(", ", humanReadable.Where(x => x.Value).Select(x => x.Key));
+ }
- if (_timespan.TotalHours >= 1)
- return $"{_timespan.Hours} {config.HoursString}, {_timespan.Minutes} {config.MinutesString}";
+ case TimeFormat.Minutes:
+ {
+ 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}";
-
- 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}";
+ return string.Join(", ", humanReadable.Where(x => x.Value).Select(x => x.Key));
+ }
default:
return _timespan.ToString();
@@ -111,4 +118,16 @@ public class HumanReadableTimeFormatConfig
/// Defaults to: "seconds".
///
public string SecondsString { get; set; } = "seconds";
+
+ ///
+ /// Must include minutes if timestamp is >= 1 day.
+ /// Defaults to: false.
+ ///
+ public bool MustIncludeMinutes { get; set; } = false;
+
+ ///
+ /// Must include seconds if timestamp is >= 1 hour.
+ /// Defaults to: false.
+ ///
+ public bool MustIncludeSeconds { get; set; } = false;
}
\ No newline at end of file