|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +namespace kate.shared.Helpers |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Helper class for formatting things |
| 8 | + /// </summary> |
| 9 | + public static class FormatHelper |
| 10 | + { |
| 11 | + public const string SqlServerDateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff"; |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// Format a TimeSpan to either one of the following; |
| 15 | + /// <list type="bullet"> |
| 16 | + /// <item><c>0d 0h 0m 0s</c></item> |
| 17 | + /// <item><c>0h 0m 0s</c></item> |
| 18 | + /// <item><c>0m 0s</c></item> |
| 19 | + /// <item><c>0s</c></item> |
| 20 | + /// <item><c>0d 0h 0m 0.000s</c></item> |
| 21 | + /// <item><c>0h 0m 0.000s</c></item> |
| 22 | + /// <item><c>0m 0.000s</c></item> |
| 23 | + /// <item><c>0.000s</c></item> |
| 24 | + /// <item><c>0ms</c> (when <see cref="TimeSpan.TotalMilliseconds"/> is less than <c>2000</c>)</item> |
| 25 | + /// </list> |
| 26 | + /// </summary> |
| 27 | + public static string Duration(TimeSpan span) |
| 28 | + { |
| 29 | + if (span.TotalMilliseconds < 2000) |
| 30 | + { |
| 31 | + return span.TotalMilliseconds.ToString() + "ms"; |
| 32 | + } |
| 33 | + var result = new List<string>(); |
| 34 | + |
| 35 | + if (span.Milliseconds > 10) |
| 36 | + { |
| 37 | + result.Add(string.Format("{0}.{1}", |
| 38 | + span.Seconds, |
| 39 | + span.Milliseconds.ToString().PadLeft(3, '0'))); |
| 40 | + } |
| 41 | + else |
| 42 | + { |
| 43 | + result.Add(span.Seconds.ToString() + 's'); |
| 44 | + } |
| 45 | + |
| 46 | + if (span.Minutes > 0) |
| 47 | + { |
| 48 | + result.Insert(0, |
| 49 | + string.Format("{0}m", span.Minutes)); |
| 50 | + } |
| 51 | + if (span.Hours > 0) |
| 52 | + { |
| 53 | + result.Insert(0, |
| 54 | + string.Format("{0}h", span.Hours)); |
| 55 | + } |
| 56 | + if (span.Days > 0) |
| 57 | + { |
| 58 | + result.Insert(0, |
| 59 | + string.Format("{0}d", span.Days)); |
| 60 | + } |
| 61 | + return string.Join(" ", result); |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Format the duration between <paramref name="start"/> and the current date (using <see cref="DateTimeOffset.UtcNow"/>) |
| 66 | + /// with <see cref="Duration(TimeSpan)"/> |
| 67 | + /// </summary> |
| 68 | + public static string DurationToUtcNow(DateTimeOffset start) |
| 69 | + { |
| 70 | + var end = DateTimeOffset.UtcNow; |
| 71 | + var diff = end - start; |
| 72 | + |
| 73 | + return Duration(diff); |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Format the duration between <paramref name="start"/> and the current date (using <see cref="DateTime.UtcNow"/>) |
| 78 | + /// with <see cref="Duration(TimeSpan)"/> |
| 79 | + /// </summary> |
| 80 | + public static string DurationToUtcNow(DateTime start) |
| 81 | + { |
| 82 | + var end = DateTime.UtcNow; |
| 83 | + var diff = end - start; |
| 84 | + |
| 85 | + return Duration(diff); |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Format the duration between <paramref name="start"/> and the current date (using <see cref="DateTimeOffset.Now"/>) |
| 90 | + /// with <see cref="Duration(TimeSpan)"/> |
| 91 | + /// </summary> |
| 92 | + public static string DurationToNow(DateTimeOffset start) |
| 93 | + { |
| 94 | + var end = DateTimeOffset.Now; |
| 95 | + var diff = end - start; |
| 96 | + |
| 97 | + return Duration(diff); |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Format the duration between <paramref name="start"/> and the current date (using <see cref="DateTime.Now"/>) |
| 102 | + /// with <see cref="Duration(TimeSpan)"/> |
| 103 | + /// </summary> |
| 104 | + public static string DurationToNow(DateTime start) |
| 105 | + { |
| 106 | + var end = DateTime.Now; |
| 107 | + var diff = end - start; |
| 108 | + |
| 109 | + return Duration(diff); |
| 110 | + } |
| 111 | + public static string DurationMilliseconds(long value) |
| 112 | + { |
| 113 | + return Duration(TimeSpan.FromMilliseconds(value)); |
| 114 | + } |
| 115 | +#if NET8_0_OR_GREATER |
| 116 | + public static string DurationMicroseconds(long value) |
| 117 | + { |
| 118 | + return Duration(TimeSpan.FromMicroseconds(value)); |
| 119 | + } |
| 120 | +#endif |
| 121 | + } |
| 122 | +} |
0 commit comments