コード例 #1
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();
		}