namespace VideoBrowser.Helpers { using System; using VideoBrowser.Common; /// /// Defines the . /// public static class FormatString { #region Fields private static readonly string[] TimeUnitsNames = { "Milli", "Sec", "Min", "Hour", "Day", "Month", "Year", "Decade", "Century" }; private static int[] TimeUnitsValue = { 1000, 60, 60, 24, 30, 12, 10, 10 };// Reference unit is milli #endregion Fields #region Methods /// /// Returns a formatted string of the given file size. /// /// The file size as long to format. /// The . public static string FormatFileSize(this long size) { return string.Format(new ByteFormatProvider(), "{0:fs}", size); } /// /// The FormatLeftTime. /// /// The milliseconds. /// The . public static string FormatLeftTime(this long milliseconds) { var format = ""; for (var i = 0; i < TimeUnitsValue.Length; i++) { var y = milliseconds % TimeUnitsValue[i]; milliseconds = milliseconds / TimeUnitsValue[i]; if (y == 0) { continue; } format = y + " " + TimeUnitsNames[i] + " , " + format; } format = format.Trim(',', ' '); return format == "" ? "0 Sec" : format; } /// /// Returns a formatted string of the video length. /// /// The video duration as long. /// The . public static string FormatVideoLength(this long duration) { return FormatVideoLength(TimeSpan.FromSeconds(duration)); } /// /// Returns a formatted string of the video length. /// /// The video duration as TimeSpan. /// The . public static string FormatVideoLength(this TimeSpan duration) { return duration.Hours > 0 ? string.Format("{0}:{1:00}:{2:00}", duration.Hours, duration.Minutes, duration.Seconds) : string.Format("{0}:{1:00}", duration.Minutes, duration.Seconds); } /// /// The GetDirectorySizeFormatted. /// /// The directory. /// The . public static string GetDirectorySizeFormatted(this string directory) { return FormatFileSize(FileHelper.GetDirectorySize(directory)); } #endregion Methods } }