// 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 System.Globalization; using Humanizer; using Humanizer.Localisation; namespace osu.Game.Utils { public static class HumanizerUtils { /// /// Turns the current or provided date into a human readable sentence /// /// The date to be humanized /// distance of time in words public static string Humanize(DateTimeOffset input) { // this works around https://github.com/xamarin/xamarin-android/issues/2012 and https://github.com/Humanizr/Humanizer/issues/690#issuecomment-368536282 try { return input.Humanize(); } catch (ArgumentException) { return input.Humanize(culture: new CultureInfo("en-US")); } } /// /// Turns the current or provided timespan into a human readable sentence /// /// The date to be humanized /// The maximum number of time units to return. Defaulted is 1 which means the largest unit is returned /// The maximum unit of time to output. The default value is . The time units and will give approximations for time spans bigger 30 days by calculating with 365.2425 days a year and 30.4369 days a month. /// The minimum unit of time to output. /// Uses words instead of numbers if true. E.g. one day. /// distance of time in words public static string Humanize(TimeSpan input, int precision = 1, TimeUnit maxUnit = TimeUnit.Week, TimeUnit minUnit = TimeUnit.Millisecond, bool toWords = false) { // this works around https://github.com/xamarin/xamarin-android/issues/2012 and https://github.com/Humanizr/Humanizer/issues/690#issuecomment-368536282 try { return input.Humanize(precision: precision, maxUnit: maxUnit, minUnit: minUnit); } catch (ArgumentException) { return input.Humanize(culture: new CultureInfo("en-US"), precision: precision, maxUnit: maxUnit, minUnit: minUnit); } } } }