diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2ab10fd..a9f656d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
## [Unreleased]
+### Changed
+ - Improved methods for human formatted timespan, including without milliseconds.
## [17.0.0] 2022-12-02
### Changed
diff --git a/ICD.Common.Utils/Extensions/TimeSpanExtensions.cs b/ICD.Common.Utils/Extensions/TimeSpanExtensions.cs
index 178e9ca..eb3f356 100644
--- a/ICD.Common.Utils/Extensions/TimeSpanExtensions.cs
+++ b/ICD.Common.Utils/Extensions/TimeSpanExtensions.cs
@@ -13,34 +13,61 @@ namespace ICD.Common.Utils.Extensions
///
public static string ToReadableString(this TimeSpan extends)
{
- StringBuilder builder = new StringBuilder();
-
- if (extends.Days == 1)
- builder.AppendFormat("{0} day, ", extends.Days);
- else if (extends.Days > 1)
- builder.AppendFormat("{0} days, ", extends.Days);
-
- if (extends.Hours == 1)
- builder.AppendFormat("{0} hour, ", extends.Hours);
- else if (extends.Hours > 1)
- builder.AppendFormat("{0} hours, ", extends.Hours);
-
- if (extends.Minutes == 1)
- builder.AppendFormat("{0} minute, ", extends.Minutes);
- else if (extends.Minutes > 1)
- builder.AppendFormat("{0} minutes, ", extends.Minutes);
-
- if (extends.Seconds == 1)
- builder.AppendFormat("{0} second, ", extends.Seconds);
- else if (extends.Seconds > 1)
- builder.AppendFormat("{0} seconds, ", extends.Seconds);
-
- if (extends.Milliseconds > 0)
- builder.AppendFormat("{0} ms", extends.Milliseconds);
-
- return builder.ToString().TrimEnd(',', ' ');
+ return extends.ToReadableString(false);
}
+ ///
+ /// Writes the TimeSpan to a string in the format "A day/s, B hour/s, C minute/s, D second/s, E ms"
+ /// Omits any items that are 0.
+ /// Optionally hides the miliseconds
+ ///
+ ///
+ ///
+ ///
+ public static string ToReadableString(this TimeSpan extends, bool hideMilliseconds)
+ {
+ int zeroComparison = extends.CompareTo(TimeSpan.Zero);
+
+ if (zeroComparison == 0)
+ return "Zero Time";
+
+ StringBuilder builder = new StringBuilder();
+
+ // Handle negative time spans
+ if (zeroComparison < 0)
+ builder.Append("-");
+
+ // Get absolute value so negatives can be ignored
+ TimeSpan timeSpan = extends.Duration();
+
+ if (timeSpan.Days == 1)
+ builder.AppendFormat("{0} day, ", timeSpan.Days);
+ else if (timeSpan.Days > 1)
+ builder.AppendFormat("{0} days, ", timeSpan.Days);
+
+ if (timeSpan.Hours == 1)
+ builder.AppendFormat("{0} hour, ", timeSpan.Hours);
+ else if (timeSpan.Hours > 1)
+ builder.AppendFormat("{0} hours, ", timeSpan.Hours);
+
+ if (timeSpan.Minutes == 1)
+ builder.AppendFormat("{0} minute, ", timeSpan.Minutes);
+ else if (timeSpan.Minutes > 1)
+ builder.AppendFormat("{0} minutes, ", timeSpan.Minutes);
+
+ if (timeSpan.Seconds == 1)
+ builder.AppendFormat("{0} second, ", timeSpan.Seconds);
+ else if (timeSpan.Seconds > 1)
+ builder.AppendFormat("{0} seconds, ", timeSpan.Seconds);
+ else if (hideMilliseconds && (long)timeSpan.TotalSeconds == 0)
+ builder.AppendFormat("0 seconds");
+
+ if (!hideMilliseconds && timeSpan.Milliseconds > 0)
+ builder.AppendFormat("{0} ms", timeSpan.Milliseconds);
+
+ return builder.ToString().TrimEnd(',', ' ');
+ }
+
///
/// Adds the given number of hours to the time, wrapping every 24 hours without modifying the day.
///