fork download
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. public class Test
  7. {
  8. public static TimeSpan? TryParseTimeSpan(string input)
  9. {
  10. TimeSpan? ts = (TimeSpan?)null;
  11. if (!String.IsNullOrEmpty(input))
  12. {
  13. input = input.Trim();
  14. int length = input.Length % 2 == 0 ? input.Length : input.Length + 1;
  15. int count = length / 2;
  16.  
  17. if(count > 3) return null;
  18.  
  19. input = input.PadLeft(count * 2, '0');
  20.  
  21. string[] validFormats = new[] { "HHmmss", "mmss", "ss" };
  22. DateTime dt;
  23. if (DateTime.TryParseExact(input, validFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
  24. ts = dt.TimeOfDay;
  25. }
  26. return ts;
  27. }
  28.  
  29. public static void Main()
  30. {
  31. List<string> inputs = new List<string> { "78", "10545", "5" };
  32. IEnumerable<TimeSpan> timeSpans = inputs
  33. .Select(i => TryParseTimeSpan(i))
  34. .Where(ts => ts.HasValue)
  35. .Select(ts => ts.Value);
  36. foreach (TimeSpan ts in timeSpans)
  37. Console.WriteLine(ts.ToString());
  38.  
  39. }
  40. }
Success #stdin #stdout 0.06s 34024KB
stdin
Standard input is empty
stdout
01:05:45
00:00:05