/// <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; } }
/// <summary> /// Parses a dicom Date/Time string using the DateParser and TimeParser /// (TryParseExact) functions. The Hour/Minute adjustment factor (as /// specified in Dicom for universal time adjustment) is accounted for /// (and parsed) by this function. /// </summary> /// <param name="dicomDateTime">the dicom date/time string</param> /// <param name="dateTime">the date/time as a DateTime object</param> /// <returns>true on success, false otherwise</returns> public static bool Parse(string dicomDateTime, out DateTime dateTime) { dateTime = new DateTime(); if (String.IsNullOrEmpty(dicomDateTime)) { return(false); } int plusMinusIndex = dicomDateTime.IndexOfAny(_plusMinus); string dateTimeString = dicomDateTime; string offsetString = String.Empty; if (plusMinusIndex > 0) { offsetString = dateTimeString.Substring(plusMinusIndex); dateTimeString = dateTimeString.Remove(plusMinusIndex); } string dateString; if (dateTimeString.Length >= 8) { dateString = dateTimeString.Substring(0, 8); } else { return(false); } string timeString = String.Empty; if (dateTimeString.Length > 8) { timeString = dateTimeString.Substring(8); } int hourOffset = 0; int minuteOffset = 0; if (!String.IsNullOrEmpty(offsetString)) { if (offsetString.Length > 3) { if (!Int32.TryParse(offsetString.Substring(3), NumberStyles.Integer, CultureInfo.InvariantCulture, out minuteOffset)) { return(false); } if (!Int32.TryParse(offsetString.Remove(3), NumberStyles.Integer, CultureInfo.InvariantCulture, out hourOffset)) { return(false); } } else { if (!Int32.TryParse(offsetString, NumberStyles.Integer, CultureInfo.InvariantCulture, out hourOffset)) { return(false); } } minuteOffset *= Math.Sign(hourOffset); } DateTime date; if (!DateParser.Parse(dateString, out date)) { return(false); } DateTime time = new DateTime(); //zero datetime if (!String.IsNullOrEmpty(timeString)) { if (!TimeParser.Parse(timeString, out time)) { return(false); } } dateTime = date; dateTime = dateTime.AddTicks(time.Ticks); dateTime = dateTime.AddHours(hourOffset); dateTime = dateTime.AddMinutes(minuteOffset); return(true); }