mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-01-11 19:44:55 +00:00
feat: Nicer timespan readable formatting
This commit is contained in:
@@ -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).
|
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
### Changed
|
||||||
|
- Improved methods for human formatted timespan, including without milliseconds.
|
||||||
|
|
||||||
## [17.0.0] 2022-12-02
|
## [17.0.0] 2022-12-02
|
||||||
### Changed
|
### Changed
|
||||||
|
|||||||
@@ -13,30 +13,57 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string ToReadableString(this TimeSpan extends)
|
public static string ToReadableString(this TimeSpan extends)
|
||||||
{
|
{
|
||||||
|
return extends.ToReadableString(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 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
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="extends"></param>
|
||||||
|
/// <param name="hideMilliseconds"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
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();
|
StringBuilder builder = new StringBuilder();
|
||||||
|
|
||||||
if (extends.Days == 1)
|
// Handle negative time spans
|
||||||
builder.AppendFormat("{0} day, ", extends.Days);
|
if (zeroComparison < 0)
|
||||||
else if (extends.Days > 1)
|
builder.Append("-");
|
||||||
builder.AppendFormat("{0} days, ", extends.Days);
|
|
||||||
|
|
||||||
if (extends.Hours == 1)
|
// Get absolute value so negatives can be ignored
|
||||||
builder.AppendFormat("{0} hour, ", extends.Hours);
|
TimeSpan timeSpan = extends.Duration();
|
||||||
else if (extends.Hours > 1)
|
|
||||||
builder.AppendFormat("{0} hours, ", extends.Hours);
|
|
||||||
|
|
||||||
if (extends.Minutes == 1)
|
if (timeSpan.Days == 1)
|
||||||
builder.AppendFormat("{0} minute, ", extends.Minutes);
|
builder.AppendFormat("{0} day, ", timeSpan.Days);
|
||||||
else if (extends.Minutes > 1)
|
else if (timeSpan.Days > 1)
|
||||||
builder.AppendFormat("{0} minutes, ", extends.Minutes);
|
builder.AppendFormat("{0} days, ", timeSpan.Days);
|
||||||
|
|
||||||
if (extends.Seconds == 1)
|
if (timeSpan.Hours == 1)
|
||||||
builder.AppendFormat("{0} second, ", extends.Seconds);
|
builder.AppendFormat("{0} hour, ", timeSpan.Hours);
|
||||||
else if (extends.Seconds > 1)
|
else if (timeSpan.Hours > 1)
|
||||||
builder.AppendFormat("{0} seconds, ", extends.Seconds);
|
builder.AppendFormat("{0} hours, ", timeSpan.Hours);
|
||||||
|
|
||||||
if (extends.Milliseconds > 0)
|
if (timeSpan.Minutes == 1)
|
||||||
builder.AppendFormat("{0} ms", extends.Milliseconds);
|
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(',', ' ');
|
return builder.ToString().TrimEnd(',', ' ');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user