/// <summary> /// Converts the specified <paramref name="input"/> string to its <see cref="EssentialsPartialDate"/> /// equivalent and returns a value that indicates whether the conversion succeeded. /// </summary> /// <param name="input">The input string to be converted.</param> /// <param name="provider">An object that supplies culture-specific formatting information about /// <paramref name="input"/>.</param> /// <param name="result">When this method returns, contains the <see cref="EssentialsPartialDate"/> value /// equivalent to the partial date and time contained in <paramref name="input"/>, if the conversion succeeded, /// or <c>null</c> if the conversion failed. The conversion fails if <paramref name="input"/> is /// <c>null</c>, is an empty string (""), or does not contain a valid string representation of a partial /// date. This parameter is passed uninitialized.</param> /// <returns><c>true</c> if <paramref name="input"/> was converted successfully; otherwise, /// <c>false</c>.</returns> public static bool TryParse(string input, IFormatProvider provider, out EssentialsPartialDate result) { // Initialize "date" result = null; // Strip all commas to make parsing easier input = (input ?? "").Replace(",", ""); // Parse the string into an instance of DateTime for full dates if (DateTime.TryParseExact(input, new[] { "yyyy-MM-dd", "d MMMM yyyy", "MMMM d yyyy" }, provider, DateTimeStyles.None, out DateTime dt)) { result = new EssentialsPartialDate(dt); return(true); } // Match various formats Match m1 = Regex.Match(input, "^([0-9]{4})$"); Match m2 = Regex.Match(input, "^([0-9]{4})-([0-9]{2})$"); Match m3 = Regex.Match(input, "^([0-9]{4})-([0-9]{2})-([0-9]{2})$"); Match m4 = Regex.Match(input, "^([a-zA-Z]+) ([0-9]{4})$"); Match m6 = Regex.Match(input, "^([a-zA-Z]+) ([0-9]{1,2})(st|nd|rd|th) ([0-9]{4})$"); if (m1.Success) { result = new EssentialsPartialDate(Int32.Parse(m1.Groups[1].Value)); } if (m2.Success) { result = new EssentialsPartialDate(Int32.Parse(m2.Groups[1].Value), Int32.Parse(m2.Groups[2].Value)); } if (m3.Success) { result = new EssentialsPartialDate(Int32.Parse(m3.Groups[1].Value), Int32.Parse(m3.Groups[2].Value), Int32.Parse(m3.Groups[3].Value)); } if (m4.Success) { if (!TimeUtils.TryParseNumberFromMonthName(m4.Groups[1].Value, provider, out int month)) { return(false); } int year = Int32.Parse(m4.Groups[2].Value); result = new EssentialsPartialDate(year, month); return(true); } if (m6.Success) { if (!TimeUtils.TryParseNumberFromMonthName(m6.Groups[1].Value, provider, out int month)) { return(false); } int year = Int32.Parse(m6.Groups[4].Value); int day = Int32.Parse(m6.Groups[2].Value); result = new EssentialsPartialDate(year, month, day); return(true); } return(result != null); }
internal static object ToFormat(EssentialsPartialDate value, TimeFormat format) { switch (format) { case TimeFormat.Iso8601: return(value.ToString()); default: throw new ArgumentException("Unsupported format " + format, nameof(format)); } }
/// <summary> /// Converts the specified <paramref name="input"/> string to its <see cref="EssentialsPartialDate"/> /// equivalent and returns a value that indicates whether the conversion succeeded. /// </summary> /// <param name="input">The input string to be converted.</param> /// <param name="result">When this method returns, contains the <see cref="EssentialsPartialDate"/> value /// equivalent to the partial date and time contained in <paramref name="input"/>, if the conversion succeeded, /// or <c>null</c> if the conversion failed. The conversion fails if <paramref name="input"/> is /// <c>null</c>, is an empty string (""), or does not contain a valid string representation of a partial /// date. This parameter is passed uninitialized.</param> /// <returns><c>true</c> if <paramref name="input"/> was converted successfully; otherwise, /// <c>false</c>.</returns> public static bool TryParse(string input, out EssentialsPartialDate result) { return(TryParse(input, CultureInfo.InvariantCulture, out result)); }