C#でISO8601形式の継続時間文字列をTimeSpanに変換

 

ISO8601形式の継続時間文字列をTimeSpanに変換

  1. public class ExchangeUtil : PublicWebApiUtil {
  2. ///
  3. /// ISO8601形式の文字列をTimeSpanに変換します。
  4. ///
  5. /// ISO8601形式の文字列
  6. ///
  7. public static TimeSpan Iso8601ToTimeSpan(string iso8601) {
  8. if (iso8601 == null || iso8601 == "") {
  9. //0秒で返す。
  10. return new TimeSpan(0, 0, 0);
  11. }
  12. TimeSpan? ret = null;
  13. int index = iso8601.IndexOf("D");
  14. if (index > 0) {
  15. //日付部("P1D")が存在する場合
  16. string strDays = iso8601.Substring(0, index);
  17. TimeSpan days = new TimeSpan(int.Parse(strDays.Replace("P", "")), 0, 0, 0);
  18. ret = days;
  19.  
  20. //時間部が存在する場合
  21. index = iso8601.IndexOf("DT");
  22. if (index > 0) {
  23. TimeSpan other = XmlConvert.ToTimeSpan("PT" + iso8601.Substring(index + 2));
  24. ret = days.Add(other);
  25. }
  26. } else {
  27. ret = XmlConvert.ToTimeSpan(iso8601.Replace("P", "PT"));
  28. }
  29. return new TimeSpan(ret.Value.Days, ret.Value.Hours, ret.Value.Minutes, 0);
  30. }
  31. }

0 件のコメント:

コメントを投稿

厳選 Visual Studioの便利なショートカット

  エラー箇所にジャンプ 「Ctrl + Shift + F12」 ブレークポイント 設定/解除 「F9」 有効化/無効化 「Ctrl + F9」 ViEmu特有 「:ls」:バッファナンバーのリストを表示。 「:b2」:バッファ2のファイルを開く。 「:n」:次のバッファのファ...