TimeSpanToXSDuration static method

String TimeSpanToXSDuration(
  1. TimeSpan timeSpan
)
Takes a System.TimeSpan structure and converts it into 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 TimeSpan structure to convert

Implementation

static String TimeSpanToXSDuration(TimeSpan timeSpan) {
  // Optional '-' offset
  String offsetStr = (timeSpan.TotalSeconds < 0) ? "-" : "";

  // The TimeSpan structure does not have a Year or Month
  // property, therefore we wouldn't be able to return an xs:duration
  // String from a TimeSpan that included the nY or nM components.
  return "${offsetStr}P${timeSpan.Days.abs}DT${timeSpan.Hours.abs}H${timeSpan.Minutes.abs}M${timeSpan.Seconds.abs}.${timeSpan.Milliseconds.abs}S";
}