/// <summary> /// Creates a vague date string from a vague date instance containing a date type start and end dates /// expressed as days elapsed since 30/12/1899. /// </summary> /// <param name="vd">a VagueDateInstance with the date to be converted.</param> /// <param name="outputFormat">A vague date string in the format specified in the dateType parameter.</param> /// <returns></returns> public static string FromVagueDateInstance(VagueDateInstance vd, DateType outputFormat) { return FromTimeSpanDays(vd.StartDate, vd.EndDate, vd.DateType, outputFormat); }
/// <summary> /// Creates a vague date string from a vague date instance containing a date type start and end dates /// expressed as days elapsed since 30/12/1899. /// </summary> /// <param name="vd">a VagueDateInstance with the date to be converted.</param> /// <param name="outputFormat">A vague date string in the format specified in the dateType parameter.</param> /// <returns></returns> public static string FromVagueDateInstance(VagueDateInstance vd, DateType outputFormat) { return(FromTimeSpanDays(vd.StartDate, vd.EndDate, vd.DateType, outputFormat)); }
//--------------------------------------------------------------------- // FIX: 048 Enable fields to be exported using a different // data type. // /// <summary> /// Converts the input field into the output field, applying any /// required formatting as appropriate. /// </summary> /// <param name="inOrdinal">The input field ordinal.</param> /// <param name="inValue">The input field value.</param> /// <param name="inType">Data type of the input field.</param> /// <param name="outType">Date type of the output field.</param> /// <param name="outFormat">The required output field format.</param> /// <param name="sourceDateStart">The source date start.</param> /// <param name="sourceDateEnd">The source date end.</param> /// <param name="sourceDateType">The source date type.</param> /// <returns></returns> private object ConvertInput(int inOrdinal, object inValue, System.Type inType, System.Type outType, string outFormat, int sourceDateStart, int sourceDateEnd, string sourceDateType) { // If the output field is a DateTime. if (outType == System.Type.GetType("System.DateTime")) { // If the input field is also a DateTime if (inType == System.Type.GetType("System.DateTime")) { // Returns the value as a DateTime. if (inValue is DateTime) return inValue; else return null; } // If the input field is an integer and is part of // the source date. else if ((inType == System.Type.GetType("System.Int32")) && (_sourceDateStartOrdinals.Contains(inOrdinal) || _sourceDateEndOrdinals.Contains(inOrdinal))) { // Convert the value to an integer. int inInt = (int)inValue; // Convert the value to a vague date instance. string vt = "D"; VagueDateInstance vd = new VagueDateInstance(inInt, inInt, vt); // If the vague date is invalid then return null. if ((vd == null) || (vd.IsBad) || (vd.IsUnknown)) return null; else { // If the vague date is valid then parse it into // a date format. string itemStr = VagueDate.FromVagueDateInstance(vd, VagueDate.DateType.Vague); DateTime inDate; if (DateTime.TryParseExact(itemStr, "dd/MM/yyyy", null, DateTimeStyles.None, out inDate)) return inDate; else return null; } } else { // Otherwise, try and parse the input value as // if it was a date string and return the date value // if it is valid, or the raw value if not. string inStr = inValue.ToString(); DateTime inDate; if (DateTime.TryParseExact(inStr, "dd/MM/yyyy", null, DateTimeStyles.None, out inDate)) return inDate; else return null; } } // If the output field is a string and there is // a required output format. else if ((outType == System.Type.GetType("System.String")) && (outFormat != null)) { // If the input field is a DateTime field. if (inType == System.Type.GetType("System.DateTime")) { // If the input value is a valid DateTime then // convert it to a string of the required format. if (inValue is DateTime) { DateTime inDate = (DateTime)inValue; string inStr = inDate.ToString(outFormat); if (inStr != null) return inStr; else return null; //// If the date string formats exactly then return //// the formatted value. //if (DateTime.TryParseExact(inDate.ToString(), outFormat, // null, DateTimeStyles.None, out inDate)) // return inDate.ToString(); //else // return null; } else return null; } //--------------------------------------------------------------------- // CHANGED: CR17 (Exporting date fields) // Convert source dates into a text field with the required // date format. // // If the input field is an integer and is part of // the source date. else if ((inType == System.Type.GetType("System.Int32")) && (_sourceDateStartOrdinals.Contains(inOrdinal) || _sourceDateEndOrdinals.Contains(inOrdinal))) { // Convert the value to an integer. int inInt = (int)inValue; // Convert the value to a vague date instance. VagueDateInstance vd = new VagueDateInstance(sourceDateStart, sourceDateEnd, sourceDateType); // If the vague date is invalid then set the output // field to null. if ((vd == null) || (vd.IsBad)) return null; else if (vd.IsUnknown) return VagueDate.VagueDateTypes.Unknown.ToString(); else { // If the output format is 'v' or 'V' then format the dates according // to the source date type. if (outFormat.ToLower() == "v") { return VagueDate.FromVagueDateInstance(vd, VagueDate.DateType.Vague); } // If the output format is blank then format the start or end date // according to the source date type. else if (String.IsNullOrEmpty(outFormat)) { if (_sourceDateStartOrdinals.Contains(inOrdinal)) { return VagueDate.FromVagueDateInstance(vd, VagueDate.DateType.Start); } else if (_sourceDateEndOrdinals.Contains(inOrdinal)) { return VagueDate.FromVagueDateInstance(vd, VagueDate.DateType.End); } else return null; } // If the output format is a vague date type then format it // like that. else if (VagueDate.FromCode(outFormat) != VagueDate.VagueDateTypes.Unknown) { // If the field is a start date then use the first character of // the vague date type. if (_sourceDateStartOrdinals.Contains(inOrdinal)) { // Set the date type applicable to the source date. string dateType = outFormat.Substring(0, 1); return VagueDate.FromVagueDateInstance(new VagueDateInstance(sourceDateStart, sourceDateEnd, dateType), VagueDate.DateType.Start); } // If the field is an end date then use the last character of // the vague date type. else if (_sourceDateEndOrdinals.Contains(inOrdinal)) { // Set the date type applicable to the source date. vd.DateType = outFormat.Length == 1 ? outFormat + outFormat : outFormat; return VagueDate.FromVagueDateInstance(vd, VagueDate.DateType.End); } else return null; } // If the output format is not a vague date type then format it // as if it is a standard date format (e.g. dd/MM/yyyy). else { // Parse the date into a date format using the output format. VagueDate.DateType dateType = VagueDate.DateType.Vague; if (_sourceDateStartOrdinals.Contains(inOrdinal)) dateType = VagueDate.DateType.Start; else if (_sourceDateStartOrdinals.Contains(inOrdinal)) dateType = VagueDate.DateType.Start; string inStr = VagueDate.FromVagueDateInstance(new VagueDateInstance(sourceDateStart, sourceDateEnd, "D"), dateType); DateTime inDate; if (!DateTime.TryParseExact(inStr, "dd/MM/yyyy", null, DateTimeStyles.None, out inDate)) return null; // Convert the DateTime to a string of the output format. string outDate; outDate = inDate.ToString(outFormat); // Parse the formatted date back into a date using the // output format to check it is a valid date. DateTime inDateAgain; if (!DateTime.TryParseExact(outDate, outFormat, null, DateTimeStyles.None, out inDateAgain) || (inDate != inDateAgain)) return null; else return outDate; } } } //--------------------------------------------------------------------- else { // Otherwise, try and parse the input value as // if it was a date string and return the value // as a string if it is valid. string inStr = inValue.ToString(); DateTime inDate; if (DateTime.TryParse(inStr, null, DateTimeStyles.None, out inDate)) return inDate.ToString(); else { // If the input is an IHS code that is blank (i.e. only the // separator character is retrieved) then return null. if (inValue.ToString() == " : ") return null; else return inValue; } } } else return inValue; }