Beispiel #1
0
        /// <summary>
        /// Get a time unit name in a certain format.
        /// </summary>
        /// <param name="unit">The time unit.</param>
        /// <param name="format">The format (full or minimum).</param>
        /// <returns>The time unit name in the given format.</returns>
        public static string GetTimeUnitInFormat(this TimeUnit unit, TimeUnitFormat format)
        {
            switch (unit)
            {
            case TimeUnit.Year:
                return(format == TimeUnitFormat.Full ? " years" : "yr");

            case TimeUnit.Month:
                return(format == TimeUnitFormat.Full ? " months" : "M");

            case TimeUnit.Day:
                return(format == TimeUnitFormat.Full ? " days" : "day");

            case TimeUnit.Hour:
                return(format == TimeUnitFormat.Full ? " hours" : "hr");

            case TimeUnit.Minute:
                return(format == TimeUnitFormat.Full ? " minutes" : "min");

            case TimeUnit.Second:
                return(format == TimeUnitFormat.Full ? " seconds" : "sec");

            case TimeUnit.Millisecond:
                return(format == TimeUnitFormat.Full ? " milliseconds" : "ms");

            default:
                throw new ArgumentException($"TimeUnit {unit} is not a valid time unit.");
            }
        }
Beispiel #2
0
		/// <summary>
		/// Format a millisecond time to a string with a certain time unit and depth (e.g. "3min, 14sec, 15ms" with depth 3).
		/// </summary>
		/// <param name="timeMilliseconds">The time in milliseconds.</param>
		/// <param name="separator">The seperator between each unit (", " by default).</param>
		/// <param name="format">The time unit format.</param>
		/// <param name="depth">The depth to represent units with (depth 0 = just top unit, depth 1 = top unit and 1 specific unit, ...)</param>
		/// <param name="putFirstUnitLast">Indicate if no other unit than the first unit should be put in the string. First unit will be put at the end of the string (for e.g. "3.14min").</param>
		/// <param name="padSubUnits">Indicate if sub units should be left-padded with zeros </param>
		/// <returns>The time string.</returns>
		public static string FormatTime(double timeMilliseconds, string separator = ", ", TimeUnitFormat format = TimeUnitFormat.Minimum, int depth = 1, bool putFirstUnitLast = false, bool padSubUnits = false)
		{
			TimeUnit localUnit = TimeUnit.Millisecond;

			IList<string> localTimes = new List<string>();
			double localTime = timeMilliseconds;

			while (true)
			{
				TimeUnit? nextUnit = localUnit.GetBigger();

				localTimes.Add(((int)localTime).ToString(CultureInfo.InvariantCulture));

				if (!nextUnit.HasValue) break;

				double timeUnitParts = nextUnit.Value.GetTimeUnitParts();
				double nextLocalTime = localTime / timeUnitParts;

				if (nextLocalTime < 1.0) break;

				string subTime = (localTime % timeUnitParts).ToString(CultureInfo.InvariantCulture);

				if (padSubUnits) subTime = subTime.PadLeft((int)Math.Log10(timeUnitParts), '0');

				localTimes[localTimes.Count - 1] = subTime;

				localTime /= nextUnit.Value.GetTimeUnitParts();
				localUnit = nextUnit.Value;
			}

			StringBuilder builder = new StringBuilder();

			TimeUnit first = localUnit;

			int printDepth = Math.Max(0, localTimes.Count - depth - 1);
			for (int i = localTimes.Count - 1; i >= printDepth; i--)
			{
				builder.Append(localTimes[i]);

				if (!putFirstUnitLast) builder.Append(localUnit.GetTimeUnitInFormat(format));

				if (i - 1 >= printDepth)
				{
					builder.Append(separator);

					localUnit = localUnit.GetSmaller().Value;
				}
			}

			if (putFirstUnitLast) builder.Append(first.GetTimeUnitInFormat(format));

			return builder.ToString();
		}
Beispiel #3
0
		/// <summary>
		/// Format a millisecond time to a simple string, e.g. "12.423min". 
		/// </summary>
		/// <param name="timeMilliseconds">The time in milliseconds.</param>
		/// <param name="format">The time unit format.</param>
		/// <returns>The time string.</returns>
		public static string FormatTimeSimple(double timeMilliseconds, TimeUnitFormat format = TimeUnitFormat.Minimum)
		{
			return FormatTime(timeMilliseconds, separator: ".", format: format, depth: 1, putFirstUnitLast: true, padSubUnits: true);
		}