1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 14:32:55 +08:00

Add xmldoc and clean up ScoreboardTimeUtils extension methods a touch

This commit is contained in:
Dean Herbert 2022-02-21 13:30:58 +09:00
parent 2ded7d281b
commit 79408f6afc
3 changed files with 23 additions and 17 deletions

View File

@ -395,7 +395,7 @@ namespace osu.Game.Online.Leaderboards
Font = OsuFont.GetFont(size: 17, weight: FontWeight.Bold, italics: true);
}
protected override string Format() => ScoreboardTimeUtils.FormatDate(Date, TimeSpan.FromSeconds(30));
protected override string Format() => ScoreboardTimeUtils.FormatRelativeTime(Date, TimeSpan.FromSeconds(30));
}
public class LeaderboardScoreStatistic

View File

@ -15,6 +15,6 @@ namespace osu.Game.Overlays.BeatmapSet.Scores
}
protected override string Format()
=> ScoreboardTimeUtils.FormatDate(Date, TimeSpan.FromHours(1));
=> ScoreboardTimeUtils.FormatRelativeTime(Date, TimeSpan.FromHours(1));
}
}

View File

@ -10,15 +10,13 @@ namespace osu.Game.Utils
{
public static class ScoreboardTimeUtils
{
public static string FormatQuantity(string template, int quantity)
{
if (quantity <= 1)
return $@"{quantity}{template}";
return $@"{quantity}{template}s";
}
public static string FormatDate(DateTimeOffset time, TimeSpan lowerCutoff)
/// <summary>
/// Formats a provided date to a short relative string version for compact display.
/// </summary>
/// <param name="time">The time to be displayed.</param>
/// <param name="lowerCutoff">A timespan denoting the time length beneath which "now" should be displayed.</param>
/// <returns>A short relative string representing the input time.</returns>
public static string FormatRelativeTime(DateTimeOffset time, TimeSpan lowerCutoff)
{
// web uses momentjs's custom locales to format the date for the purposes of the scoreboard.
// this is intended to be a best-effort, more legible approximation of that.
@ -36,23 +34,23 @@ namespace osu.Game.Utils
return CommonStrings.TimeNow.ToString();
if (span.TotalMinutes < 1)
return FormatQuantity("sec", (int)span.TotalSeconds);
return formatQuantity("sec", (int)span.TotalSeconds);
if (span.TotalHours < 1)
return FormatQuantity("min", (int)span.TotalMinutes);
return formatQuantity("min", (int)span.TotalMinutes);
if (span.TotalDays < 1)
return FormatQuantity("hr", (int)span.TotalHours);
return formatQuantity("hr", (int)span.TotalHours);
// this is where this gets more complicated because of how the calendar works.
// since there's no `TotalMonths` / `TotalYears`, we have to iteratively add months/years
// and test against cutoff dates to determine how many months/years to show.
if (time > now.AddMonths(-1))
return FormatQuantity("dy", (int)span.TotalDays);
return formatQuantity("dy", (int)span.TotalDays);
for (int months = 1; months <= 11; ++months)
{
if (time > now.AddMonths(-(months + 1)))
return FormatQuantity("mo", months);
return formatQuantity("mo", months);
}
int years = 1;
@ -60,9 +58,17 @@ namespace osu.Game.Utils
while (years < 20 && time <= now.AddYears(-(years + 1)))
years += 1;
if (years < 20)
return FormatQuantity("yr", years);
return formatQuantity("yr", years);
return "never";
}
private static string formatQuantity(string template, int quantity)
{
if (quantity <= 1)
return $@"{quantity}{template}";
return $@"{quantity}{template}s";
}
}
}