ISO8601形式の継続時間文字列をTimeSpanに変換
public class ExchangeUtil : PublicWebApiUtil {
///
/// ISO8601形式の文字列をTimeSpanに変換します。
///
/// ISO8601形式の文字列
///
public static TimeSpan Iso8601ToTimeSpan(string iso8601) {
if (iso8601 == null || iso8601 == "") {
//0秒で返す。
return new TimeSpan(0, 0, 0);
}
TimeSpan? ret = null;
int index = iso8601.IndexOf("D");
if (index > 0) {
//日付部("P1D")が存在する場合
string strDays = iso8601.Substring(0, index);
TimeSpan days = new TimeSpan(int.Parse(strDays.Replace("P", "")), 0, 0, 0);
ret = days;
//時間部が存在する場合
index = iso8601.IndexOf("DT");
if (index > 0) {
TimeSpan other = XmlConvert.ToTimeSpan("PT" + iso8601.Substring(index + 2));
ret = days.Add(other);
}
} else {
ret = XmlConvert.ToTimeSpan(iso8601.Replace("P", "PT"));
}
return new TimeSpan(ret.Value.Days, ret.Value.Hours, ret.Value.Minutes, 0);
}
}