Exemplo n.º 1
0
        /// <summary>
        /// Check date for correct format and validness.
        /// </summary>
        /// <param name="input">String in the format yyyy.mm.dd</param>
        /// <param name="calendar">Indicates type of calendar, possible values SE_JUL_CAL and SE_GREG_CAL</param>
        /// <returns>Validated item</returns>
        public static ValidationItem checkDate(String input, int calendar)
        {
            ValidationItem vi     = new ValidationItem(input, false);
            bool           result = true;

            try {
                input        = input.Trim();
                vi.resultTxt = input;
                if (input.Length != 10)
                {
                    result = false;
                }
                else if (!Char.IsDigit(input[0]) ||
                         !Char.IsDigit(input[1]) ||
                         !Char.IsDigit(input[2]) ||
                         !Char.IsDigit(input[3]) ||
                         !input[4].Equals('.') ||
                         !Char.IsDigit(input[5]) ||
                         !Char.IsDigit(input[6]) ||
                         !input[7].Equals('.') ||
                         !Char.IsDigit(input[8]) ||
                         !Char.IsDigit(input[9]))
                {
                    result = false;
                }
                else
                {
                    int    d  = Convert.ToInt32(input.Substring(8, 2));
                    int    m  = Convert.ToInt32(input.Substring(5, 2));
                    int    y  = Convert.ToInt32(input.Substring(0, 4));
                    double ut = 0.0;
                    double jd = Sweph.getJD(y, m, d, ut, calendar);
                    if (d != Sweph.getDayFromJd(jd, calendar) || m < 1 || m > 12)
                    {
                        result = false;
                    }
                }
                vi.noErrors = result;
            }
            catch {
                vi.resultTxt = input;
                vi.noErrors  = false;
            }
            return(vi);
        }