public void SetFormat(string fmt) { FmtStr = fmt; Traits = new DateTraits(fmt.Contains("%y") || fmt.Contains("%Y"), fmt.Contains("%m") || fmt.Contains("%b"), fmt.Contains("%d")); ParseDotNetFmtStr = CTimeToNetFormatConverter.ConvertCTimeToNet(FmtStr, NetDateTimeFormat.ParseFormat); PrintDotNetFmtStr = CTimeToNetFormatConverter.ConvertCTimeToNet(FmtStr, NetDateTimeFormat.PrintFormat); }
/// <summary> /// Ported from parse_date_mask_routine /// </summary> public Date ParseDateMaskRoutine(string dateStr, DateIO io, out DateTraits traits) { if (dateStr != null && dateStr.Length > 127) { throw new DateError(String.Format(DateError.ErrorMessageInvalidDate, dateStr)); } string buf = dateStr; if (ConvertSeparatorsToSlashes) { buf = buf.Replace('.', '/').Replace('-', '/'); } Date when = io.Parse(buf); traits = default(DateTraits); if (!when.IsNotADate()) { Logger.Current.Debug(DebugTimesParse, () => String.Format("Passed date string: {0}", dateStr)); Logger.Current.Debug(DebugTimesParse, () => String.Format("Parsed date string: {0}", buf)); Logger.Current.Debug(DebugTimesParse, () => String.Format("Parsed result is: {0}", when)); Logger.Current.Debug(DebugTimesParse, () => String.Format("Formatted result is: {0}", io.Format(when))); string whenStr = io.Format(when); int indexP = 0; int indexQ = 0; for (; indexP < whenStr.Length && indexQ < buf.Length; indexP++, indexQ++) { if (whenStr[indexP] != buf[indexQ] && whenStr[indexP] == '0') { indexP++; } if (!(indexP < whenStr.Length) || whenStr[indexP] != buf[indexQ]) { break; } } if (indexP < whenStr.Length || indexQ < buf.Length) { throw new DateError(String.Format("Invalid date: {0}", dateStr)); } traits = io.Traits; if (!io.Traits.HasYear) { when = new Date(CurrentDate.Year, when.Month, when.Day); if (when.Month > CurrentDate.Month) { when = when.AddYears(-1); } } } return(when); }
/// <summary> /// Ported from parse_date_mask /// </summary> public Date ParseDateMask(string dateStr, out DateTraits traits) { foreach (DateIO reader in Readers) { Date when = ParseDateMaskRoutine(dateStr, reader, out traits); if (!when.IsNotADate()) { return(when); } } throw new DateError(String.Format(DateError.ErrorMessageInvalidDate, dateStr)); }