/// <summary> /// Parses Dicom Date/Time tags. The <paramref name="dicomDateTime"/> would be a DateTime tag value - such as AcquisitionDatetime, /// the <paramref name="dicomDate"/> would be just a Date tag value - such as AcquisitionDate; and <paramref name="dicomTime"/> would /// be just the Time tag value - such as AcquisitionTime. So, this method will parse the <paramref name="dicomDateTime"/> if it is not empty, /// otherwise it will parse the <paramref name="dicomDate"/> and <paramref name="dicomTime"/> together. /// </summary> /// <param name="dicomDateTime">The dicom date time.</param> /// <param name="dicomDate">The dicom date.</param> /// <param name="dicomTime">The dicom time.</param> /// <param name="outDateTime">The date time.</param> /// <returns></returns> public static bool ParseDateAndTime(string dicomDateTime, string dicomDate, string dicomTime, out DateTime outDateTime) { outDateTime = DateTime.MinValue; // set default value string dateTimeValue = dicomDateTime == null ? String.Empty : dicomDateTime.Trim(); // First try to do dateValue and timeValue separately - if both are there then set // dateTimeConcat, and then parse dateTimeConcat the same as if dateTimeValue was set if (dateTimeValue != String.Empty) { return(Parse(dateTimeValue, out outDateTime)); } string dateValue = dicomDate == null ? String.Empty : dicomDate.Trim(); string timeValue = dicomTime == null ? String.Empty : dicomTime.Trim(); if (dateValue == String.Empty) { return(false); } DateTime date; if (!DateParser.Parse(dateValue, out date)) { return(false); } outDateTime = date; if (timeValue == String.Empty) { return(true); } //Even though we have the date, the time is wrong. The "out" parameter will still have the parsed date in it if anyone cares. DateTime time; if (!TimeParser.Parse(timeValue, out time)) { return(false); } outDateTime = outDateTime.Add(time.TimeOfDay); return(true); }
/// <summary> /// Will parse a date range adhering to the dicom date format, returning the dates as <see cref="DateTime"/> objects. /// </summary> /// <param name="dateRange">the string to be parsed</param> /// <param name="fromDate">the "from date", or null</param> /// <param name="toDate">the "to date" or null</param> /// <param name="isRange">whether or not the input value was actually a range. If not, then the "from date" value should be taken /// to be an exact value, not a range, depending on the application.</param> /// <exception cref="InvalidOperationException">if the input range is poorly formatted</exception> public static void Parse(string dateRange, out DateTime?fromDate, out DateTime?toDate, out bool isRange) { try { fromDate = null; toDate = null; isRange = false; if (dateRange == null) { return; } string fromDateString = "", toDateString = ""; string[] splitRange = dateRange.Split('-'); if (splitRange.Length == 1) { fromDateString = splitRange[0]; } else if (splitRange.Length == 2) { fromDateString = splitRange[0]; toDateString = splitRange[1]; isRange = true; } else { throw new InvalidOperationException(string.Format(SR.ExceptionPoorlyFormattedDateRange, dateRange)); } DateTime outDate; if (fromDateString == "") { fromDate = null; } else { if (!DateParser.Parse(fromDateString, out outDate)) { throw new InvalidOperationException(string.Format(SR.ExceptionPoorlyFormattedDateRange, dateRange)); } fromDate = outDate; } if (toDateString == "") { toDate = null; } else { if (!DateParser.Parse(toDateString, out outDate)) { throw new InvalidOperationException(string.Format(SR.ExceptionPoorlyFormattedDateRange, dateRange)); } toDate = outDate; } if (fromDate != null && toDate != null) { if (fromDate > toDate) { throw new InvalidOperationException(string.Format(SR.ExceptionPoorlyFormattedDateRange, dateRange)); } } } catch { fromDate = toDate = null; throw; } }