Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static class TimeExtensions
- {
- public static string FormatShortTime( TimeSpan time )
- {
- string result = string.Empty;
- if (time.Days != 0)
- result += $"{time.Days} д. ";
- if (time.Hours != 0)
- result += $"{time.Hours} ч. ";
- if (time.Minutes != 0)
- result += $"{time.Minutes} м. ";
- if (time.Seconds != 0)
- result += $"{time.Seconds} с. ";
- return result;
- }
- public static string FormatTime( TimeSpan time )
- {
- string result = string.Empty;
- if (time.Days != 0)
- result += $"{Format( time.Days, "дней", "дня", "день" )} ";
- if (time.Hours != 0)
- result += $"{Format( time.Hours, "часов", "часа", "час" )} ";
- if (time.Minutes != 0)
- result += $"{Format( time.Minutes, "минут", "минуты", "минута" )} ";
- if (time.Seconds != 0)
- result += $"{Format( time.Seconds, "секунд", "секунды", "секунда" )} ";
- return result;
- }
- private static string Format( int units, string form1, string form2, string form3 )
- {
- var tmp = units % 10;
- if (units >= 5 && units <= 20 || tmp >= 5 && tmp <= 9)
- return $"{units} {form1}";
- if (tmp >= 2 && tmp <= 4)
- return $"{units} {form2}";
- return $"{units} {form3}";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement