fork(1) download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. private static readonly string[] oneformats = {"{0:d}d", "{0:d}h", "{0:d}m", "{0:d}s"};
  6. private static readonly string[] twoformats = {"{0:d}d {1:d}h", "{0:d}h {1:d}m", "{0:d}m {1:d}s"};
  7.  
  8.  
  9. static string FormatRushTime (TimeSpan span, bool reportZero )
  10. {
  11. int[] times = new int[]{span.Days, span.Hours, span.Minutes, span.Seconds};
  12. for (int i = 0; i < times.Length; i++)
  13. {
  14. if (times[i] != 0)
  15. {
  16. if (((i + 1) < times.Length) && (times[i+1] != 0 || reportZero))
  17. {
  18. return String.Format(twoformats[i], times[i], times[i+1]);
  19. }
  20. return String.Format(oneformats[i], times[i]);
  21. }
  22. }
  23. return String.Empty;
  24. }
  25.  
  26. private static void testPrint(TimeSpan span)
  27. {
  28. Console.WriteLine(String.Format("{0} - Zero: {1} NoZero: {2}", span,
  29. FormatRushTime(span, false), FormatRushTime(span, true)));
  30. }
  31.  
  32. public static void Main()
  33. {
  34. testPrint(new TimeSpan(123456789123));
  35. testPrint(new TimeSpan(1,0,1,1));
  36. testPrint(new TimeSpan(0,0,1,1));
  37. testPrint(new TimeSpan(0,1,0,1));
  38. testPrint(new TimeSpan(-1,1,0,1));
  39. }
  40. }
Success #stdin #stdout 0.03s 33832KB
stdin
Standard input is empty
stdout
03:25:45.6789123 - Zero: 3h 25m  NoZero: 3h 25m
1.00:01:01 - Zero: 1d  NoZero: 1d 0h
00:01:01 - Zero: 1m 1s  NoZero: 1m 1s
01:00:01 - Zero: 1h  NoZero: 1h 0m
-22:59:59 - Zero: -22h -59m  NoZero: -22h -59m