// TODO: move logic into GetDateTimeFormatPartsProcess public string TranslateDotNetToDev2Format(string originalFormat, out string error) { var getDateTimeFormatParts = new GetDateTimeFormatPartsProcess(originalFormat, _dateTimeFormatForwardLookupsForDotNet, _dateTimeFormatPartOptionsForDotNet); getDateTimeFormatParts.Execute(); getDateTimeFormatParts.Result = ReplaceToken(getDateTimeFormatParts.Result, "m", "Minutes"); getDateTimeFormatParts.Result = ReplaceToken(getDateTimeFormatParts.Result, "mm", "Minutes"); getDateTimeFormatParts.Result = ReplaceToken(getDateTimeFormatParts.Result, "M", "Month in single digit"); getDateTimeFormatParts.Result = ReplaceToken(getDateTimeFormatParts.Result, "MM", "Month in 2 digits"); getDateTimeFormatParts.Result = ReplaceToken(getDateTimeFormatParts.Result, "MMM", "Month text abbreviated"); getDateTimeFormatParts.Result = ReplaceToken(getDateTimeFormatParts.Result, "MMMM", "Month text in full"); getDateTimeFormatParts.Result = ReplaceToken(getDateTimeFormatParts.Result, "h", "Hours in 12 hour format"); getDateTimeFormatParts.Result = ReplaceToken(getDateTimeFormatParts.Result, "hh", "Hours in 12 hour format"); getDateTimeFormatParts.Result = ReplaceToken(getDateTimeFormatParts.Result, "H", "Hours in 24 hour format"); getDateTimeFormatParts.Result = ReplaceToken(getDateTimeFormatParts.Result, "HH", "Hours in 24 hour format"); getDateTimeFormatParts.Result = ReplaceToken(getDateTimeFormatParts.Result, "tt", "am or pm"); getDateTimeFormatParts.Result = ReplaceToken(getDateTimeFormatParts.Result, "ddd", "Day of Week text abbreviated"); getDateTimeFormatParts.Result = ReplaceToken(getDateTimeFormatParts.Result, "dddd", "Day of Week in full"); getDateTimeFormatParts.Result = ReplaceToken(getDateTimeFormatParts.Result, "fff", "Split Seconds: 987"); // // Get input format string for the dotnet parts // var dev2Format = new StringBuilder(""); foreach (IDateTimeFormatPartTO part in getDateTimeFormatParts.Result) { dev2Format.Append(part.Isliteral ? "'" + part.Value + "'" : part.Value); } error = getDateTimeFormatParts.Error; return(dev2Format.ToString()); }
public bool TryGetDateTimeFormatParts(string format, out List <IDateTimeFormatPartTO> formatParts, out string error) { var getDateTimeFormatParts = new GetDateTimeFormatPartsProcess(format, _dateTimeFormatForwardLookups, _dateTimeFormatPartOptions); var result = getDateTimeFormatParts.Execute(); formatParts = getDateTimeFormatParts.Result; error = getDateTimeFormatParts.Error; return(result); }
bool TryParse(string data, string inputFormat, bool parseAsTime, out IDateTimeResultTO result, out string error) { var nothingDied = true; result = new DateTimeResultTO(); error = ""; var originalInputFormat = inputFormat; var originalData = data; var culturesTried = 0; const int MaxAttempts = 8; if (string.IsNullOrWhiteSpace(data)) { originalData = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString(); } if (string.IsNullOrWhiteSpace(inputFormat)) { originalInputFormat = TranslateDotNetToDev2Format( GlobalConstants.Dev2DotNetDefaultDateTimeFormat.Replace("ss", "ss.fff"), out error); } else { culturesTried = MaxAttempts; } while (culturesTried <= MaxAttempts) { var dateTimeArray = originalData.ToArray(); var position = 0; // TODO: remove this allocation from the while loop var getDateTimeFormatParts = new GetDateTimeFormatPartsProcess(originalInputFormat, _dateTimeFormatForwardLookups, _dateTimeFormatPartOptions); nothingDied = getDateTimeFormatParts.Execute(); error = getDateTimeFormatParts.Error; if (!string.IsNullOrEmpty(error)) { return(false); } if (nothingDied) { TryCulture(parseAsTime, ref result, ref error, ref nothingDied, ref originalInputFormat, ref culturesTried, MaxAttempts, dateTimeArray, ref position, getDateTimeFormatParts.Result); } else { culturesTried++; } } return(nothingDied); }