Example #1
0
            /// <summary>
            /// Compare to another object, check whether or not two objects are same.
            /// </summary>
            /// <param name="obj">Another object to be compared.</param>
            /// <returns>True if two objects are same; Otherwise return false.</returns>
            public override bool Equals(object obj)
            {
                if (!(obj is DateTimeFormatArgs))
                {
                    return(false);
                }
                DateTimeFormatArgs o = (DateTimeFormatArgs)obj;

                return(format.Equals(o.format) &&
                       cultureName.Equals(o.cultureName));
            }
Example #2
0
        /// <summary>
        /// Format cell
        /// </summary>
        /// <param name="cell">cell to be formatted</param>
        /// <returns>Formatted text used to display as cell content</returns>
        public string FormatCell(Cell cell)
        {
            object data = cell.InnerData;

            bool     isFormat = false;
            double   number;
            DateTime value         = baseStartDate;
            string   formattedText = null;

            if (data is DateTime)
            {
                value    = (DateTime)data;
                isFormat = true;
            }
            else if (CellUtility.TryGetNumberData(data, out number))
            {
                try
                {
                    var val = (double)number;

                    // Excel/Lotus 2/29/1900 bug
                    // original post: http://stackoverflow.com/questions/727466/how-do-i-convert-an-excel-serial-date-number-to-a-net-datetime
                    if (val > 59)
                    {
                        val -= 1;
                    }

                    value    = BaseStartDate.AddDays(val - 1);
                    isFormat = true;
                }
                catch { }
            }
            else
            {
                string strdata = (data is string?(string)data : Convert.ToString(data));

                double days = 0;
                if (double.TryParse(strdata, out days))
                {
                    try
                    {
                        value    = value.AddDays(days);
                        isFormat = true;
                    }
                    catch { }
                }
                else
                {
                    isFormat = (DateTime.TryParse(strdata, out value));
                }
            }

            if (isFormat)
            {
                if (cell.InnerStyle.HAlign == ReoGridHorAlign.General)
                {
                    cell.RenderHorAlign = ReoGridRenderHorAlign.Right;
                }

                CultureInfo culture = null;

                string pattern = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;

                if (cell.DataFormatArgs != null && cell.DataFormatArgs is DateTimeFormatArgs)
                {
                    DateTimeFormatArgs dargs = (DateTimeFormatArgs)cell.DataFormatArgs;

                    pattern = dargs.Format;
                    culture = (dargs.CultureName == null ||
                               string.Equals(dargs.CultureName, Thread.CurrentThread.CurrentCulture.Name))
                                                ? Thread.CurrentThread.CurrentCulture : new CultureInfo(dargs.CultureName);
                }
                else
                {
                    culture             = System.Threading.Thread.CurrentThread.CurrentCulture;
                    cell.DataFormatArgs = new DateTimeFormatArgs {
                        Format = pattern, CultureName = culture.Name
                    };
                }

                if (culture.Name.StartsWith("ja") && pattern.Contains("g"))
                {
                    culture = new CultureInfo("ja-JP", true);
                    culture.DateTimeFormat.Calendar = new JapaneseCalendar();
                }

                try
                {
                    switch (pattern)
                    {
                    case "d":
                        formattedText = value.Day.ToString();
                        break;

                    default:
                        formattedText = value.ToString(pattern, culture);
                        break;
                    }
                }
                catch
                {
                    formattedText = Convert.ToString(value);
                }
            }

            return(isFormat ? formattedText : null);
        }