| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TimeHumanizerExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using MyNet.Globalization.Facade; |
| | | 10 | | using MyNet.Humanizer.Temporal; |
| | | 11 | | using MyNet.Temporal.Decomposition; |
| | | 12 | | |
| | | 13 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 14 | | namespace MyNet.Humanizer.Facade; |
| | | 15 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 16 | | |
| | | 17 | | public static class TimeHumanizerExtensions |
| | | 18 | | { |
| | | 19 | | extension(TimeSpan timeSpan) |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Humanizes the time span using default options. |
| | | 23 | | /// </summary> |
| | 3 | 24 | | public string Humanize(CultureInfo? culture = null) => timeSpan.Humanize(TimeHumanizationPresets.Default, cultur |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Humanizes the time span using the specified options. |
| | | 28 | | /// </summary> |
| | | 29 | | public string Humanize(TimeHumanizationOptions options, CultureInfo? culture = null) |
| | | 30 | | { |
| | 291 | 31 | | ArgumentNullException.ThrowIfNull(options); |
| | | 32 | | |
| | 291 | 33 | | var decompositionOptions = options.ToDecompositionOptions(); |
| | | 34 | | |
| | 291 | 35 | | var decomposition = timeSpan.Decompose(decompositionOptions); |
| | | 36 | | |
| | 291 | 37 | | return options.Mode switch |
| | 291 | 38 | | { |
| | 9 | 39 | | TimeHumanizationMode.Duration => Localizer.ForCulture(culture).GetRequired<ITimeHumanizer>().Humaniz |
| | 282 | 40 | | TimeHumanizationMode.Relative => Localizer.ForCulture(culture).GetRequired<ITimeHumanizer>().Humaniz |
| | 0 | 41 | | _ => string.Empty |
| | 291 | 42 | | }; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | public string Humanize(Action<TimeHumanizationBuilder> configure, CultureInfo? culture = null) |
| | | 46 | | { |
| | 3 | 47 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 48 | | |
| | 3 | 49 | | var builder = new TimeHumanizationBuilder(); |
| | | 50 | | |
| | 3 | 51 | | configure(builder); |
| | | 52 | | |
| | 3 | 53 | | return timeSpan.Humanize(builder.Build(), culture); |
| | | 54 | | } |
| | | 55 | | } |
| | | 56 | | } |
| | | 57 | | |