// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using osu.Framework.Localisation; namespace osu.Game.Extensions { public static class TimeDisplayExtensions { /// /// Get an editor formatted string (mm:ss:mss) /// /// A time value in milliseconds. /// An editor formatted display string. public static string ToEditorFormattedString(this double milliseconds) => ToEditorFormattedString(TimeSpan.FromMilliseconds(milliseconds)); /// /// Get an editor formatted string (mm:ss:mss) /// /// A time value. /// An editor formatted display string. public static string ToEditorFormattedString(this TimeSpan timeSpan) => $"{(timeSpan < TimeSpan.Zero ? "-" : string.Empty)}{(int)timeSpan.TotalMinutes:00}:{timeSpan:ss\\:fff}"; /// /// Get a formatted duration (dd:hh:mm:ss with days/hours omitted if zero). /// /// A duration in milliseconds. /// A formatted duration string. public static LocalisableString ToFormattedDuration(this double milliseconds) => ToFormattedDuration(TimeSpan.FromMilliseconds(milliseconds)); /// /// Get a formatted duration (dd:hh:mm:ss with days/hours omitted if zero). /// /// A duration value. /// A formatted duration string. public static LocalisableString ToFormattedDuration(this TimeSpan timeSpan) { if (timeSpan.TotalDays >= 1) return new LocalisableFormattableString(timeSpan, @"dd\:hh\:mm\:ss"); if (timeSpan.TotalHours >= 1) return new LocalisableFormattableString(timeSpan, @"hh\:mm\:ss"); return new LocalisableFormattableString(timeSpan, @"mm\:ss"); } } }