Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.74 KB | None | 0 0
  1. private static class TimeExtensions
  2.         {
  3.             public static string FormatShortTime( TimeSpan time )
  4.             {
  5.                 string result = string.Empty;
  6.                 if (time.Days != 0)
  7.                     result += $"{time.Days} д. ";
  8.  
  9.                 if (time.Hours != 0)
  10.                     result += $"{time.Hours} ч. ";
  11.  
  12.                 if (time.Minutes != 0)
  13.                     result += $"{time.Minutes} м. ";
  14.  
  15.                 if (time.Seconds != 0)
  16.                     result += $"{time.Seconds} с. ";
  17.  
  18.                 return result;
  19.             }
  20.  
  21.             public static string FormatTime( TimeSpan time )
  22.             {
  23.                 string result = string.Empty;
  24.                 if (time.Days != 0)
  25.                     result += $"{Format( time.Days, "дней", "дня", "день" )} ";
  26.  
  27.                 if (time.Hours != 0)
  28.                     result += $"{Format( time.Hours, "часов", "часа", "час" )} ";
  29.  
  30.                 if (time.Minutes != 0)
  31.                     result += $"{Format( time.Minutes, "минут", "минуты", "минута" )} ";
  32.  
  33.                 if (time.Seconds != 0)
  34.                     result += $"{Format( time.Seconds, "секунд", "секунды", "секунда" )} ";
  35.  
  36.                 return result;
  37.             }
  38.  
  39.             private static string Format( int units, string form1, string form2, string form3 )
  40.             {
  41.                 var tmp = units % 10;
  42.  
  43.                 if (units >= 5 && units <= 20 || tmp >= 5 && tmp <= 9)
  44.                     return $"{units} {form1}";
  45.  
  46.                 if (tmp >= 2 && tmp <= 4)
  47.                     return $"{units} {form2}";
  48.  
  49.                 return $"{units} {form3}";
  50.             }
  51.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement