| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | namespace AmbientServices.Extensions; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// A static class that extends <see cref="System.TimeSpan"/>. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks> |
| | | 9 | | /// <pitch>Human-oriented duration handling: parse strings like "30s", "5 minutes", or "1:30:00 |
| | | 10 | | /// <pledge> |
| | | 11 | | /// Parsing accepts standard colon-delimited <see cref="TimeSpan"/> syntax, or a number followed by an optional unit (ye |
| | | 12 | | /// Rendering is lossy by design: it picks a single unit large enough for the value to read comfortably, keeps at most o |
| | | 13 | | /// </pledge> |
| | | 14 | | /// </remarks> |
| | | 15 | | public static class TimeSpanExtensions |
| | | 16 | | { |
| | 2 | 17 | | private static readonly char[] Alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); |
| | 2 | 18 | | private static readonly char[] Numeric = "-0123456789. \t".ToCharArray(); |
| | | 19 | | /// <summary> |
| | | 20 | | /// Attempts to parse a string as a timespan. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="span">The candidate string.</param> |
| | | 23 | | /// <returns>A <see cref="TimeSpan"/>, if one could be parsed, or <b>null</b> if not.</returns> |
| | | 24 | | public static TimeSpan? TryParseTimeSpan(this string span) |
| | | 25 | | { |
| | 2 | 26 | | if (string.IsNullOrEmpty(span)) return null; |
| | | 27 | | // is a span specified and does it contain a : and does it parse correctly as a timespan? |
| | | 28 | | TimeSpan timeSpan; |
| | 2 | 29 | | if (span.Contains(':', StringComparison.Ordinal) && TimeSpan.TryParse(span, out timeSpan)) |
| | | 30 | | { |
| | | 31 | | // use that timespan |
| | 2 | 32 | | return timeSpan; |
| | | 33 | | } |
| | | 34 | | // split out the numeric part |
| | | 35 | | double units; |
| | 2 | 36 | | if (!double.TryParse(span.TrimEnd(Alpha), out units)) |
| | | 37 | | { |
| | 2 | 38 | | return null; |
| | | 39 | | } |
| | | 40 | | // figure out the unit type |
| | 2 | 41 | | string unitType = span.TrimStart(Numeric); |
| | | 42 | | // handle "M" specially because it's months as opposed to minutes |
| | 2 | 43 | | if (unitType == "m") unitType = "MINUTES"; |
| | 2 | 44 | | return unitType.ToUpperInvariant() switch { |
| | 2 | 45 | | "Y" or "YEAR" or "YEARS" => TimeSpan.FromDays(365.25 * units), |
| | 2 | 46 | | "M" or "MONTH" or "MONTHS" => TimeSpan.FromDays(30.4375 * units), |
| | 2 | 47 | | "D" or "DAY" or "DAYS" => TimeSpan.FromDays(units), |
| | 2 | 48 | | "H" or "HOUR" or "HOURS" => TimeSpan.FromHours(units), |
| | 2 | 49 | | "MINUTE" or "MINUTES" => TimeSpan.FromMinutes(units), |
| | 2 | 50 | | "S" or "SECOND" or "SECONDS" => TimeSpan.FromSeconds(units), |
| | 2 | 51 | | "MS" or "MILLISECOND" or "MILLISECONDS" => TimeSpan.FromMilliseconds(units), |
| | 2 | 52 | | _ => TimeSpan.FromTicks((long)units), |
| | 2 | 53 | | }; |
| | | 54 | | } |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets a short string representing the specified timespan. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="duration">The <see cref="TimeSpan"/> whose string representation is to be generated</param> |
| | | 59 | | /// <returns>An easily human-readable string representing the time span with a postfix character indicating the unit |
| | | 60 | | public static string ToShortHumanReadableString(this TimeSpan duration) |
| | | 61 | | { |
| | | 62 | | string sign; |
| | | 63 | | TimeSpan absTimeSpan; |
| | 2 | 64 | | if (duration.Ticks < 0) |
| | | 65 | | { |
| | 2 | 66 | | sign = "-"; |
| | 2 | 67 | | absTimeSpan = new TimeSpan(-duration.Ticks); |
| | | 68 | | } |
| | | 69 | | else |
| | | 70 | | { |
| | 2 | 71 | | sign = ""; |
| | 2 | 72 | | absTimeSpan = new TimeSpan(duration.Ticks); |
| | | 73 | | } |
| | 2 | 74 | | if (MatchUnits(absTimeSpan.TotalDays, 731)) |
| | | 75 | | { |
| | 2 | 76 | | return UnitString(sign, absTimeSpan.TotalDays / 365.25, "Y"); |
| | | 77 | | } |
| | 2 | 78 | | if (MatchUnits(absTimeSpan.TotalDays, 61)) |
| | | 79 | | { |
| | 2 | 80 | | return UnitString(sign, absTimeSpan.TotalDays / 30.4375, "M"); |
| | | 81 | | } |
| | 2 | 82 | | if (MatchUnits(absTimeSpan.TotalHours, 48)) |
| | | 83 | | { |
| | 2 | 84 | | return UnitString(sign, absTimeSpan.TotalDays, "D"); |
| | | 85 | | } |
| | 2 | 86 | | if (MatchUnits(absTimeSpan.TotalMinutes, 120)) |
| | | 87 | | { |
| | 2 | 88 | | return UnitString(sign, absTimeSpan.TotalHours, "h"); |
| | | 89 | | } |
| | 2 | 90 | | if (MatchUnits(absTimeSpan.TotalSeconds, 120)) |
| | | 91 | | { |
| | 2 | 92 | | return UnitString(sign, absTimeSpan.TotalMinutes, "m"); |
| | | 93 | | } |
| | 2 | 94 | | if (MatchUnits(absTimeSpan.TotalMilliseconds, 2000)) |
| | | 95 | | { |
| | 2 | 96 | | return UnitString(sign, absTimeSpan.TotalSeconds, "s"); |
| | | 97 | | } |
| | 2 | 98 | | return UnitString(sign, absTimeSpan.TotalMilliseconds, "ms"); |
| | | 99 | | } |
| | | 100 | | /// <summary> |
| | | 101 | | /// Gets a long string representing the specified timespan. |
| | | 102 | | /// </summary> |
| | | 103 | | /// <param name="duration">The <see cref="TimeSpan"/> whose string is to be generated.</param> |
| | | 104 | | /// <returns>An easily human-readable string representing the time span with a postfix string indicating the units ( |
| | | 105 | | public static string ToLongHumanReadableString(this TimeSpan duration) |
| | | 106 | | { |
| | | 107 | | string sign; |
| | | 108 | | TimeSpan absTimeSpan; |
| | 2 | 109 | | if (duration.Ticks < 0) |
| | | 110 | | { |
| | 2 | 111 | | sign = "-"; |
| | 2 | 112 | | absTimeSpan = new TimeSpan(-duration.Ticks); |
| | | 113 | | } |
| | | 114 | | else |
| | | 115 | | { |
| | 2 | 116 | | sign = ""; |
| | 2 | 117 | | absTimeSpan = new TimeSpan(duration.Ticks); |
| | | 118 | | } |
| | 2 | 119 | | if (MatchUnits(absTimeSpan.TotalDays, 731)) |
| | | 120 | | { |
| | 2 | 121 | | return UnitStringWithPlural(sign, absTimeSpan.TotalDays / 365.25, " Year"); |
| | | 122 | | } |
| | 2 | 123 | | if (MatchUnits(absTimeSpan.TotalDays, 61)) |
| | | 124 | | { |
| | 2 | 125 | | return UnitStringWithPlural(sign, absTimeSpan.TotalDays / 30.4375, " Month"); |
| | | 126 | | } |
| | 2 | 127 | | if (MatchUnits(absTimeSpan.TotalHours, 48)) |
| | | 128 | | { |
| | 2 | 129 | | return UnitStringWithPlural(sign, absTimeSpan.TotalDays, " Day"); |
| | | 130 | | } |
| | 2 | 131 | | if (MatchUnits(absTimeSpan.TotalMinutes, 120)) |
| | | 132 | | { |
| | 2 | 133 | | return UnitStringWithPlural(sign, absTimeSpan.TotalHours, " hour"); |
| | | 134 | | } |
| | 2 | 135 | | if (MatchUnits(absTimeSpan.TotalSeconds, 120)) |
| | | 136 | | { |
| | 2 | 137 | | return UnitStringWithPlural(sign, absTimeSpan.TotalMinutes, " minute"); |
| | | 138 | | } |
| | 2 | 139 | | if (MatchUnits(absTimeSpan.TotalMilliseconds, 2000)) |
| | | 140 | | { |
| | 2 | 141 | | return UnitStringWithPlural(sign, absTimeSpan.TotalSeconds, " second"); |
| | | 142 | | } |
| | 2 | 143 | | return UnitStringWithPlural(sign, absTimeSpan.TotalMilliseconds, " millisecond"); |
| | | 144 | | } |
| | | 145 | | private static bool MatchUnits(double total, int count) |
| | | 146 | | { |
| | 2 | 147 | | if (total < 2.0) |
| | | 148 | | { |
| | 2 | 149 | | return false; |
| | | 150 | | } |
| | 2 | 151 | | return total >= count; |
| | | 152 | | } |
| | | 153 | | private static string UnitString(string prefix, double count, string postfix) |
| | | 154 | | { |
| | 2 | 155 | | int intPart = (int)count; |
| | 2 | 156 | | int firstDecimal = ((int)(count * 10.0) % 10); |
| | 2 | 157 | | if (intPart < 10 && firstDecimal != 0) |
| | | 158 | | { |
| | 2 | 159 | | return prefix + intPart.ToString(System.Globalization.CultureInfo.InvariantCulture) + "." + firstDecimal.ToS |
| | | 160 | | } |
| | 2 | 161 | | return prefix + intPart.ToString(System.Globalization.CultureInfo.InvariantCulture) + postfix; |
| | | 162 | | } |
| | | 163 | | private static string UnitStringWithPlural(string prefix, double count, string postfix) |
| | | 164 | | { |
| | 2 | 165 | | int intPart = (int)count; |
| | 2 | 166 | | int firstDecimal = ((int)(count * 10.0) % 10); |
| | 2 | 167 | | if (intPart < 10 && firstDecimal != 0) |
| | | 168 | | { |
| | 2 | 169 | | return prefix + intPart.ToString(System.Globalization.CultureInfo.InvariantCulture) + "." + firstDecimal.ToS |
| | | 170 | | } |
| | | 171 | | // else nothing past the decimal |
| | 2 | 172 | | if (intPart != 1) postfix += "s"; |
| | 2 | 173 | | return prefix + intPart.ToString(System.Globalization.CultureInfo.InvariantCulture) + postfix; |
| | | 174 | | } |
| | | 175 | | } |