using System;

namespace Expedience.Web.Extensions;

public static class Extensions
{
	public static string Format(this TimeSpan? timeSpan)
	{
		if (timeSpan is null)
			return "";
		var roundedTimeSpan = new TimeSpan(timeSpan.Value.Days, timeSpan.Value.Hours,
		timeSpan.Value.Minutes, timeSpan.Value.Seconds,
			RoundToNearestTen(timeSpan.Value.Milliseconds));

		return roundedTimeSpan.ToString("hh\\:mm\\:ss\\.ff");
	}

	static int RoundToNearestTen(int num)
	{
		int rem = num % 10;
		return rem >= 5 ? (num - rem + 10) : (num - rem);
	}
}
