| | 1 | | using System; |
| | 2 | |
|
| | 3 | | namespace NtpServiceLibrary |
| | 4 | | { |
| | 5 | | /// <summary> |
| | 6 | | /// Provides extension methods for the <see cref="TimeSpan"/> structure. |
| | 7 | | /// </summary> |
| | 8 | | public static class TimeSpanExtension |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Formats the <see cref="TimeSpan"/> into a human-readable string |
| | 12 | | /// with correct pluralization for hours, minutes, and seconds. |
| | 13 | | /// </summary> |
| | 14 | | /// <param name="timespan">The <see cref="TimeSpan"/> to format.</param> |
| | 15 | | /// <returns> |
| | 16 | | /// A string in the format "X hour(s) Y minute(s) Z second(s)". |
| | 17 | | /// </returns> |
| | 18 | | public static string Format(this TimeSpan timespan) |
| 4 | 19 | | { |
| 4 | 20 | | return string.Format("{0} hour{1} {2} minute{3} {4} second{5}", |
| 4 | 21 | | timespan.Hours, Suffix(timespan.Hours), |
| 4 | 22 | | timespan.Minutes, Suffix(timespan.Minutes), |
| 4 | 23 | | timespan.Seconds, Suffix(timespan.Seconds) |
| 4 | 24 | | ); |
| 4 | 25 | | } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Returns the plural suffix "s" if the value is not 1. |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="value">The numeric value to check.</param> |
| | 31 | | /// <returns>"s" if value is not 1; otherwise, an empty string.</returns> |
| | 32 | | private static string Suffix(int value) |
| 12 | 33 | | { |
| 12 | 34 | | return value != 1 ? "s" : ""; |
| 12 | 35 | | } |
| | 36 | | } |
| | 37 | | } |