Ejemplo n.º 1
0
        private static string FormatDate(DateTime dateTime, FormatParametersList formatParameters)
        {
            var locale = formatParameters.GetParamValue(LOCALE);
            DateTimeFormatInfo df;

            if (locale != null)
            {
                // culture codes in 1C-style
                var culture = CreateCulture(locale);
                df = (DateTimeFormatInfo)culture.DateTimeFormat.Clone();
            }
            else
            {
                var currentDF = DateTimeFormatInfo.CurrentInfo;
                if (currentDF == null)
                {
                    df = new DateTimeFormatInfo();
                }
                else
                {
                    df = (DateTimeFormatInfo)currentDF.Clone();
                }
            }

            string param;

            if (dateTime == DateTime.MinValue)
            {
                if (formatParameters.HasParam(DATE_EMPTY, out param))
                {
                    return(param);
                }

                return(String.Empty);
            }

            string formatString = "G";

            if (formatParameters.HasParam(DATE_FORMAT, out param))
            {
                formatString = ProcessDateFormat(param);
            }

            if (formatParameters.HasParam(DATE_LOCAL_FORMAT, out param))
            {
                formatString = ProcessLocalDateFormat(param);
            }

            return(dateTime.ToString(formatString, df));
        }
Ejemplo n.º 2
0
        public string NStr(string src, string lang = null)
        {
            var    parser = new FormatParametersList(src);
            string str;

            if (lang == null)
            {
                str = parser.EnumerateValues().FirstOrDefault();
            }
            else
            {
                str = parser.GetParamValue(lang);
            }

            return(str == null ? string.Empty : str);
        }
Ejemplo n.º 3
0
        private static string FormatBoolean(bool p, FormatParametersList formatParameters)
        {
            if (p)
            {
                var truePresentation = formatParameters.GetParamValue(BOOLEAN_TRUE);
                if (truePresentation != null)
                {
                    return(truePresentation);
                }
            }
            else
            {
                var falsePresentation = formatParameters.GetParamValue(BOOLEAN_FALSE);
                if (falsePresentation != null)
                {
                    return(falsePresentation);
                }
            }

            return(ValueFactory.Create(p).AsString());
        }
Ejemplo n.º 4
0
 private static string DefaultFormat(IValue value, FormatParametersList formatParameters)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 5
0
        private static string FormatNumber(decimal p, FormatParametersList formatParameters)
        {
            var locale = formatParameters.GetParamValue(LOCALE);
            NumberFormatInfo nf;

            if (locale != null)
            {
                // culture codes in 1C-style
                var culture = CreateCulture(locale);
                nf = (NumberFormatInfo)culture.NumberFormat.Clone();
            }
            else
            {
                nf = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
            }

            string param;

            if (p == 0)
            {
                if (formatParameters.HasParam(NUM_ZERO_APPEARANCE, out param))
                {
                    if (param == "")
                    {
                        return("0");
                    }
                    else
                    {
                        return(param);
                    }
                }
                else
                {
                    return("");
                }
            }

            bool hasDigitLimits = false;
            int  totalDigits    = 0;
            int  fractionDigits = 0;

            if (formatParameters.HasParam(NUM_MAX_SIZE, out param))
            {
                int paramToInt;
                if (Int32.TryParse(param, out paramToInt))
                {
                    if (paramToInt < 0)
                    {
                        paramToInt = 0;
                    }

                    hasDigitLimits = true;
                    totalDigits    = paramToInt;
                }
            }

            if (formatParameters.HasParam(NUM_DECIMAL_SIZE, out param))
            {
                int paramToInt;
                if (Int32.TryParse(param, out paramToInt))
                {
                    if (paramToInt < 0)
                    {
                        paramToInt = 0;
                    }

                    hasDigitLimits = true;
                    fractionDigits = paramToInt;
                }
            }

            if (formatParameters.HasParam(NUM_FRACTION_DELIMITER, out param))
            {
                nf.NumberDecimalSeparator = param;
            }

            if (formatParameters.HasParam(NUM_GROUPS_DELIMITER, out param))
            {
                nf.NumberGroupSeparator = param;
            }
            else
            {
                nf.NumberGroupSeparator = " ";
            }

            if (formatParameters.HasParam(NUM_GROUPING, out param))
            {
                nf.NumberGroupSizes = ParseGroupSizes(param);
            }

            if (formatParameters.HasParam(NUM_NEGATIVE_APPEARANCE, out param))
            {
                int pattern;
                if (int.TryParse(param, out pattern))
                {
                    nf.NumberNegativePattern = pattern;
                }
            }

            char leadingFormatSpecifier = '#';

            if (formatParameters.HasParam(NUM_LEADING_ZERO, out param))
            {
                leadingFormatSpecifier = '0';
            }

            StringBuilder formatBuilder = new StringBuilder();

            if (hasDigitLimits)
            {
                ApplyNumericSizeRestrictions(ref p, totalDigits, fractionDigits);

                int repeatCount = totalDigits - fractionDigits;
                if (repeatCount < 0)
                {
                    repeatCount = 1;
                }

                formatBuilder.Append(leadingFormatSpecifier, repeatCount);

                ApplyDigitsGrouping(formatBuilder, nf);

                formatBuilder.Append('.');
                formatBuilder.Append('#', fractionDigits);
            }
            else
            {
                int precision = GetDecimalPrecision(Decimal.GetBits(p));
                nf.NumberDecimalDigits = precision;
                formatBuilder.Append("N");
            }

            return(p.ToString(formatBuilder.ToString(), nf));
        }