XSDurationToTimeSpan static method

TimeSpan XSDurationToTimeSpan(
  1. String xsDuration
)
Takes an xs:duration String as defined by the W3 Consortiums Recommendation "XML Schema Part 2: Datatypes Second Edition", http://www.w3.org/TR/xmlschema-2/#duration, and converts it into a System.TimeSpan structure xs:duration String to convert

Implementation

static TimeSpan XSDurationToTimeSpan(String xsDuration) {
  String xsDateDuration = xsDuration.contains("T")
      ? xsDuration.split("T").first
      : xsDuration;

  String xsTimeDuration = xsDuration.split("T").length > 1
      ? xsDuration.split("T")[1]
      : "";

  RegExpMatch? m = PATTERN_TIME_SPAN.firstMatch(xsDateDuration);
  bool negative = false;

  if (m != null) {
    negative = true;
  }

  // Year
  m = PATTERN_YEAR.firstMatch(xsDateDuration);

  int year = 0;
  if (m != null) {
    year = int.parse(m.group(0)!.substring(0, m.group(0)!.indexOf("Y")));
  }

  // Month
  m = PATTERN_MONTH.firstMatch(xsDateDuration);

  int month = 0;
  if (m != null) {
    month = int.parse(m.group(0)!.substring(0, m.group(0)!.indexOf("M")));
  }

  // Day
  m = PATTERN_DAY.firstMatch(xsDateDuration);

  int day = 0;
  if (m != null) {
    day = int.parse(m.group(0)!.substring(0, m.group(0)!.indexOf("D")));
  }

  // Hour
  m = PATTERN_HOUR.firstMatch(xsTimeDuration);

  int hour = 0;
  if (m != null) {
    hour = int.parse(m.group(0)!.substring(0, m.group(0)!.indexOf("H")));
  }

  // Minute
  m = PATTERN_MINUTES.firstMatch(xsTimeDuration);

  int minute = 0;
  if (m != null) {
    minute = int.parse(m.group(0)!.substring(0, m.group(0)!.indexOf("M")));
  }

  // Seconds
  m = PATTERN_SECONDS.firstMatch(xsTimeDuration);

  int seconds = 0;
  if (m != null) {
    seconds = int.parse(m.group(0)!.substring(0, m.group(0)!.indexOf("S")));
  }

  int milliseconds = 0;
  //          m = PATTERN_MILLISECONDS.firstMatch(xsDuration);
  //
  //          if (m != null) {
  //            // Only allowed 4 digits of precision
  //            if (m.group(0).length > 5) {
  //              milliseconds = int.parse(m.group(0).substring(0, 4));
  //            } else {
  //              seconds = int.parse(m.group(0).substring(0,
  //                  m.group(0).indexOf("S")));
  //            }
  //          }

  // Apply conversions of year and months to days.
  // Year = 365 days
  // Month = 30 days
  day = day + (year * 365) + (month * 30);
  // TimeSpan retval = new TimeSpan(day, hour, minute, seconds,
  // milliseconds);
  int retval =
      (((((((day * 24) + hour) * 60) + minute) * 60) + seconds) * 1000) +
      milliseconds;
  if (negative) {
    retval = -retval;
  }
  return TimeSpan(retval);
  //          RegExp timeSpanParser = new RegExp(
  //                "(?<pos>-)?" +
  //                "P" +
  //                "((?<year>[0-9]+)Y)?" +
  //                "((?<month>[0-9]+)M)?" +
  //                "((?<day>[0-9]+)D)?" +
  //                "(T" +
  //                "((?<hour>[0-9]+)H)?" +
  //                "((?<minute>[0-9]+)M)?" +
  //                "((?<seconds>[0-9]+)(\\.(?<precision>[0-9]+))?S)?)?");
  //
  //            Match m = timeSpanParser.Match(xsDuration);
  //            if (!m.Success)
  //            {
  //                throw new ArgumentException(Strings.XsDurationCouldNotBeParsed);
  //            }
  //            String token = m.Result("${pos}");
  //            bool negative = false;
  //            if (!StringUtils.IsNullOrEmpty(token))
  //            {
  //                negative = true;
  //            }
  //
  //            // Year
  //            token = m.Result("${year}");
  //            int year = 0;
  //            if (!StringUtils.IsNullOrEmpty(token))
  //            {
  //                year = Int32.Parse(token);
  //            }
  //
  //            // Month
  //            token = m.Result("${month}");
  //            int month = 0;
  //            if (!StringUtils.IsNullOrEmpty(token))
  //            {
  //                month = Int32.Parse(token);
  //            }
  //
  //            // Day
  //            token = m.Result("${day}");
  //            int day = 0;
  //            if (!StringUtils.IsNullOrEmpty(token))
  //            {
  //                day = Int32.Parse(token);
  //            }
  //
  //            // Hour
  //            token = m.Result("${hour}");
  //            int hour = 0;
  //            if (!StringUtils.IsNullOrEmpty(token))
  //            {
  //                hour = Int32.Parse(token);
  //            }
  //
  //            // Minute
  //            token = m.Result("${minute}");
  //            int minute = 0;
  //            if (!StringUtils.IsNullOrEmpty(token))
  //            {
  //                minute = Int32.Parse(token);
  //            }
  //
  //            // Seconds
  //            token = m.Result("${seconds}");
  //            int seconds = 0;
  //            if (!StringUtils.IsNullOrEmpty(token))
  //            {
  //                seconds = Int32.Parse(token);
  //            }
  //
  //            int milliseconds = 0;
  //            token = m.Result("${precision}");
  //
  //            // Only allowed 4 digits of precision
  //            if (token.Length > 4)
  //            {
  //                token = token.Substring(0, 4);
  //            }
  //
  //            if (!StringUtils.IsNullOrEmpty(token))
  //            {
  //                milliseconds = Int32.Parse(token);
  //            }
  //
  //            // Apply conversions of year and months to days.
  //            // Year = 365 days
  //            // Month = 30 days
  //            day = day + (year * 365) + (month * 30);
  //            TimeSpan retval = new TimeSpan(day, hour, minute, seconds, milliseconds);
  //
  //            if (negative)
  //            {
  //                retval = -retval;
  //            }
  //
  //            return retval;
}