Пример #1
0
        public override StringBuilder Format(long numberL, StringBuilder toAppendTo,
                                             FieldPosition pos)
        {
            if (numberL < 0)
            {
                // negative
                toAppendTo.Append(minusSign);
            }

            // Note: NumberFormat used by DateFormat only uses int numbers.
            // Remainder operation on 32bit platform using long is significantly
            // slower
            // than int. So, this method casts long number into int.
            int number = (int)numberL;

            int limit = (decimalBuf.Length < maxIntDigits) ? decimalBuf.Length
                        : maxIntDigits;
            int index = limit - 1;

            while (true)
            {
                decimalBuf[index] = (char)((number % 10) + zeroDigit);
                number           /= 10;
                if (index == 0 || number == 0)
                {
                    break;
                }
                index--;
            }
            int padding = minIntDigits - (limit - index);

            for (; padding > 0; padding--)
            {
                decimalBuf[--index] = zeroDigit;
            }
            int length = limit - index;

            toAppendTo.Append(decimalBuf, index, length);
            pos.SetBeginIndex(0);
            if (pos.GetField() == IBM.ICU.Text.NumberFormat.INTEGER_FIELD)
            {
                pos.SetEndIndex(length);
            }
            else
            {
                pos.SetEndIndex(0);
            }
            return(toAppendTo);
        }
Пример #2
0
 /// <summary>
 /// Adds a new FieldContainer with MessageFormat.Field.ARGUMENT field,
 /// argnumber, begin and end index to the fields vector, or sets the
 /// position's begin and end index if it has MessageFormat.Field.ARGUMENT as
 /// its field attribute.
 /// </summary>
 ///
 /// <param name="begin"></param>
 /// <param name="end"></param>
 /// <param name="argnumber"></param>
 /// <param name="position"></param>
 /// <param name="fields"></param>
 private void HandleArgumentField(int begin, int end, int argnumber,
                                  FieldPosition position, List <FieldContainer> fields)
 {
     if (fields != null)
     {
         fields.Add(new MessageFormat.FieldContainer(begin, end, MessageFormat.Field.ARGUMENT,
                                                     ((int)(argnumber))));
     }
     else
     {
         if (position != null &&
             (Object)position.GetFieldAttribute() == (Object)MessageFormat.Field.ARGUMENT &&
             position.GetEndIndex() == 0)
         {
             position.SetBeginIndex(begin);
             position.SetEndIndex(end);
         }
     }
 }
Пример #3
0
        private void Append(StringBuilder buffer, FieldPosition position,
                            List <FieldPosition> fields, char format, int count)
        {
            int field = -1;
            int index = patternChars.IndexOf(format);

            if (index == -1)
            {
                // text.03=Unknown pattern character - '{0}'
                throw new ArgumentException("text.03" + format);                 //$NON-NLS-1$
            }

            int beginPosition = buffer.Length;

            DateFormat.Field dateFormatField = null;
            System.Console.Out.WriteLine("index:" + index);
            switch (index)
            {
            case DateFormat.ERA_FIELD:
                dateFormatField = DateFormat.Field.ERA;
                buffer.Append(formatData.eras[calendar.Get(ILOG.J2CsMapping.Util.Calendar.ERA)]);
                break;

            case DateFormat.YEAR_FIELD:
                dateFormatField = DateFormat.Field.YEAR;
                int year = calendar.Get(ILOG.J2CsMapping.Util.Calendar.YEAR);
                if (count < 4)
                {
                    AppendNumber(buffer, 2, year %= 100);
                }
                else
                {
                    AppendNumber(buffer, count, year);
                }
                break;

            case DateFormat.MONTH_FIELD:
                dateFormatField = DateFormat.Field.MONTH;
                int month = calendar.Get(ILOG.J2CsMapping.Util.Calendar.MONTH);
                if (count <= 2)
                {
                    AppendNumber(buffer, count, month + 1);
                }
                else if (count == 3)
                {
                    buffer.Append(formatData.shortMonths[month]);
                }
                else
                {
                    buffer.Append(formatData.months[month]);
                }
                break;

            case DateFormat.DATE_FIELD:
                dateFormatField = DateFormat.Field.DAY_OF_MONTH;
                field           = ILOG.J2CsMapping.Util.Calendar.DATE;
                break;

            case DateFormat.HOUR_OF_DAY1_FIELD:             // k
                dateFormatField = DateFormat.Field.HOUR_OF_DAY1;
                int hour = calendar.Get(ILOG.J2CsMapping.Util.Calendar.HOUR_OF_DAY);
                AppendNumber(buffer, count, (hour == 0) ? 24 : hour);
                break;

            case DateFormat.HOUR_OF_DAY0_FIELD:             // H
                dateFormatField = DateFormat.Field.HOUR_OF_DAY0;
                field           = ILOG.J2CsMapping.Util.Calendar.HOUR_OF_DAY;
                break;

            case DateFormat.MINUTE_FIELD:
                dateFormatField = DateFormat.Field.MINUTE;
                field           = ILOG.J2CsMapping.Util.Calendar.MINUTE;
                break;

            case DateFormat.SECOND_FIELD:
                dateFormatField = DateFormat.Field.SECOND;
                field           = ILOG.J2CsMapping.Util.Calendar.SECOND;
                break;

            case DateFormat.MILLISECOND_FIELD:
                dateFormatField = DateFormat.Field.MILLISECOND;
                int value_ren = calendar.Get(ILOG.J2CsMapping.Util.Calendar.MILLISECOND);
                AppendNumber(buffer, count, value_ren);
                break;

            case DateFormat.DAY_OF_WEEK_FIELD:
                dateFormatField = DateFormat.Field.DAY_OF_WEEK;
                int day = calendar.Get(ILOG.J2CsMapping.Util.Calendar.DAY_OF_WEEK);
                if (count < 4)
                {
                    buffer.Append(formatData.shortWeekdays[day]);
                }
                else
                {
                    buffer.Append(formatData.weekdays[day]);
                }
                break;

            case DateFormat.DAY_OF_YEAR_FIELD:
                dateFormatField = DateFormat.Field.DAY_OF_YEAR;
                field           = ILOG.J2CsMapping.Util.Calendar.DAY_OF_YEAR;
                break;

            case DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD:
                dateFormatField = DateFormat.Field.DAY_OF_WEEK_IN_MONTH;
                field           = ILOG.J2CsMapping.Util.Calendar.DAY_OF_WEEK_IN_MONTH;
                break;

            case DateFormat.WEEK_OF_YEAR_FIELD:
                dateFormatField = DateFormat.Field.WEEK_OF_YEAR;
                field           = ILOG.J2CsMapping.Util.Calendar.WEEK_OF_YEAR;
                break;

            case DateFormat.WEEK_OF_MONTH_FIELD:
                dateFormatField = DateFormat.Field.WEEK_OF_MONTH;
                field           = ILOG.J2CsMapping.Util.Calendar.WEEK_OF_MONTH;
                break;

            case DateFormat.AM_PM_FIELD:
                dateFormatField = DateFormat.Field.AM_PM;
                buffer.Append(formatData.ampms[calendar.Get(ILOG.J2CsMapping.Util.Calendar.AM_PM)]);
                break;

            case DateFormat.HOUR1_FIELD:             // h
                dateFormatField = DateFormat.Field.HOUR1;
                hour            = calendar.Get(ILOG.J2CsMapping.Util.Calendar.HOUR);
                AppendNumber(buffer, count, (hour == 0) ? 12 : hour);
                break;

            case DateFormat.HOUR0_FIELD:             // K
                dateFormatField = DateFormat.Field.HOUR0;
                field           = ILOG.J2CsMapping.Util.Calendar.HOUR;
                break;

            case DateFormat.TIMEZONE_FIELD:             // z
                dateFormatField = DateFormat.Field.TIME_ZONE;
                AppendTimeZone(buffer, count, true);
                break;

            case IBM.ICU.Text.DateFormat.TIMEZONE_RFC_FIELD:             // Z
                dateFormatField = DateFormat.Field.TIME_ZONE;
                AppendTimeZone(buffer, count, false);
                break;
            }
            if (field != -1)
            {
                AppendNumber(buffer, count, calendar.Get(field));
            }

            if (fields != null)
            {
                position = new FieldPosition(dateFormatField);
                position.SetBeginIndex(beginPosition);
                position.SetEndIndex(buffer.Length);
                fields.Add(position);
            }
            else
            {
                // Set to the first occurrence
                if (((Object)position.GetFieldAttribute() == (Object)dateFormatField || (position
                                                                                         .GetFieldAttribute() == null && position.GetField() == index)) &&
                    position.GetEndIndex() == 0)
                {
                    position.SetBeginIndex(beginPosition);
                    position.SetEndIndex(buffer.Length);
                }
            }
        }