parse static method

TimeSpan parse(
  1. String s
)

Implementation

static TimeSpan parse(String s) {
  String str = s.trim();
  List<String> st1 = str.split("\\.");
  int days = 0, millsec = 0, totMillSec = 0;
  String data = str;
  switch (st1.length) {
    case 1:
      data = str;
      break;
    case 2:
      if (st1[0].split(":").length > 1) {
        millsec = int.parse(st1[1]);
        data = st1[0];
      } else {
        days = int.parse(st1[0]);
        data = st1[1];
      }
      break;
    case 3:
      days = int.parse(st1[0]);
      data = st1[1];
      millsec = int.parse(st1[2]);
      break;
    default:
      throw new FormatException("Bad Format");
  }
  List<String> st = data.split(":");
  switch (st.length) {
    case 1:
      totMillSec = int.parse(str) * 24 * 60 * 60 * 1000;
      break;
    case 2:
      totMillSec = (int.parse(st[0]) * 60 * 60 * 1000) +
          (int.parse(st[1]) * 60 * 1000);
      break;
    case 3:
      totMillSec = (int.parse(st[0]) * 60 * 60 * 1000) +
          (int.parse(st[1]) * 60 * 1000) +
          (int.parse(st[2]) * 1000);
      break;
    case 4:
      totMillSec = (int.parse(st[0]) * 24 * 60 * 60 * 1000) +
          (int.parse(st[1]) * 60 * 60 * 1000) +
          (int.parse(st[2]) * 60 * 1000) +
          (int.parse(st[3]) * 1000);
      break;
    default:
      throw new FormatException("Bad Format/Overflow");
  }
  totMillSec += (days * 24 * 60 * 60 * 1000) + millsec;
  return new TimeSpan(totMillSec);
}