fork(2) download
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. public class Test
  7. {
  8.  
  9.  
  10. public static void Main()
  11. {
  12. string[] strings = new[] { "1:23:45.6", "23:45.6", "23:45", "1:23:45" };
  13. string[] formats = new[] { "H:mm:ss.f", "H:mm.f", "H:mm", "H:mm:ss" };
  14. TimeSpan[] timespans = strings
  15. .Select(str =>
  16. {
  17. TimeSpan? ts = null;
  18. DateTime dt;
  19. if (DateTime.TryParseExact(str, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
  20. ts = dt.TimeOfDay;
  21. return ts;
  22. })
  23. .Where(ts => ts.HasValue)
  24. .Select(ts => ts.Value)
  25. .ToArray();
  26. foreach(TimeSpan ts in timespans)
  27. Console.WriteLine(ts.ToString());
  28. }
  29. }
Success #stdin #stdout 0.07s 34112KB
stdin
Standard input is empty
stdout
01:23:45.6000000
23:45:00.6000000
23:45:00
01:23:45