コード例 #1
0
        private static string GetUnitValueString(int value, TimeSpanUnit unit, TimeFormat timeFormat,
                                                 UnitStringRepresentation rep, IFormatProvider formatProvider)
        {
            string unitString = GetUnitString(value, unit, rep);

            return(value.ToString(formatProvider) + timeFormat.UnitValueSeparator + unitString);
        }
コード例 #2
0
 public static string ToTimeRemainingString(this TimeSpan value,
                                            int maxUnitGroups              = 1,
                                            UnitStringRepresentation rep   = UnitStringRepresentation.Long,
                                            TimeSpanUnit highestUnit       = TimeSpanUnit.Days,
                                            TimeSpanUnit lowestUnit        = TimeSpanUnit.Seconds,
                                            IFormatProvider formatProvider = null)
 {
     return(ToPrettyString(value, maxUnitGroups, rep, highestUnit, lowestUnit, IntegerRounding.Up, formatProvider));
 }
コード例 #3
0
        private static string GetUnitString(int value, TimeSpanUnit unit, UnitStringRepresentation rep)
        {
            switch (rep)
            {
            case UnitStringRepresentation.Compact:
            case UnitStringRepresentation.CompactWithSpace:
                return(GetCompactUnitString(unit));

            case UnitStringRepresentation.Long:
                return(GetLongUnitString(value, unit));

            case UnitStringRepresentation.Short:
                return(GetShortUnitString(value, unit));

            default:
                throw new NotImplementedException("TimeSpanUnit: " + unit);
            }
        }
コード例 #4
0
        public string ShowsUnitValuesInConfiguredUnitRepresentation(UnitStringRepresentation rep)
        {
            var t = new TimeSpan(3, 4, 0);

            return(t.ToPrettyString(2, rep));
        }
コード例 #5
0
        /// <summary>
        ///     Returns a human readable string from TimeSpan, with a max number of parts to include. Units are included from
        ///     largest to smallest.
        /// </summary>
        /// <param name="value">Time span value.</param>
        /// <param name="maxUnitGroups">
        ///     The max number of timespan units to use.
        /// </param>
        /// <param name="lowestUnit">Lowest unit to include in string.</param>
        /// <param name="rep"></param>
        /// <param name="highestUnit">Highest unit to include in string.</param>
        /// <param name="lowestUnitRounding">Rounding behavior of <paramref name="lowestUnit" />.</param>
        /// <param name="formatProvider">Specify the formatProvider used to .ToString() the numeric values.</param>
        /// <returns>Human readable string.</returns>
        public static string ToPrettyString(this TimeSpan value,
                                            int maxUnitGroups                  = 1,
                                            UnitStringRepresentation rep       = UnitStringRepresentation.Long,
                                            TimeSpanUnit highestUnit           = TimeSpanUnit.Days,
                                            TimeSpanUnit lowestUnit            = TimeSpanUnit.Seconds,
                                            IntegerRounding lowestUnitRounding = IntegerRounding.ToNearestOrUp,
                                            IFormatProvider formatProvider     = null)
        {
            if (maxUnitGroups <= 0)
            {
                maxUnitGroups = 1;
            }

            TimeFormat timeFormat;

            if (!Formats.TryGetValue(rep, out timeFormat))
            {
                throw new NotImplementedException("UnitStringRepresentation: " + rep);
            }

            if (value == TimeSpan.Zero)
            {
                return(GetUnitValueString(0, lowestUnit, timeFormat, rep, formatProvider));
            }

            //int days = value.Days,
            //    hours = value.Hours,
            //    minutes = value.Minutes,
            //    seconds = value.Seconds,
            //    milliseconds = value.Milliseconds;
            //if (highestUnit < TimeSpanUnit.Days || lowestUnit > TimeSpanUnit.Days)
            //    days = 0;
            //if (highestUnit < TimeSpanUnit.Hours || lowestUnit > TimeSpanUnit.Hours)
            //    hours = 0;
            //if (highestUnit < TimeSpanUnit.Minutes || lowestUnit > TimeSpanUnit.Minutes)
            //    minutes = 0;
            //if (highestUnit < TimeSpanUnit.Seconds || lowestUnit > TimeSpanUnit.Seconds)
            //    seconds = 0;
            //if (highestUnit < TimeSpanUnit.Milliseconds || lowestUnit > TimeSpanUnit.Milliseconds)
            //    milliseconds = 0;

            // Trim off any values outside range
            TimeSpan trimmedValue = value; //new TimeSpan(days, hours, minutes, seconds, milliseconds);

            //var roundedValue = GetRoundedValue(trimmedValue, highestUnit, lowestUnit, lowestUnitRounding, maxUnitGroups);
            IList <UnitValue> nonZeroUnitValues =
                GetNonZeroUnitValues(trimmedValue, highestUnit, lowestUnit, lowestUnitRounding, maxUnitGroups)
                .RoundUnitsUp();

            if (!nonZeroUnitValues.Any())
            {
                // Value is zero or near zero. Fall back to lowest unit.
                // Example: 0.1 seconds when lowest unit is seconds
                int nearZeroValueRounded = GetInteger(GetDouble(value, lowestUnit), lowestUnitRounding);
                return(GetUnitValueString(nearZeroValueRounded, lowestUnit, timeFormat, rep, formatProvider));
            }

            List <string> unitStrings =
                nonZeroUnitValues
                .Select((uv, i) => GetUnitValueString(uv.Value, uv.Unit, timeFormat, rep, formatProvider))
                .ToList();

            // Example: "3 hours"
            if (unitStrings.Count == 1)
            {
                return(unitStrings.First());
            }

            // Example: "1 weeks, 2 days"
            string firstParts = string.Join(timeFormat.GroupSeparator, unitStrings.Take(unitStrings.Count - 1).ToArray());

            // Example: "3 hours"
            string lastPart = unitStrings.Last();

            // Example: "1 weeks, 2 days and 3 hours"
            return(firstParts + timeFormat.LastGroupSeparator + lastPart);
        }