Exemplo n.º 1
0
        /// <summary>
        /// Converts the string representation of a date format to its Calendar date equivalent.
        /// </summary>
        /// <param name="date">A string containing a date format to convert.</param>
        /// <param name="calendarType">a instance of calendar that date string based on it</param>
        /// <returns>Calendar equivalent to the  date contained in date.</returns>
        public static ICalendar Parse(string date, ICalendar calendarType)
        {
            if (date == null)
            {
                throw new ArgumentNullException(nameof(date));
            }
            if (calendarType == null)
            {
                throw new ArgumentNullException(nameof(calendarType));
            }
            date = date.ToLower().Trim();
            if (date.Length < 6)
            {
                throw new Exception("Format of string is not a date format!");
            }
            if (calendarType is GregorianDate)
            {
                DateTime    dt;
                CultureInfo cultureInfo = new CultureInfo("en-US");
                DateTime.TryParse(date, cultureInfo, DateTimeStyles.None, out dt);
                return(new GregorianDate(dt));
            }
            var tmp = date.Replace('\\', '/').Replace("-", "/").Replace(".", "/").Replace("_", "/").Split('/');

            if (tmp[2].Length > tmp[0].Length)
            {
                calendarType.Year  = Convert.ToInt32(tmp[2]);
                calendarType.Month = calendarType.GetMonthInfo(Convert.ToInt32(tmp[1]));
                calendarType.Day   = Convert.ToInt32(tmp[0]);
            }
            else
            {
                calendarType.Year  = Convert.ToInt32(tmp[0]);
                calendarType.Month = calendarType.GetMonthInfo(Convert.ToInt32(tmp[1]));
                calendarType.Day   = Convert.ToInt32(tmp[2]);
            }
            return(calendarType);
        }