Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        /// <summary>
        /// Ported from format_date
        /// </summary>
        public string FormatDate(Date when, FormatTypeEnum formatType = FormatTypeEnum.FMT_PRINTED, string format = null)
        {
            if (formatType == FormatTypeEnum.FMT_WRITTEN)
            {
                return(WrittenDateIO.Format(when));
            }
            else if (formatType == FormatTypeEnum.FMT_CUSTOM && format != null)
            {
                DateIO dateIO;
                if (TempDateIO.TryGetValue(format, out dateIO))
                {
                    return(dateIO.Format(when));
                }
                else
                {
                    dateIO = new DateIO(format, false);
                    TempDateIO.Add(format, dateIO);
                    return(dateIO.Format(when));
                }
            }
            else if (formatType == FormatTypeEnum.FMT_PRINTED)
            {
                return(PrintedDateIO.Format(when));
            }

            throw new InvalidOperationException("formatter");
        }
Beispiel #3
0
        public void TimesInitialize()
        {
            if (!IsInitialized)
            {
                InputDateTimeIO   = new DateTimeIO("%Y/%m/%d %H:%M:%S", true);
                TimelogDateTimeIO = new DateTimeIO("%m/%d/%Y %H:%M:%S", true);

                WrittenDateTimeIO = new DateTimeIO("%Y/%m/%d %H:%M:%S", false);
                WrittenDateIO     = new DateIO("%Y/%m/%d", false);

                PrintedDateTimeIO = new DateTimeIO("%y-%b-%d %H:%M:%S", false);
                PrintedDateIO     = new DateIO("%y-%b-%d", false);

                Readers.Add(new DateIO("%m/%d", true));
                Readers.Add(new DateIO("%Y/%m/%d", true));
                Readers.Add(new DateIO("%Y/%m", true));
                Readers.Add(new DateIO("%y/%m/%d", true));
                Readers.Add(new DateIO("%Y-%m-%d", true));

                IsInitialized = true;
            }
        }