예제 #1
0
        public static string ToSimpleString(this TimeSpan time, string format = "h-m-s")
        {
            var build = ObjectPool <StringBuilder> .AcquireObject();

            build.EnsureCapacity(format.Length * 2);

            var noformat  = false;
            var nopadding = false;
            var zeroValue = false;
            var zeroEmpty = 0;

            string fFormat, dFormat;
            object append;

            for (var i = 0; i < format.Length; i++)
            {
                if (format[i] == '#')
                {
                    noformat = !noformat;
                    continue;
                }

                if (noformat)
                {
                    build.Append(format[i]);
                    continue;
                }

                switch (format[i])
                {
                case '!':
                    nopadding = !nopadding;
                    continue;
                }

                fFormat = zeroEmpty > 0 ? (nopadding ? "{0:#.#}" : "{0:#.##}") : (nopadding ? "{0:0.#}" : "{0:0.##}");
                dFormat = zeroEmpty > 0 ? (nopadding ? "{0:#}" : "{0:##}") : (nopadding ? "{0:0}" : "{0:00}");

                append = null;

                switch (format[i])
                {
                case '<':
                    ++zeroEmpty;
                    break;

                case '>':
                {
                    if (zeroEmpty == 0 || (zeroEmpty > 0 && --zeroEmpty == 0))
                    {
                        zeroValue = false;
                    }
                }
                break;

                case '\\':
                {
                    if (i + 1 < format.Length)
                    {
                        append = format[++i];
                    }
                }
                break;

                case 'x':
                    append = ToSimpleString(TimeZoneInfo.Utc, false);
                    break;

                case 'X':
                    append = ToSimpleString(TimeZoneInfo.Utc, DateTime.UtcNow.IsDaylightSavingTime());
                    break;

                case 'z':
                    append = ToSimpleString(TimeZoneInfo.Local, false);
                    break;

                case 'Z':
                    append = ToSimpleString(TimeZoneInfo.Local, DateTime.Now.IsDaylightSavingTime());
                    break;

                case 'D':
                {
                    append    = String.Format(fFormat, time.TotalDays);
                    zeroValue = String.IsNullOrWhiteSpace((string)append);
                }
                break;

                case 'H':
                {
                    append    = String.Format(fFormat, time.TotalHours);
                    zeroValue = String.IsNullOrWhiteSpace((string)append);
                }
                break;

                case 'M':
                {
                    append    = String.Format(fFormat, time.TotalMinutes);
                    zeroValue = String.IsNullOrWhiteSpace((string)append);
                }
                break;

                case 'S':
                {
                    append    = String.Format(fFormat, time.TotalSeconds);
                    zeroValue = String.IsNullOrWhiteSpace((string)append);
                }
                break;

                case 'd':
                {
                    append    = String.Format(dFormat, time.Days);
                    zeroValue = String.IsNullOrWhiteSpace((string)append);
                }
                break;

                case 'h':
                {
                    append    = String.Format(dFormat, time.Hours);
                    zeroValue = String.IsNullOrWhiteSpace((string)append);
                }
                break;

                case 'm':
                {
                    append    = String.Format(dFormat, time.Minutes);
                    zeroValue = String.IsNullOrWhiteSpace((string)append);
                }
                break;

                case 's':
                {
                    append    = String.Format(dFormat, time.Seconds);
                    zeroValue = String.IsNullOrWhiteSpace((string)append);
                }
                break;

                default:
                    append = format[i];
                    break;
                }

                if (append != null && (!zeroValue || zeroEmpty <= 0))
                {
                    build.Append(append);
                }
            }

            var value = build.ToString();

            ObjectPool.Free(ref build);

            return(value);
        }
예제 #2
0
        public static string ToSimpleString(this DateTime date, string format = "t D d M y")
        {
            var build = ObjectPool <StringBuilder> .AcquireObject();

            build.EnsureCapacity(format.Length * 2);

            var noformat = false;

            for (var i = 0; i < format.Length; i++)
            {
                if (format[i] == '#')
                {
                    noformat = !noformat;
                    continue;
                }

                if (noformat)
                {
                    build.Append(format[i]);
                    continue;
                }

                switch (format[i])
                {
                case '\\':
                    build.Append((i + 1 < format.Length) ? Convert.ToString(format[++i]) : String.Empty);
                    break;

                case 'x':
                case 'z':
                {
                    var utc = date.Kind == DateTimeKind.Utc;
                    var tzo = utc ? TimeZoneInfo.Utc : TimeZoneInfo.Local;

                    build.Append(ToSimpleString(tzo, false));
                }
                break;

                case 'X':
                case 'Z':
                {
                    var utc = date.Kind == DateTimeKind.Utc;
                    var tzo = utc ? TimeZoneInfo.Utc : TimeZoneInfo.Local;

                    build.Append(ToSimpleString(tzo, date.IsDaylightSavingTime()));
                }
                break;

                case 'D':
                    build.Append(date.DayOfWeek);
                    break;

                case 'd':
                    build.Append(date.Day);
                    break;

                case 'M':
                    build.Append(GetMonth(date));
                    break;

                case 'm':
                    build.Append(date.Month);
                    break;

                case 'y':
                    build.Append(date.Year);
                    break;

                case 't':
                {
                    var tf = String.Empty;

                    if (i + 1 < format.Length)
                    {
                        if (format[i + 1] == '@')
                        {
                            ++i;

                            while (++i < format.Length && format[i] != '@')
                            {
                                tf += format[i];
                            }
                        }
                    }

                    build.Append(ToSimpleString(date.TimeOfDay, !String.IsNullOrWhiteSpace(tf) ? tf : "h-m-s"));
                }
                break;

                default:
                    build.Append(format[i]);
                    break;
                }
            }

            var value = build.ToString();

            ObjectPool.Free(ref build);

            return(value);
        }