Example #1
0
        /// <summary>
        /// Formats the specified object as a string.
        /// </summary>
        /// <param name="obj">Object with cell data to format.</param>
        /// <returns>Returns the formatted string.</returns>
        public string Format(object obj)
        {
            DateTime time     = DateTime.Now;
            DateTime?nullable = FormatConverter.TryDateTime(obj, true);

            if (!nullable.HasValue)
            {
                return(FormatConverter.ToString(obj, true));
            }
            return(nullable.Value.ToString(this.formatString, (IFormatProvider)this.FormatProvider));
        }
Example #2
0
        /// <summary>
        /// Formats the specified value.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns>Returns the string of the formatted value.</returns>
        public override string Format(object obj)
        {
            if (obj is bool)
            {
                return(FormatConverter.ToString(obj, true));
            }
            string   number   = string.Empty;
            DateTime time     = DateTime.Now;
            DateTime?nullable = FormatConverter.TryDateTime(obj, true);

            if (nullable.HasValue)
            {
                time = nullable.Value;
                if ((((this.validDateTimeFormatString == "H") || (this.validDateTimeFormatString == "h")) || ((this.validDateTimeFormatString == "m") || (this.validDateTimeFormatString == "M"))) || (((this.validDateTimeFormatString == "d") || (this.validDateTimeFormatString == "s")) || (this.validDateTimeFormatString == "y")))
                {
                    this.validDateTimeFormatString = "%" + this.validDateTimeFormatString;
                }
                number = time.ToString(this.validDateTimeFormatString, (IFormatProvider)this.DateTimeFormatInfo);
                if (this.hasJD)
                {
                    string monthName = DefaultTokens.DateTimeFormatInfo.GetMonthName(time.Month);
                    number = number.Replace(PlaceholderMonthJD, monthName.Substring(0, 1));
                }
                if (this.absTimeParts != null)
                {
                    TimeSpan span = (TimeSpan)(time - this.AbsoluteTime);
                    foreach (ABSTimeFormatPart part in this.absTimeParts)
                    {
                        string newValue = null;
                        switch (part.TimePartType)
                        {
                        case TimePart.Hour:
                            newValue = ((double)Math.Floor(span.TotalHours)).ToString(part.FormatString);
                            break;

                        case TimePart.Minute:
                            newValue = ((double)Math.Floor(span.TotalMinutes)).ToString(part.FormatString);
                            break;

                        case TimePart.Second:
                            newValue = ((double)Math.Floor(span.TotalSeconds)).ToString(part.FormatString);
                            break;
                        }
                        if (newValue != null)
                        {
                            number = number.Replace(DefaultTokens.ReplacePlaceholder + part.Token, newValue);
                        }
                    }
                }
            }
            else
            {
                number = FormatConverter.ToString(obj, true);
            }
            if (this.NumberStringConverter is DefaultDateTimeNumberStringConverter)
            {
                if (this.NumberStringConverter != null)
                {
                    number = this.NumberStringConverter.ConvertTo(number, obj, false, base.PartLocaleID, base.PartDBNumberFormat);
                }
                return(number);
            }
            if (this.NumberStringConverter == null)
            {
                return(number);
            }
            if (this.hasYearDelay)
            {
                number = number.Replace(DefaultTokens.ReplacePlaceholder + YearFourDigit, time.ToString(YearFourDigit)).Replace(DefaultTokens.ReplacePlaceholder + YearTwoDigit, time.ToString(YearTwoDigit));
            }
            return(this.NumberStringConverter.ConvertTo(number, obj, true, base.PartLocaleID, base.PartDBNumberFormat));
        }