This repository has been archived on 2026-03-14. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Eco/VideoBrowser/Helpers/Format.cs
2023-07-24 12:00:34 +02:00

90 lines
3.0 KiB
C#

namespace VideoBrowser.Helpers
{
using System;
using VideoBrowser.Common;
/// <summary>
/// Defines the <see cref="FormatString" />.
/// </summary>
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
/// <summary>
/// Returns a formatted string of the given file size.
/// </summary>
/// <param name="size">The file size as long to format.</param>
/// <returns>The <see cref="string"/>.</returns>
public static string FormatFileSize(this long size)
{
return string.Format(new ByteFormatProvider(), "{0:fs}", size);
}
/// <summary>
/// The FormatLeftTime.
/// </summary>
/// <param name="milliseconds">The milliseconds<see cref="long"/>.</param>
/// <returns>The <see cref="string"/>.</returns>
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;
}
/// <summary>
/// Returns a formatted string of the video length.
/// </summary>
/// <param name="duration">The video duration as long.</param>
/// <returns>The <see cref="string"/>.</returns>
public static string FormatVideoLength(this long duration)
{
return FormatVideoLength(TimeSpan.FromSeconds(duration));
}
/// <summary>
/// Returns a formatted string of the video length.
/// </summary>
/// <param name="duration">The video duration as TimeSpan.</param>
/// <returns>The <see cref="string"/>.</returns>
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);
}
/// <summary>
/// The GetDirectorySizeFormatted.
/// </summary>
/// <param name="directory">The directory<see cref="string"/>.</param>
/// <returns>The <see cref="string"/>.</returns>
public static string GetDirectorySizeFormatted(this string directory)
{
return FormatFileSize(FileHelper.GetDirectorySize(directory));
}
#endregion Methods
}
}