public static DynamicDateFormatInfo GetInstance(string calendarModelId, IFormatProvider provider = null)
        {
            if (provider == null)
            {
                provider = CultureInfo.CurrentCulture;
            }
            string cInfId = calendarModelId + "_" + provider.ToString();

            if (infoCacheS.ContainsKey(cInfId))
            {
                return(infoCacheS[cInfId]);
            }
            DateTimeFormatInfo x = DateTimeFormatInfo.GetInstance(provider);
            var inf = new DynamicDateFormatInfo(x);

            if (string.IsNullOrEmpty(calendarModelId))
            {
                return((DynamicDateFormatInfo)inf.Clone());
            }

            var model = DynamicCalendarModel.GetCachedModel(calendarModelId);

            if (model != null && model.FormatInfo != null)
            {
                inf.CalendarModelID = calendarModelId;

                var mi = model.FormatInfo;
                inf.Import(mi);
            }

            return(inf);
        }
예제 #2
0
        internal static String Format(DynamicDate dDate, string format, DynamicDateFormatInfo ddfi)
        {
            if (dDate.IsOutOfTime)
            {
                return("OutATime");
            }

            if (format.Length == 1)
            {
                format = ExpandPredefinedFormat(format, ref ddfi);
            }

            if (format.StartsWith("_") && format.Length < 5)
            {
                format = DynamicDateFormatInfo.ExpandDynamicPredefinedFormat(format, ddfi);
            }

            return(FormatCustomized(dDate, format, ddfi));
        }
        public static List <DateFormatPattern> GetDateFormatPatternCultureSamples(IFormatProvider provider)
        {
            var res = new List <DateFormatPattern>();

            try
            {
                var dtfi = DateTimeFormatInfo.GetInstance(provider);
                var fi   = new DynamicDateFormatInfo(dtfi);

                res.Add(new DateFormatPattern(fi.ShortDatePattern, DateFormatPatternType.Date, DateFormatPatternLength.Short));
                res.Add(new DateFormatPattern(fi.MiddleDatePattern, DateFormatPatternType.Date, DateFormatPatternLength.Middle));
                res.Add(new DateFormatPattern(fi.LongDatePattern, DateFormatPatternType.Date, DateFormatPatternLength.Long));

                res.Add(new DateFormatPattern(fi.ShortDayPattern, DateFormatPatternType.Day, DateFormatPatternLength.Short));
                res.Add(new DateFormatPattern(fi.ShortMonthPattern, DateFormatPatternType.Month, DateFormatPatternLength.Short));
                res.Add(new DateFormatPattern(fi.ShortYearPattern, DateFormatPatternType.Year, DateFormatPatternLength.Short));
                res.Add(new DateFormatPattern(fi.ShortWeekDayDayPattern, DateFormatPatternType.WeekDayDay, DateFormatPatternLength.Short));
                res.Add(new DateFormatPattern(fi.ShortMonthDayPattern, DateFormatPatternType.MonthDay, DateFormatPatternLength.Short));
                res.Add(new DateFormatPattern(fi.ShortYearMonthPattern, DateFormatPatternType.YearMonth, DateFormatPatternLength.Short));

                res.Add(new DateFormatPattern(fi.MiddleDayPattern, DateFormatPatternType.Day, DateFormatPatternLength.Middle));
                res.Add(new DateFormatPattern(fi.MiddleMonthPattern, DateFormatPatternType.Month, DateFormatPatternLength.Middle));
                res.Add(new DateFormatPattern(fi.MiddleYearPattern, DateFormatPatternType.Year, DateFormatPatternLength.Middle));
                res.Add(new DateFormatPattern(fi.MiddleWeekDayDayPattern, DateFormatPatternType.WeekDayDay, DateFormatPatternLength.Middle));
                res.Add(new DateFormatPattern(fi.MiddleMonthDayPattern, DateFormatPatternType.MonthDay, DateFormatPatternLength.Middle));
                res.Add(new DateFormatPattern(fi.MiddleYearMonthPattern, DateFormatPatternType.YearMonth, DateFormatPatternLength.Middle));

                res.Add(new DateFormatPattern(fi.LongDayPattern, DateFormatPatternType.Day, DateFormatPatternLength.Long));
                res.Add(new DateFormatPattern(fi.LongMonthPattern, DateFormatPatternType.Month, DateFormatPatternLength.Long));
                res.Add(new DateFormatPattern(fi.LongYearPattern, DateFormatPatternType.Year, DateFormatPatternLength.Long));
                res.Add(new DateFormatPattern(fi.LongWeekDayDayPattern, DateFormatPatternType.WeekDayDay, DateFormatPatternLength.Long));
                res.Add(new DateFormatPattern(fi.LongMonthDayPattern, DateFormatPatternType.MonthDay, DateFormatPatternLength.Long));
                res.Add(new DateFormatPattern(fi.LongYearMonthPattern, DateFormatPatternType.YearMonth, DateFormatPatternLength.Long));
            }
            catch { }
            return(res);
        }
예제 #4
0
        // Expand a pre-defined format string (like "D" for long date) to the real format that
        // we are going to use in the date time parsing.
        // This method also convert the dateTime if necessary (e.g. when the format is in Universal time),
        // and change ddfi if necessary (e.g. when the format should use invariant culture).
        //
        private static String ExpandPredefinedFormat(String format, ref DynamicDateFormatInfo ddfi)
        {
            switch (format[0])
            {
            case 'o':
            case 'O':           // Round trip format
                ddfi = DynamicDateFormatInfo.InvariantInfo;
                break;

            case 'r':
            case 'R':           // RFC 1123 Standard
                ddfi = DynamicDateFormatInfo.InvariantInfo;
                break;

            case 's':           // Sortable without Time Zone Info
                ddfi = DynamicDateFormatInfo.InvariantInfo;
                break;

            case 'u':           // Universal time in sortable format.
                ddfi = DynamicDateFormatInfo.InvariantInfo;
                break;

            case 'U':           // Universal time in culture dependent format.
                // Universal time is always in Greogrian calendar.
                //
                // Change the Calendar to be Gregorian Calendar.
                //
                ddfi = (DynamicDateFormatInfo)ddfi.Clone();
                //if (ddfi.Calendar.GetType() != typeof(GregorianCalendar))
                {
                    //ddfi.Calendar = GregorianCalendar.GetDefaultInstance();
                }
                break;
            }
            format = GetRealFormat(format, ddfi);
            return(format);
        }
        public static string ExpandDynamicPredefinedFormat(String format, DynamicDateFormatInfo ddfi)
        {
            switch (format)
            {
            case "_sdt":
                return(ddfi.ShortDatePattern);

            case "_sd":
                return(ddfi.ShortDayPattern);

            case "_sM":
                return(ddfi.ShortMonthPattern);

            case "_sy":
                return(ddfi.ShortYearPattern);

            case "_swd":
                return(ddfi.ShortWeekDayDayPattern);

            case "_sMd":
                return(ddfi.ShortMonthDayPattern);

            case "_syM":
                return(ddfi.ShortYearMonthPattern);

            case "_mdt":
                return(ddfi.MiddleDatePattern);

            case "_md":
                return(ddfi.MiddleDayPattern);

            case "_mM":
                return(ddfi.MiddleMonthPattern);

            case "_my":
                return(ddfi.MiddleYearPattern);

            case "_mwd":
                return(ddfi.MiddleWeekDayDayPattern);

            case "_mMd":
                return(ddfi.MiddleMonthDayPattern);

            case "_myM":
                return(ddfi.MiddleYearMonthPattern);

            case "_ldt":
                return(ddfi.LongDatePattern);

            case "_ld":
                return(ddfi.LongDayPattern);

            case "_lM":
                return(ddfi.LongMonthPattern);

            case "_ly":
                return(ddfi.LongYearPattern);

            case "_lwd":
                return(ddfi.LongWeekDayDayPattern);

            case "_lMd":
                return(ddfi.LongMonthDayPattern);

            case "_lyM":
                return(ddfi.LongYearMonthPattern);
            }
            return(format);
        }
        public void Import(DynamicDateFormatInfo other)
        {
            if (other == null)
            {
                return;
            }
            if (other.OrderStyle != DateFormatOrderStyle.Default)
            {
                OrderStyle = other.OrderStyle;
            }
            if (!string.IsNullOrEmpty(other.ShortDatePattern))
            {
                ShortDatePattern = other.ShortDatePattern;
            }
            if (!string.IsNullOrEmpty(other.ShortDayPattern))
            {
                ShortDayPattern = other.ShortDayPattern;
            }
            if (!string.IsNullOrEmpty(other.ShortMonthPattern))
            {
                ShortMonthPattern = other.ShortMonthPattern;
            }
            if (!string.IsNullOrEmpty(other.ShortYearPattern))
            {
                ShortYearPattern = other.ShortYearPattern;
            }
            if (!string.IsNullOrEmpty(other.ShortWeekDayDayPattern))
            {
                ShortWeekDayDayPattern = other.ShortWeekDayDayPattern;
            }
            if (!string.IsNullOrEmpty(other.ShortMonthDayPattern))
            {
                ShortMonthDayPattern = other.ShortMonthDayPattern;
            }
            if (!string.IsNullOrEmpty(other.ShortYearMonthPattern))
            {
                ShortYearMonthPattern = other.ShortYearMonthPattern;
            }

            if (!string.IsNullOrEmpty(other.MiddleDatePattern))
            {
                MiddleDatePattern = other.MiddleDatePattern;
            }
            if (!string.IsNullOrEmpty(other.MiddleDayPattern))
            {
                MiddleDayPattern = other.MiddleDayPattern;
            }
            if (!string.IsNullOrEmpty(other.MiddleMonthPattern))
            {
                MiddleMonthPattern = other.MiddleMonthPattern;
            }
            if (!string.IsNullOrEmpty(other.MiddleYearPattern))
            {
                MiddleYearPattern = other.MiddleYearPattern;
            }
            if (!string.IsNullOrEmpty(other.MiddleWeekDayDayPattern))
            {
                MiddleWeekDayDayPattern = other.MiddleWeekDayDayPattern;
            }
            if (!string.IsNullOrEmpty(other.MiddleMonthDayPattern))
            {
                MiddleMonthDayPattern = other.MiddleMonthDayPattern;
            }
            if (!string.IsNullOrEmpty(other.MiddleYearMonthPattern))
            {
                MiddleYearMonthPattern = other.MiddleYearMonthPattern;
            }

            if (!string.IsNullOrEmpty(other.LongDatePattern))
            {
                LongDatePattern = other.LongDatePattern;
            }
            if (!string.IsNullOrEmpty(other.LongDayPattern))
            {
                LongDayPattern = other.LongDayPattern;
            }
            if (!string.IsNullOrEmpty(other.LongMonthPattern))
            {
                LongMonthPattern = other.LongMonthPattern;
            }
            if (!string.IsNullOrEmpty(other.LongYearPattern))
            {
                LongYearPattern = other.LongYearPattern;
            }
            if (!string.IsNullOrEmpty(other.LongWeekDayDayPattern))
            {
                LongWeekDayDayPattern = other.LongWeekDayDayPattern;
            }
            if (!string.IsNullOrEmpty(other.LongMonthDayPattern))
            {
                LongMonthDayPattern = other.LongMonthDayPattern;
            }
            if (!string.IsNullOrEmpty(other.LongYearMonthPattern))
            {
                LongYearMonthPattern = other.LongYearMonthPattern;
            }

            if (other.OverrideMaxYearDigits > 0)
            {
                OverrideMaxYearDigits = other.OverrideMaxYearDigits;
            }
            if (!string.IsNullOrEmpty(other.DateSeparator))
            {
                DateSeparator = other.DateSeparator;
            }

            if (!string.IsNullOrEmpty(other.RFC1123Pattern))
            {
                RFC1123Pattern = other.RFC1123Pattern;
            }
            if (!string.IsNullOrEmpty(other.SortableDatePattern))
            {
                SortableDatePattern = other.SortableDatePattern;
            }
        }
예제 #7
0
        private static String FormatCustomized(DynamicDate dDate, string format, DynamicDateFormatInfo ddfi)
        {
            StringBuilder result = new StringBuilder();

            int i = 0;
            int tokenLen;

            while (i < format.Length)
            {
                char ch = format[i];
                int  nextChar;
                switch (ch)
                {
                /*case 'g':
                 *  tokenLen = ParseRepeatPattern(format, i, ch);
                 *  result.Append(ddfi.GetEraName(cal.GetEra(dateTime)));
                 *  break;*/
                case 'd':
                    //
                    // tokenLen == 1 : Day of month as digits with no leading zero.
                    // tokenLen == 2 : Day of month as digits with leading zero for single-digit months.
                    // tokenLen == 3 : Day of week as a three-leter abbreviation.
                    // tokenLen >= 4 : Day of week as its full name.
                    //
                    tokenLen = ParseRepeatPattern(format, i, ch);
                    if (tokenLen <= 2)
                    {
                        FormatDigits(result, dDate.DayNumber, tokenLen);
                    }
                    else
                    {
                        if (tokenLen == 3)
                        {
                            result.Append(dDate.WeekDayNameShort);
                        }
                        else
                        {
                            result.Append(dDate.WeekDayNameFull);
                        }
                    }
                    break;

                case 'D':
                    //
                    // tokenLen == 1 : Day of month as digits with no leading zero.
                    // tokenLen == 2 : Day of month as digits with leading zero for single-digit months.
                    // tokenLen == 3 : Day of month as its short name.
                    // tokenLen >= 4 : Day of week as its full name.
                    //
                    tokenLen = ParseRepeatPattern(format, i, ch);
                    if (tokenLen <= 2)
                    {
                        FormatDigits(result, dDate.DayNumber, tokenLen);
                    }
                    else
                    {
                        if (tokenLen == 3)
                        {
                            result.Append(dDate.MonthDayNameShort);
                        }
                        else
                        {
                            result.Append(dDate.MonthDayNameFull);
                        }
                    }
                    break;

                case 'M':
                    //
                    // tokenLen == 1 : Month as digits with no leading zero.
                    // tokenLen == 2 : Month as digits with leading zero for single-digit months.
                    // tokenLen == 3 : Month as a three-letter abbreviation.
                    // tokenLen >= 4 : Month as its full name.
                    //
                    tokenLen = ParseRepeatPattern(format, i, ch);
                    if (tokenLen <= 2)
                    {
                        FormatDigits(result, dDate.MonthNumber, tokenLen);
                    }
                    else
                    {
                        if (tokenLen == 3)
                        {
                            result.Append(dDate.MonthNameShort);
                        }
                        else
                        {
                            result.Append(dDate.MonthNameFull);
                        }
                    }
                    break;

                case 'y':
                    //Info aus der DateTyme-Klasse und die letzte Zeile klingt falsch..
                    // Notes about OS behavior:
                    // y: Always print (year % 100). No leading zero.
                    // yy: Always print (year % 100) with leading zero.
                    // yyy: Always print (year % 1000) with leading zero.
                    // yyyy/yyyyy/... : Print year value. No leading zero.

                    int year = dDate.YearNumber;
                    tokenLen = ParseRepeatPattern(format, i, ch);
                    int iX = ddfi.OverrideMaxYearDigits > 0 ? Math.Min(tokenLen, ddfi.OverrideMaxYearDigits) : tokenLen;
                    if (iX <= 2)
                    {
                        FormatDigits(result, year % 100, iX);
                    }
                    else if (iX == 3)
                    {
                        FormatDigits(result, year % 1000, iX);
                    }
                    else
                    {
                        String fmtPattern = "D" + iX;
                        result.Append(year.ToString(fmtPattern, CultureInfo.InvariantCulture));
                    }
                    break;

                case ':':
                    //result.Append(ddfi.TimeSeparator);
                    tokenLen = 1;
                    break;

                case '/':
                    result.Append(ddfi.DateSeparator);
                    tokenLen = 1;
                    break;

                /*case '|':
                 *  tokenLen = 1;
                 *  nextChar = ParseNextChar(format, i);
                 *  if (nextChar == ch)
                 *  {
                 *      tokenLen = 2;
                 *      result.Append(DynamicDate.FormatLBMode ? "\n" : ", ");
                 *  } else
                 *      result.Append(DynamicDate.FormatLBMode ? '\n' : ' ');
                 *  break;*/
                case '\'':
                case '\"':
                    StringBuilder enquotedString = new StringBuilder();
                    tokenLen = ParseQuoteString(format, i, enquotedString);
                    result.Append(enquotedString);
                    break;

                case '&':
                    enquotedString = new StringBuilder();
                    tokenLen       = ParseQuoteString(format, i, enquotedString);
                    string c = "???";
                    switch (enquotedString.ToString())
                    {
                    case "MayaDate":
                        c = MayaCalc.Gregorian2Maya(dDate.UtcDate, dDate.Model.MayaCorrelation);
                        break;

                    case "MayaHaabKin":
                        c = MayaCalc.Gregorian2MayaHaabKin(dDate.UtcDate, dDate.Model.MayaCorrelation).ToString();
                        break;

                    case "MayaDateHaab":
                        c = MayaCalc.Gregorian2MayaHaab(dDate.UtcDate, dDate.Model.MayaCorrelation);
                        break;

                    case "MayaDateTzolkin":
                        c = MayaCalc.Gregorian2MayaTzolkin(dDate.UtcDate, dDate.Model.MayaCorrelation);
                        break;
                    }
                    result.Append(c);
                    break;

                case '%':
                    // Optional format character.
                    // For example, format string "%d" will print day of month
                    // without leading zero.  Most of the cases, "%" can be ignored.
                    nextChar = ParseNextChar(format, i);
                    // nextChar will be -1 if we already reach the end of the format string.
                    // Besides, we will not allow "%%" appear in the pattern.
                    if (nextChar >= 0 && nextChar != (int)'%')
                    {
                        result.Append(FormatCustomized(dDate, ((char)nextChar).ToString(), ddfi));
                        tokenLen = 2;
                    }
                    else
                    {
                        //
                        // This means that '%' is at the end of the format string or
                        // "%%" appears in the format string.
                        //
                        throw new FormatException("Format_InvalidString");
                    }
                    break;

                case '\\':
                    // Escaped character.  Can be used to insert character into the format string.
                    // For exmple, "\d" will insert the character 'd' into the string.
                    //
                    // NOTENOTE : we can remove this format character if we enforce the enforced quote
                    // character rule.
                    // That is, we ask everyone to use single quote or double quote to insert characters,
                    // then we can remove this character.
                    //
                    nextChar = ParseNextChar(format, i);
                    if (nextChar >= 0)
                    {
                        result.Append(((char)nextChar));
                        tokenLen = 2;
                    }
                    else
                    {
                        //
                        // This means that '\' is at the end of the formatting string.
                        //
                        throw new FormatException("Format_InvalidString");
                    }
                    break;

                default:
                    // NOTENOTE : we can remove this rule if we enforce the enforced quote
                    // character rule.
                    // That is, if we ask everyone to use single quote or double quote to insert characters,
                    // then we can remove this default block.
                    result.Append(ch);
                    tokenLen = 1;
                    break;
                }
                i += tokenLen;
            }
            return(result.ToString());
        }
예제 #8
0
        internal static String Format(DynamicDate dDate, string format, IFormatProvider provider)
        {
            DynamicDateFormatInfo ddfi = DynamicDateFormatInfo.GetInstance(dDate.ModelID, provider);

            return(Format(dDate, format, ddfi));
        }
예제 #9
0
        internal const String RoundtripDateTimeUnfixed = "yyyy'-'MM'-'ddT";   // HH':'mm':'ss zzz";

        internal static String GetRealFormat(String format, DynamicDateFormatInfo ddfi)
        {
            String realFormat = null;

            switch (format[0])
            {
            case 'd':           // Short Date
                realFormat = ddfi.MiddleDatePattern;
                break;

            case 'D':           // Long Date
                realFormat = ddfi.LongDatePattern;
                break;

            case 'f':                              // Full (long date + short time)
                realFormat = ddfi.LongDatePattern; // + " " + ddfi.;
                break;

            case 'F':                              // Full (long date + long time)
                realFormat = ddfi.LongDatePattern; //FullDateTimePattern;
                break;

            case 'g':           // General (short date + short time)
                realFormat = ddfi.MiddleDatePattern;
                break;

            case 'G':           // General (short date + long time)
                realFormat = ddfi.MiddleDatePattern;
                break;

            case 'm':
            case 'M':           // Month/Day Date
                realFormat = ddfi.LongMonthDayPattern;
                break;

            case 'o':
            case 'O':
                realFormat = RoundtripFormat;
                break;

            case 'r':
            case 'R':           // RFC 1123 Standard
                realFormat = ddfi.RFC1123Pattern;
                break;

            case 's':           // Sortable without Time Zone Info
                //realFormat = ddfi.SortableDateTimePattern;
                break;

            case 't':           // Short Time
                //realFormat = ddfi.ShortTimePattern;
                break;

            case 'T':           // Long Time
                //realFormat = ddfi.LongTimePattern;
                break;

            case 'u':           // Universal with Sortable format
                //realFormat = ddfi.UniversalSortableDateTimePattern;
                break;

            case 'U':           // Universal with Full (long date + long time) format
                //realFormat = ddfi.FullDateTimePattern;
                break;

            case 'y':
            case 'Y':           // Year/Month Date
                realFormat = ddfi.LongYearMonthPattern;
                break;

            default:
                throw new FormatException("Format_InvalidString");
            }
            return(realFormat);
        }