From 4e44ba5e2ca157344272572eade07b9a0c3bfb31 Mon Sep 17 00:00:00 2001 From: Julian Katzwinkel Date: Sat, 24 Aug 2024 06:09:34 +0200 Subject: [PATCH 1/7] Change TimeOnly.ToClockNotation to accept a culture so cases where UI Culture is different than the wanted culture. --- src/Humanizer/Configuration/Configurator.cs | 4 +- ...tugueseTimeOnlyToClockNotationConverter.cs | 20 +++---- ...DefaultTimeOnlyToClockNotationConverter.cs | 30 +++++------ .../EsTimeOnlyToClockNotationConverter.cs | 10 ++-- .../FrTimeOnlyToClockNotationConverter.cs | 14 ++--- .../ITimeOnlyToClockNotationConverter.cs | 5 +- .../LbTimeOnlyToClockNotationConverter.cs | 52 +++++++++---------- ...tugueseTimeOnlyToClockNotationConverter.cs | 22 ++++---- .../TimeOnlyToClockNotationExtensions.cs | 7 +-- 9 files changed, 82 insertions(+), 82 deletions(-) diff --git a/src/Humanizer/Configuration/Configurator.cs b/src/Humanizer/Configuration/Configurator.cs index 617b3622b..67976f287 100644 --- a/src/Humanizer/Configuration/Configurator.cs +++ b/src/Humanizer/Configuration/Configurator.cs @@ -1,4 +1,4 @@ -namespace Humanizer; +namespace Humanizer; /// /// Provides a configuration point for Humanizer @@ -74,7 +74,7 @@ internal static INumberToWordsConverter GetNumberToWordsConverter(CultureInfo? c /// internal static IDateOnlyToOrdinalWordConverter DateOnlyToOrdinalWordsConverter => DateOnlyToOrdinalWordsConverters.ResolveForUiCulture(); - internal static ITimeOnlyToClockNotationConverter TimeOnlyToClockNotationConverter => TimeOnlyToClockNotationConverters.ResolveForUiCulture(); + internal static ITimeOnlyToClockNotationConverter TimeOnlyToClockNotationConverter(CultureInfo? culture) => TimeOnlyToClockNotationConverters.ResolveForCulture(culture); #endif /// diff --git a/src/Humanizer/Localisation/TimeToClockNotation/BrazilianPortugueseTimeOnlyToClockNotationConverter.cs b/src/Humanizer/Localisation/TimeToClockNotation/BrazilianPortugueseTimeOnlyToClockNotationConverter.cs index 4ee18ee9f..116b150c5 100644 --- a/src/Humanizer/Localisation/TimeToClockNotation/BrazilianPortugueseTimeOnlyToClockNotationConverter.cs +++ b/src/Humanizer/Localisation/TimeToClockNotation/BrazilianPortugueseTimeOnlyToClockNotationConverter.cs @@ -1,10 +1,10 @@ -#if NET6_0_OR_GREATER +#if NET6_0_OR_GREATER namespace Humanizer; class BrazilianPortugueseTimeOnlyToClockNotationConverter : ITimeOnlyToClockNotationConverter { - public string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive) + public string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive, CultureInfo? culture) { switch (time) { @@ -21,14 +21,14 @@ public string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive) return normalizedMinutes switch { - 00 => $"{normalizedHour.ToWords(GrammaticalGender.Feminine)} em ponto", - 30 => $"{normalizedHour.ToWords(GrammaticalGender.Feminine)} e meia", - 40 => $"vinte para as {(normalizedHour + 1).ToWords(GrammaticalGender.Feminine)}", - 45 => $"quinze para as {(normalizedHour + 1).ToWords(GrammaticalGender.Feminine)}", - 50 => $"dez para as {(normalizedHour + 1).ToWords(GrammaticalGender.Feminine)}", - 55 => $"cinco para as {(normalizedHour + 1).ToWords(GrammaticalGender.Feminine)}", - 60 => $"{(normalizedHour + 1).ToWords(GrammaticalGender.Feminine)} em ponto", - _ => $"{normalizedHour.ToWords(GrammaticalGender.Feminine)} e {normalizedMinutes.ToWords()}" + 00 => $"{normalizedHour.ToWords(GrammaticalGender.Feminine, culture)} em ponto", + 30 => $"{normalizedHour.ToWords(GrammaticalGender.Feminine, culture)} e meia", + 40 => $"vinte para as {(normalizedHour + 1).ToWords(GrammaticalGender.Feminine, culture)}", + 45 => $"quinze para as {(normalizedHour + 1).ToWords(GrammaticalGender.Feminine, culture)}", + 50 => $"dez para as {(normalizedHour + 1).ToWords(GrammaticalGender.Feminine, culture)}", + 55 => $"cinco para as {(normalizedHour + 1).ToWords(GrammaticalGender.Feminine, culture)}", + 60 => $"{(normalizedHour + 1).ToWords(GrammaticalGender.Feminine, culture)} em ponto", + _ => $"{normalizedHour.ToWords(GrammaticalGender.Feminine, culture)} e {normalizedMinutes.ToWords(culture)}" }; } } diff --git a/src/Humanizer/Localisation/TimeToClockNotation/DefaultTimeOnlyToClockNotationConverter.cs b/src/Humanizer/Localisation/TimeToClockNotation/DefaultTimeOnlyToClockNotationConverter.cs index 3efc3a5ab..e7bdb70d4 100644 --- a/src/Humanizer/Localisation/TimeToClockNotation/DefaultTimeOnlyToClockNotationConverter.cs +++ b/src/Humanizer/Localisation/TimeToClockNotation/DefaultTimeOnlyToClockNotationConverter.cs @@ -1,10 +1,10 @@ -#if NET6_0_OR_GREATER +#if NET6_0_OR_GREATER namespace Humanizer; class DefaultTimeOnlyToClockNotationConverter : ITimeOnlyToClockNotationConverter { - public string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive) + public string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive, CultureInfo? culture) { switch (time) { @@ -21,19 +21,19 @@ public string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive) return normalizedMinutes switch { - 00 => $"{normalizedHour.ToWords()} o'clock", - 05 => $"five past {normalizedHour.ToWords()}", - 10 => $"ten past {normalizedHour.ToWords()}", - 15 => $"a quarter past {normalizedHour.ToWords()}", - 20 => $"twenty past {normalizedHour.ToWords()}", - 25 => $"twenty-five past {normalizedHour.ToWords()}", - 30 => $"half past {normalizedHour.ToWords()}", - 40 => $"twenty to {(normalizedHour + 1).ToWords()}", - 45 => $"a quarter to {(normalizedHour + 1).ToWords()}", - 50 => $"ten to {(normalizedHour + 1).ToWords()}", - 55 => $"five to {(normalizedHour + 1).ToWords()}", - 60 => $"{(normalizedHour + 1).ToWords()} o'clock", - _ => $"{normalizedHour.ToWords()} {normalizedMinutes.ToWords()}" + 00 => $"{normalizedHour.ToWords(culture)} o'clock", + 05 => $"five past {normalizedHour.ToWords(culture)}", + 10 => $"ten past {normalizedHour.ToWords(culture)}", + 15 => $"a quarter past {normalizedHour.ToWords(culture)}", + 20 => $"twenty past {normalizedHour.ToWords(culture)}", + 25 => $"twenty-five past {normalizedHour.ToWords(culture)}", + 30 => $"half past {normalizedHour.ToWords(culture)}", + 40 => $"twenty to {(normalizedHour + 1).ToWords(culture)}", + 45 => $"a quarter to {(normalizedHour + 1).ToWords(culture)}", + 50 => $"ten to {(normalizedHour + 1).ToWords(culture)}", + 55 => $"five to {(normalizedHour + 1).ToWords(culture)}", + 60 => $"{(normalizedHour + 1).ToWords(culture)} o'clock", + _ => $"{normalizedHour.ToWords(culture)} {normalizedMinutes.ToWords(culture)}" }; } } diff --git a/src/Humanizer/Localisation/TimeToClockNotation/EsTimeOnlyToClockNotationConverter.cs b/src/Humanizer/Localisation/TimeToClockNotation/EsTimeOnlyToClockNotationConverter.cs index e14282744..714fe2aaf 100644 --- a/src/Humanizer/Localisation/TimeToClockNotation/EsTimeOnlyToClockNotationConverter.cs +++ b/src/Humanizer/Localisation/TimeToClockNotation/EsTimeOnlyToClockNotationConverter.cs @@ -1,4 +1,4 @@ -#if NET6_0_OR_GREATER +#if NET6_0_OR_GREATER namespace Humanizer; @@ -8,7 +8,7 @@ class EsTimeOnlyToClockNotationConverter : ITimeOnlyToClockNotationConverter const int NOON = 12; const int AFTERNOON = 21; - public string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive) + public string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive, CultureInfo? culture) { switch (time) { @@ -21,8 +21,8 @@ public string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive) var article = GetArticle(time); var articleNextHour = GetArticle(time.AddHours(1)); - var hour = NormalizeHour(time).ToWords(GrammaticalGender.Feminine); - var nextHour = NormalizeHour(time.AddHours(1)).ToWords(GrammaticalGender.Feminine); + var hour = NormalizeHour(time).ToWords(GrammaticalGender.Feminine, culture); + var nextHour = NormalizeHour(time.AddHours(1)).ToWords(GrammaticalGender.Feminine, culture); var dayPeriod = GetDayPeriod(time); var dayPeriodNextHour = GetDayPeriod(time.AddHours(1)); @@ -45,7 +45,7 @@ public string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive) return clockNotationMap.GetValueOrDefault( normalizedMinutes, - $"{article} {hour} y {normalizedMinutes.ToWords()} {dayPeriod}"); + $"{article} {hour} y {normalizedMinutes.ToWords(culture)} {dayPeriod}"); } static int NormalizeHour(TimeOnly time) => diff --git a/src/Humanizer/Localisation/TimeToClockNotation/FrTimeOnlyToClockNotationConverter.cs b/src/Humanizer/Localisation/TimeToClockNotation/FrTimeOnlyToClockNotationConverter.cs index 62bdf31d3..7727e8446 100644 --- a/src/Humanizer/Localisation/TimeToClockNotation/FrTimeOnlyToClockNotationConverter.cs +++ b/src/Humanizer/Localisation/TimeToClockNotation/FrTimeOnlyToClockNotationConverter.cs @@ -1,10 +1,10 @@ -#if NET6_0_OR_GREATER +#if NET6_0_OR_GREATER namespace Humanizer; class FrTimeOnlyToClockNotationConverter : ITimeOnlyToClockNotationConverter { - public string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive) + public string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive, CultureInfo? culture) { var normalizedMinutes = (int)(roundToNearestFive == ClockNotationRounding.NearestFiveMinutes ? 5 * Math.Round(time.Minute / 5.0) @@ -12,17 +12,17 @@ public string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive) return normalizedMinutes switch { - 00 => GetHourExpression(time.Hour), - 60 => GetHourExpression(time.Hour + 1), - _ => $"{GetHourExpression(time.Hour)} {normalizedMinutes.ToWords(GrammaticalGender.Feminine)}" + 00 => GetHourExpression(time.Hour, culture), + 60 => GetHourExpression(time.Hour + 1, culture), + _ => $"{GetHourExpression(time.Hour, culture)} {normalizedMinutes.ToWords(GrammaticalGender.Feminine, culture)}" }; - static string GetHourExpression(int hour) => + static string GetHourExpression(int hour, CultureInfo? culture) => hour switch { 0 => "minuit", 12 => "midi", - _ => hour.ToWords(GrammaticalGender.Feminine) + (hour > 1 ? " heures" : " heure") + _ => hour.ToWords(GrammaticalGender.Feminine, culture) + (hour > 1 ? " heures" : " heure") }; } } diff --git a/src/Humanizer/Localisation/TimeToClockNotation/ITimeOnlyToClockNotationConverter.cs b/src/Humanizer/Localisation/TimeToClockNotation/ITimeOnlyToClockNotationConverter.cs index a2ad41551..757fad650 100644 --- a/src/Humanizer/Localisation/TimeToClockNotation/ITimeOnlyToClockNotationConverter.cs +++ b/src/Humanizer/Localisation/TimeToClockNotation/ITimeOnlyToClockNotationConverter.cs @@ -1,4 +1,4 @@ -#if NET6_0_OR_GREATER +#if NET6_0_OR_GREATER namespace Humanizer; @@ -10,7 +10,6 @@ public interface ITimeOnlyToClockNotationConverter /// /// Converts the time to Clock Notation /// - string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive); + string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive, CultureInfo? culture); } - #endif diff --git a/src/Humanizer/Localisation/TimeToClockNotation/LbTimeOnlyToClockNotationConverter.cs b/src/Humanizer/Localisation/TimeToClockNotation/LbTimeOnlyToClockNotationConverter.cs index e14c7b20c..1a9b5ac9c 100644 --- a/src/Humanizer/Localisation/TimeToClockNotation/LbTimeOnlyToClockNotationConverter.cs +++ b/src/Humanizer/Localisation/TimeToClockNotation/LbTimeOnlyToClockNotationConverter.cs @@ -1,10 +1,10 @@ -#if NET6_0_OR_GREATER +#if NET6_0_OR_GREATER namespace Humanizer; class LbTimeOnlyToClockNotationConverter : ITimeOnlyToClockNotationConverter { - public string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive) + public string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive, CultureInfo? culture) { var roundedTime = roundToNearestFive is ClockNotationRounding.NearestFiveMinutes ? GetRoundedTime(time) @@ -16,22 +16,22 @@ public string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive) { Hour: 12, Minute: 0 } => "Mëtteg", _ => roundedTime.Minute switch { - 00 => GetHourExpression(roundedTime.Hour, "Auer"), - 15 => $"Véirel op {GetHourExpression(roundedTime.Hour)}", - 25 => $"{GetMinuteExpression(30 - roundedTime.Minute, "vir")} hallwer {GetHourExpression(roundedTime.Hour + 1)}", - 30 => $"hallwer {GetHourExpression(roundedTime.Hour + 1)}", - 35 => $"{GetMinuteExpression(roundedTime.Minute - 30, "op")} hallwer {GetHourExpression(roundedTime.Hour + 1)}", - 45 => $"Véirel vir {GetHourExpression(roundedTime.Hour + 1)}", - 60 => GetHourExpression(roundedTime.Hour + 1, "Auer"), - 01 => $"{GetMinuteExpression(roundedTime.Minute, "Minutt")} op {GetHourExpression(roundedTime.Hour)}", - 59 => $"{GetMinuteExpression(60 - roundedTime.Minute, "Minutt")} vir {GetHourExpression(roundedTime.Hour + 1)}", - 05 or 10 or 20 => $"{GetMinuteExpression(roundedTime.Minute, "op")} {GetHourExpression(roundedTime.Hour)}", - 40 or 50 or 55 => $"{GetMinuteExpression(60 - roundedTime.Minute, "vir")} {GetHourExpression(roundedTime.Hour + 1)}", - > 00 and < 25 => $"{GetMinuteExpression(roundedTime.Minute, "Minutten")} op {GetHourExpression(roundedTime.Hour)}", - > 25 and < 30 => $"{GetMinuteExpression(30 - roundedTime.Minute, "Minutten")} vir hallwer {GetHourExpression(roundedTime.Hour + 1)}", - > 30 and < 35 => $"{GetMinuteExpression(roundedTime.Minute - 30, "Minutten")} op hallwer {GetHourExpression(roundedTime.Hour + 1)}", - > 35 and < 60 => $"{GetMinuteExpression(60 - roundedTime.Minute, "Minutten")} vir {GetHourExpression(roundedTime.Hour + 1)}", - _ => $"{GetHourExpression(time.Hour, "Auer")} {GetMinuteExpression(time.Minute)}" + 00 => GetHourExpression(culture, roundedTime.Hour, "Auer"), + 15 => $"Véirel op {GetHourExpression(culture, roundedTime.Hour)}", + 25 => $"{GetMinuteExpression(culture, 30 - roundedTime.Minute, "vir")} hallwer {GetHourExpression(culture, roundedTime.Hour + 1)}", + 30 => $"hallwer {GetHourExpression(culture, roundedTime.Hour + 1)}", + 35 => $"{GetMinuteExpression(culture, roundedTime.Minute - 30, "op")} hallwer {GetHourExpression(culture, roundedTime.Hour + 1)}", + 45 => $"Véirel vir {GetHourExpression(culture, roundedTime.Hour + 1)}", + 60 => GetHourExpression(culture, roundedTime.Hour + 1, "Auer"), + 01 => $"{GetMinuteExpression(culture, roundedTime.Minute, "Minutt")} op {GetHourExpression(culture, roundedTime.Hour)}", + 59 => $"{GetMinuteExpression(culture, 60 - roundedTime.Minute, "Minutt")} vir {GetHourExpression(culture, roundedTime.Hour + 1)}", + 05 or 10 or 20 => $"{GetMinuteExpression(culture, roundedTime.Minute, "op")} {GetHourExpression(culture, roundedTime.Hour)}", + 40 or 50 or 55 => $"{GetMinuteExpression(culture, 60 - roundedTime.Minute, "vir")} {GetHourExpression(culture, roundedTime.Hour + 1)}", + > 00 and < 25 => $"{GetMinuteExpression(culture, roundedTime.Minute, "Minutten")} op {GetHourExpression(culture, roundedTime.Hour)}", + > 25 and < 30 => $"{GetMinuteExpression(culture, 30 - roundedTime.Minute, "Minutten")} vir hallwer {GetHourExpression(culture, roundedTime.Hour + 1)}", + > 30 and < 35 => $"{GetMinuteExpression(culture, roundedTime.Minute - 30, "Minutten")} op hallwer {GetHourExpression(culture, roundedTime.Hour + 1)}", + > 35 and < 60 => $"{GetMinuteExpression(culture, 60 - roundedTime.Minute, "Minutten")} vir {GetHourExpression(culture, roundedTime.Hour + 1)}", + _ => $"{GetHourExpression(culture, time.Hour, "Auer")} {GetMinuteExpression(culture, time.Minute)}" } }; } @@ -44,23 +44,23 @@ private static TimeOnly GetRoundedTime(TimeOnly time) return new(roundedHours, roundedMinutes); } - private static string GetMinuteExpression(int minute, string nextWord = "") - => GetFormattedExpression(minute, nextWord); + private static string GetMinuteExpression(CultureInfo? culture, int minute, string nextWord = "") + => GetFormattedExpression(culture, minute, nextWord); - private static string GetHourExpression(int hour, string nextWord = "") + private static string GetHourExpression(CultureInfo? culture, int hour, string nextWord = "") { var normalizedHour = hour % 12; var hourExpression = normalizedHour == 0 ? 12 : normalizedHour; - return GetFormattedExpression(hourExpression, nextWord); + return GetFormattedExpression(culture, hourExpression, nextWord); } - private static string GetFormattedExpression(int number, string nextWord) => + private static string GetFormattedExpression(CultureInfo? culture, int number, string nextWord) => (number switch { - 1 or 2 => $"{number.ToWords(GrammaticalGender.Feminine)} {nextWord}", - 7 => $"{number.ToWords(LuxembourgishFormatter.DoesEifelerRuleApply(nextWord) ? WordForm.Eifeler : WordForm.Normal)} {nextWord}", - _ => $"{number.ToWords()} {nextWord}" + 1 or 2 => $"{number.ToWords(GrammaticalGender.Feminine, culture)} {nextWord}", + 7 => $"{number.ToWords(LuxembourgishFormatter.DoesEifelerRuleApply(nextWord) ? WordForm.Eifeler : WordForm.Normal, culture)} {nextWord}", + _ => $"{number.ToWords(culture)} {nextWord}" }).TrimEnd(); } diff --git a/src/Humanizer/Localisation/TimeToClockNotation/PortugueseTimeOnlyToClockNotationConverter.cs b/src/Humanizer/Localisation/TimeToClockNotation/PortugueseTimeOnlyToClockNotationConverter.cs index 1b3be880e..5e16d77b8 100644 --- a/src/Humanizer/Localisation/TimeToClockNotation/PortugueseTimeOnlyToClockNotationConverter.cs +++ b/src/Humanizer/Localisation/TimeToClockNotation/PortugueseTimeOnlyToClockNotationConverter.cs @@ -1,10 +1,10 @@ -#if NET6_0_OR_GREATER +#if NET6_0_OR_GREATER namespace Humanizer; class PortugueseTimeOnlyToClockNotationConverter : ITimeOnlyToClockNotationConverter { - public string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive) + public string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive, CultureInfo? culture) { switch (time) { @@ -21,15 +21,15 @@ public string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive) return normalizedMinutes switch { - 00 => $"{normalizedHour.ToWords(GrammaticalGender.Feminine)} horas", - 15 => $"{normalizedHour.ToWords(GrammaticalGender.Feminine)} e um quarto", - 30 => $"{normalizedHour.ToWords(GrammaticalGender.Feminine)} e meia", - 40 => $"{(normalizedHour + 1).ToWords(GrammaticalGender.Feminine)} menos vinte", - 45 => $"{(normalizedHour + 1).ToWords(GrammaticalGender.Feminine)} menos um quarto", - 50 => $"{(normalizedHour + 1).ToWords(GrammaticalGender.Feminine)} menos dez", - 55 => $"{(normalizedHour + 1).ToWords(GrammaticalGender.Feminine)} menos cinco", - 60 => $"{(normalizedHour + 1).ToWords(GrammaticalGender.Feminine)} horas", - _ => $"{normalizedHour.ToWords(GrammaticalGender.Feminine)} e {normalizedMinutes.ToWords()}" + 00 => $"{normalizedHour.ToWords(GrammaticalGender.Feminine, culture)} horas", + 15 => $"{normalizedHour.ToWords(GrammaticalGender.Feminine, culture)} e um quarto", + 30 => $"{normalizedHour.ToWords(GrammaticalGender.Feminine, culture)} e meia", + 40 => $"{(normalizedHour + 1).ToWords(GrammaticalGender.Feminine, culture)} menos vinte", + 45 => $"{(normalizedHour + 1).ToWords(GrammaticalGender.Feminine, culture)} menos um quarto", + 50 => $"{(normalizedHour + 1).ToWords(GrammaticalGender.Feminine, culture)} menos dez", + 55 => $"{(normalizedHour + 1).ToWords(GrammaticalGender.Feminine, culture)} menos cinco", + 60 => $"{(normalizedHour + 1).ToWords(GrammaticalGender.Feminine, culture)} horas", + _ => $"{normalizedHour.ToWords(GrammaticalGender.Feminine, culture)} e {normalizedMinutes.ToWords(culture)}" }; } } diff --git a/src/Humanizer/TimeOnlyToClockNotationExtensions.cs b/src/Humanizer/TimeOnlyToClockNotationExtensions.cs index 871598e45..ad4f72cc1 100644 --- a/src/Humanizer/TimeOnlyToClockNotationExtensions.cs +++ b/src/Humanizer/TimeOnlyToClockNotationExtensions.cs @@ -1,4 +1,4 @@ -#if NET6_0_OR_GREATER +#if NET6_0_OR_GREATER namespace Humanizer; @@ -12,9 +12,10 @@ public static class TimeOnlyToClockNotationExtensions /// /// The time to be made into clock notation /// Whether to round the minutes to the nearest five or not + /// Culture to use. If null, current thread's UI culture is used. /// The time in clock notation - public static string ToClockNotation(this TimeOnly input, ClockNotationRounding roundToNearestFive = ClockNotationRounding.None) => - Configurator.TimeOnlyToClockNotationConverter.Convert(input, roundToNearestFive); + public static string ToClockNotation(this TimeOnly input, ClockNotationRounding roundToNearestFive = ClockNotationRounding.None, CultureInfo? culture = null) => + Configurator.TimeOnlyToClockNotationConverter(culture).Convert(input, roundToNearestFive, culture); } #endif From 963b5c6bef7393937c4ee69ab9e1ad836070388c Mon Sep 17 00:00:00 2001 From: Julian Katzwinkel Date: Sat, 24 Aug 2024 06:18:54 +0200 Subject: [PATCH 2/7] Added tests --- .../en/TimeToClockNotationTests.cs | 50 ++++++++++++++- .../es/TimeToClockNotationTests.cs | 60 +++++++++++++++++- .../fr/TimeToClockNotationTests.cs | 55 ++++++++++++++++- .../lb/TimeToClockNotationTests.cs | 61 ++++++++++++++++++- .../pt-BR/TimeToClockNotationTests.cs | 49 ++++++++++++++- .../pt/TimeToClockNotationTests.cs | 50 ++++++++++++++- 6 files changed, 319 insertions(+), 6 deletions(-) diff --git a/src/Humanizer.Tests/Localisation/en/TimeToClockNotationTests.cs b/src/Humanizer.Tests/Localisation/en/TimeToClockNotationTests.cs index 7e43774c9..6c14ecd7a 100644 --- a/src/Humanizer.Tests/Localisation/en/TimeToClockNotationTests.cs +++ b/src/Humanizer.Tests/Localisation/en/TimeToClockNotationTests.cs @@ -1,4 +1,4 @@ -#if NET6_0_OR_GREATER +#if NET6_0_OR_GREATER namespace en; @@ -28,6 +28,29 @@ public void ConvertToClockNotationTimeOnlyStringEnUs(int hours, int minutes, str Assert.Equal(expectedResult, actualResult); } + [Theory] + [InlineData(00, 00, "midnight")] + [InlineData(04, 00, "four o'clock")] + [InlineData(05, 01, "five one")] + [InlineData(06, 05, "five past six")] + [InlineData(07, 10, "ten past seven")] + [InlineData(08, 15, "a quarter past eight")] + [InlineData(09, 20, "twenty past nine")] + [InlineData(10, 25, "twenty-five past ten")] + [InlineData(11, 30, "half past eleven")] + [InlineData(12, 00, "noon")] + [InlineData(15, 35, "three thirty-five")] + [InlineData(16, 40, "twenty to five")] + [InlineData(17, 45, "a quarter to six")] + [InlineData(18, 50, "ten to seven")] + [InlineData(19, 55, "five to eight")] + [InlineData(20, 59, "eight fifty-nine")] + public void ConvertToClockNotationTimeOnlyStringWithCultureEnUs(int hours, int minutes, string expectedResult) + { + var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.None, CultureInfo.CreateSpecificCulture("en")); + Assert.Equal(expectedResult, actualResult); + } + [Theory] [InlineData(00, 00, "midnight")] [InlineData(04, 00, "four o'clock")] @@ -52,6 +75,31 @@ public void ConvertToRoundedClockNotationTimeOnlyStringEnUs(int hours, int minut var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.NearestFiveMinutes); Assert.Equal(expectedResult, actualResult); } + + [Theory] + [InlineData(00, 00, "midnight")] + [InlineData(04, 00, "four o'clock")] + [InlineData(05, 01, "five o'clock")] + [InlineData(06, 05, "five past six")] + [InlineData(07, 10, "ten past seven")] + [InlineData(08, 15, "a quarter past eight")] + [InlineData(09, 20, "twenty past nine")] + [InlineData(10, 25, "twenty-five past ten")] + [InlineData(11, 30, "half past eleven")] + [InlineData(12, 00, "noon")] + [InlineData(13, 23, "twenty-five past one")] + [InlineData(14, 32, "half past two")] + [InlineData(15, 35, "three thirty-five")] + [InlineData(16, 40, "twenty to five")] + [InlineData(17, 45, "a quarter to six")] + [InlineData(18, 50, "ten to seven")] + [InlineData(19, 55, "five to eight")] + [InlineData(20, 59, "nine o'clock")] + public void ConvertToRoundedClockNotationTimeOnlyStringWithCultureEnUs(int hours, int minutes, string expectedResult) + { + var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.NearestFiveMinutes, CultureInfo.CreateSpecificCulture("en")); + Assert.Equal(expectedResult, actualResult); + } } #endif diff --git a/src/Humanizer.Tests/Localisation/es/TimeToClockNotationTests.cs b/src/Humanizer.Tests/Localisation/es/TimeToClockNotationTests.cs index 0f5e93794..dc8b42d11 100644 --- a/src/Humanizer.Tests/Localisation/es/TimeToClockNotationTests.cs +++ b/src/Humanizer.Tests/Localisation/es/TimeToClockNotationTests.cs @@ -1,4 +1,4 @@ -#if NET6_0_OR_GREATER +#if NET6_0_OR_GREATER namespace es; @@ -34,6 +34,35 @@ public void ConvertToClockNotationTimeOnlyString(int hours, int minutes, string Assert.Equal(expectedResult, actualResult); } + [Theory] + [InlineData(0, 0, "medianoche")] + [InlineData(0, 7, "las doce y siete de la noche")] + [InlineData(1, 11, "la una y once de la madrugada")] + [InlineData(4, 0, "las cuatro de la madrugada")] + [InlineData(5, 1, "las cinco y uno de la madrugada")] + [InlineData(6, 0, "las seis de la mañana")] + [InlineData(6, 5, "las seis y cinco de la mañana")] + [InlineData(7, 10, "las siete y diez de la mañana")] + [InlineData(8, 15, "las ocho y cuarto de la mañana")] + [InlineData(9, 20, "las nueve y veinte de la mañana")] + [InlineData(10, 25, "las diez y veinticinco de la mañana")] + [InlineData(11, 30, "las once y media de la mañana")] + [InlineData(12, 00, "mediodía")] + [InlineData(12, 38, "las doce y treinta y ocho de la tarde")] + [InlineData(12, 35, "la una menos veinticinco de la tarde")] + [InlineData(15, 40, "las cuatro menos veinte de la tarde")] + [InlineData(17, 45, "las seis menos cuarto de la tarde")] + [InlineData(19, 50, "las ocho menos diez de la tarde")] + [InlineData(21, 0, "las nueve de la noche")] + [InlineData(21, 55, "las diez menos cinco de la noche")] + [InlineData(22, 59, "las diez y cincuenta y nueve de la noche")] + [InlineData(23, 43, "las once y cuarenta y tres de la noche")] + public void ConvertToClockNotationTimeOnlyStringWithCulture(int hours, int minutes, string expectedResult) + { + var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.None, CultureInfo.CreateSpecificCulture("es-ES")); + Assert.Equal(expectedResult, actualResult); + } + [Theory] [InlineData(0, 0, "medianoche")] [InlineData(0, 7, "las doce y cinco de la noche")] @@ -62,6 +91,35 @@ public void ConvertToRoundedClockNotationTimeOnlyString(int hours, int minutes, var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.NearestFiveMinutes); Assert.Equal(expectedResult, actualResult); } + + [Theory] + [InlineData(0, 0, "medianoche")] + [InlineData(0, 7, "las doce y cinco de la noche")] + [InlineData(1, 11, "la una y diez de la madrugada")] + [InlineData(4, 0, "las cuatro de la madrugada")] + [InlineData(5, 1, "las cinco de la madrugada")] + [InlineData(6, 0, "las seis de la mañana")] + [InlineData(6, 5, "las seis y cinco de la mañana")] + [InlineData(7, 10, "las siete y diez de la mañana")] + [InlineData(8, 15, "las ocho y cuarto de la mañana")] + [InlineData(9, 20, "las nueve y veinte de la mañana")] + [InlineData(10, 25, "las diez y veinticinco de la mañana")] + [InlineData(11, 30, "las once y media de la mañana")] + [InlineData(12, 00, "mediodía")] + [InlineData(12, 38, "la una menos veinte de la tarde")] + [InlineData(12, 35, "la una menos veinticinco de la tarde")] + [InlineData(15, 40, "las cuatro menos veinte de la tarde")] + [InlineData(17, 45, "las seis menos cuarto de la tarde")] + [InlineData(19, 50, "las ocho menos diez de la tarde")] + [InlineData(21, 0, "las nueve de la noche")] + [InlineData(21, 55, "las diez menos cinco de la noche")] + [InlineData(22, 59, "las once de la noche")] + [InlineData(23, 43, "las doce menos cuarto de la noche")] + public void ConvertToRoundedClockNotationTimeOnlyStringWithCulture(int hours, int minutes, string expectedResult) + { + var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.NearestFiveMinutes, CultureInfo.CreateSpecificCulture("es-ES")); + Assert.Equal(expectedResult, actualResult); + } } #endif diff --git a/src/Humanizer.Tests/Localisation/fr/TimeToClockNotationTests.cs b/src/Humanizer.Tests/Localisation/fr/TimeToClockNotationTests.cs index a1d1ff929..86a17f5f6 100644 --- a/src/Humanizer.Tests/Localisation/fr/TimeToClockNotationTests.cs +++ b/src/Humanizer.Tests/Localisation/fr/TimeToClockNotationTests.cs @@ -1,4 +1,4 @@ -#if NET6_0_OR_GREATER +#if NET6_0_OR_GREATER namespace fr; @@ -30,6 +30,31 @@ public void ConvertToClockNotationTimeOnlyString(int hours, int minutes, string var actualResult = new TimeOnly(hours, minutes).ToClockNotation(); Assert.Equal(expectedResult, actualResult); } + [Theory] + [InlineData(00, 00, "minuit")] + [InlineData(00, 07, "minuit sept")] + [InlineData(01, 11, "une heure onze")] + [InlineData(04, 00, "quatre heures")] + [InlineData(05, 01, "cinq heures une")] + [InlineData(06, 05, "six heures cinq")] + [InlineData(07, 10, "sept heures dix")] + [InlineData(08, 15, "huit heures quinze")] + [InlineData(09, 20, "neuf heures vingt")] + [InlineData(10, 25, "dix heures vingt-cinq")] + [InlineData(11, 30, "onze heures trente")] + [InlineData(12, 00, "midi")] + [InlineData(12, 38, "midi trente-huit")] + [InlineData(15, 35, "quinze heures trente-cinq")] + [InlineData(16, 40, "seize heures quarante")] + [InlineData(17, 45, "dix-sept heures quarante-cinq")] + [InlineData(18, 50, "dix-huit heures cinquante")] + [InlineData(19, 55, "dix-neuf heures cinquante-cinq")] + [InlineData(20, 59, "vingt heures cinquante-neuf")] + public void ConvertToClockNotationTimeOnlyStringWithCulture(int hours, int minutes, string expectedResult) + { + var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.None, CultureInfo.CreateSpecificCulture("fr")); + Assert.Equal(expectedResult, actualResult); + } [Theory] [InlineData(00, 00, "minuit")] @@ -58,6 +83,34 @@ public void ConvertToRoundedClockNotationTimeOnlyString(int hours, int minutes, var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.NearestFiveMinutes); Assert.Equal(expectedResult, actualResult); } + + [Theory] + [InlineData(00, 00, "minuit")] + [InlineData(00, 07, "minuit cinq")] + [InlineData(01, 11, "une heure dix")] + [InlineData(04, 00, "quatre heures")] + [InlineData(05, 01, "cinq heures")] + [InlineData(06, 05, "six heures cinq")] + [InlineData(07, 10, "sept heures dix")] + [InlineData(08, 15, "huit heures quinze")] + [InlineData(09, 20, "neuf heures vingt")] + [InlineData(10, 25, "dix heures vingt-cinq")] + [InlineData(11, 30, "onze heures trente")] + [InlineData(12, 00, "midi")] + [InlineData(12, 38, "midi quarante")] + [InlineData(13, 23, "treize heures vingt-cinq")] + [InlineData(14, 32, "quatorze heures trente")] + [InlineData(15, 35, "quinze heures trente-cinq")] + [InlineData(16, 40, "seize heures quarante")] + [InlineData(17, 45, "dix-sept heures quarante-cinq")] + [InlineData(18, 50, "dix-huit heures cinquante")] + [InlineData(19, 55, "dix-neuf heures cinquante-cinq")] + [InlineData(20, 59, "vingt et une heures")] + public void ConvertToRoundedClockNotationTimeOnlyStringWithCulture(int hours, int minutes, string expectedResult) + { + var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.NearestFiveMinutes, CultureInfo.CreateSpecificCulture("fr")); + Assert.Equal(expectedResult, actualResult); + } } #endif diff --git a/src/Humanizer.Tests/Localisation/lb/TimeToClockNotationTests.cs b/src/Humanizer.Tests/Localisation/lb/TimeToClockNotationTests.cs index 06a8fef60..f8750416d 100644 --- a/src/Humanizer.Tests/Localisation/lb/TimeToClockNotationTests.cs +++ b/src/Humanizer.Tests/Localisation/lb/TimeToClockNotationTests.cs @@ -1,4 +1,4 @@ -#if NET6_0_OR_GREATER +#if NET6_0_OR_GREATER namespace lb; @@ -34,6 +34,35 @@ public void ConvertToClockNotationTimeOnlyString(int hours, int minutes, string var actualResult = new TimeOnly(hours, minutes).ToClockNotation(); Assert.Equal(expectedResult, actualResult); } + [Theory] + [InlineData(00, 00, "Mëtternuecht")] + [InlineData(00, 07, "siwe Minutten op zwielef")] + [InlineData(01, 11, "eelef Minutten op eng")] + [InlineData(04, 00, "véier Auer")] + [InlineData(05, 01, "eng Minutt op fënnef")] + [InlineData(06, 05, "fënnef op sechs")] + [InlineData(07, 10, "zéng op siwen")] + [InlineData(08, 15, "Véirel op aacht")] + [InlineData(09, 20, "zwanzeg op néng")] + [InlineData(10, 25, "fënnef vir hallwer eelef")] + [InlineData(11, 30, "hallwer zwielef")] + [InlineData(12, 00, "Mëtteg")] + [InlineData(12, 39, "eenanzwanzeg Minutten vir eng")] + [InlineData(13, 23, "dräianzwanzeg Minutten op eng")] + [InlineData(14, 32, "zwou Minutten op hallwer dräi")] + [InlineData(15, 35, "fënnef op hallwer véier")] + [InlineData(16, 40, "zwanzeg vir fënnef")] + [InlineData(17, 45, "Véirel vir sechs")] + [InlineData(18, 50, "zéng vir siwen")] + [InlineData(19, 52, "aacht Minutten vir aacht")] + [InlineData(20, 55, "fënnef vir néng")] + [InlineData(21, 58, "zwou Minutten vir zéng")] + [InlineData(22, 59, "eng Minutt vir eelef")] + public void ConvertToClockNotationTimeOnlyStringWithCulture(int hours, int minutes, string expectedResult) + { + var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.None, CultureInfo.CreateSpecificCulture("lb-LU")); + Assert.Equal(expectedResult, actualResult); + } [Theory] [InlineData(00, 00, "Mëtternuecht")] @@ -64,6 +93,36 @@ public void ConvertToRoundedClockNotationTimeOnlyString(int hours, int minutes, var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.NearestFiveMinutes); Assert.Equal(expectedResult, actualResult); } + + [Theory] + [InlineData(00, 00, "Mëtternuecht")] + [InlineData(00, 07, "fënnef op zwielef")] + [InlineData(01, 11, "zéng op eng")] + [InlineData(04, 00, "véier Auer")] + [InlineData(05, 01, "fënnef Auer")] + [InlineData(06, 05, "fënnef op sechs")] + [InlineData(07, 10, "zéng op siwen")] + [InlineData(08, 15, "Véirel op aacht")] + [InlineData(09, 20, "zwanzeg op néng")] + [InlineData(10, 25, "fënnef vir hallwer eelef")] + [InlineData(11, 30, "hallwer zwielef")] + [InlineData(12, 00, "Mëtteg")] + [InlineData(12, 39, "zwanzeg vir eng")] + [InlineData(13, 23, "fënnef vir hallwer zwou")] + [InlineData(14, 32, "hallwer dräi")] + [InlineData(15, 35, "fënnef op hallwer véier")] + [InlineData(16, 40, "zwanzeg vir fënnef")] + [InlineData(17, 45, "Véirel vir sechs")] + [InlineData(18, 50, "zéng vir siwen")] + [InlineData(19, 52, "zéng vir aacht")] + [InlineData(20, 55, "fënnef vir néng")] + [InlineData(21, 58, "zéng Auer")] + [InlineData(22, 59, "eelef Auer")] + public void ConvertToRoundedClockNotationTimeOnlyStringWithCulture(int hours, int minutes, string expectedResult) + { + var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.NearestFiveMinutes, CultureInfo.CreateSpecificCulture("lb-LU")); + Assert.Equal(expectedResult, actualResult); + } } #endif diff --git a/src/Humanizer.Tests/Localisation/pt-BR/TimeToClockNotationTests.cs b/src/Humanizer.Tests/Localisation/pt-BR/TimeToClockNotationTests.cs index 53496aba8..21c3b8fbf 100644 --- a/src/Humanizer.Tests/Localisation/pt-BR/TimeToClockNotationTests.cs +++ b/src/Humanizer.Tests/Localisation/pt-BR/TimeToClockNotationTests.cs @@ -1,4 +1,4 @@ -#if NET6_0_OR_GREATER +#if NET6_0_OR_GREATER namespace ptBR; @@ -27,6 +27,28 @@ public void ConvertToClockNotationTimeOnlyStringPtBr(int hours, int minutes, str var actualResult = new TimeOnly(hours, minutes).ToClockNotation(); Assert.Equal(expectedResult, actualResult); } + [Theory] + [InlineData(00, 00, "meia-noite")] + [InlineData(04, 00, "quatro em ponto")] + [InlineData(05, 01, "cinco e um")] + [InlineData(06, 05, "seis e cinco")] + [InlineData(07, 10, "sete e dez")] + [InlineData(08, 15, "oito e quinze")] + [InlineData(09, 20, "nove e vinte")] + [InlineData(10, 25, "dez e vinte e cinco")] + [InlineData(11, 30, "onze e meia")] + [InlineData(12, 00, "meio-dia")] + [InlineData(15, 35, "três e trinta e cinco")] + [InlineData(16, 40, "vinte para as cinco")] + [InlineData(17, 45, "quinze para as seis")] + [InlineData(18, 50, "dez para as sete")] + [InlineData(19, 55, "cinco para as oito")] + [InlineData(20, 59, "oito e cinquenta e nove")] + public void ConvertToClockNotationTimeOnlyStringWithCulturePtBr(int hours, int minutes, string expectedResult) + { + var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.None, CultureInfo.CreateSpecificCulture("pt-BR")); + Assert.Equal(expectedResult, actualResult); + } [Theory] [InlineData(00, 00, "meia-noite")] @@ -52,6 +74,31 @@ public void ConvertToRoundedClockNotationTimeOnlyStringPtBr(int hours, int minut var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.NearestFiveMinutes); Assert.Equal(expectedResult, actualResult); } + + [Theory] + [InlineData(00, 00, "meia-noite")] + [InlineData(04, 00, "quatro em ponto")] + [InlineData(05, 01, "cinco em ponto")] + [InlineData(06, 05, "seis e cinco")] + [InlineData(07, 10, "sete e dez")] + [InlineData(08, 15, "oito e quinze")] + [InlineData(09, 20, "nove e vinte")] + [InlineData(10, 25, "dez e vinte e cinco")] + [InlineData(11, 30, "onze e meia")] + [InlineData(12, 00, "meio-dia")] + [InlineData(13, 23, "uma e vinte e cinco")] + [InlineData(14, 32, "duas e meia")] + [InlineData(15, 35, "três e trinta e cinco")] + [InlineData(16, 40, "vinte para as cinco")] + [InlineData(17, 45, "quinze para as seis")] + [InlineData(18, 50, "dez para as sete")] + [InlineData(19, 55, "cinco para as oito")] + [InlineData(20, 59, "nove em ponto")] + public void ConvertToRoundedClockNotationTimeOnlyStringWithCulturePtBr(int hours, int minutes, string expectedResult) + { + var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.NearestFiveMinutes, CultureInfo.CreateSpecificCulture("pt-BR")); + Assert.Equal(expectedResult, actualResult); + } } #endif diff --git a/src/Humanizer.Tests/Localisation/pt/TimeToClockNotationTests.cs b/src/Humanizer.Tests/Localisation/pt/TimeToClockNotationTests.cs index 960ffaf89..e484c79b7 100644 --- a/src/Humanizer.Tests/Localisation/pt/TimeToClockNotationTests.cs +++ b/src/Humanizer.Tests/Localisation/pt/TimeToClockNotationTests.cs @@ -1,4 +1,4 @@ -#if NET6_0_OR_GREATER +#if NET6_0_OR_GREATER namespace pt; @@ -28,6 +28,29 @@ public void ConvertToClockNotationTimeOnlyStringPtBr(int hours, int minutes, str Assert.Equal(expectedResult, actualResult); } + [Theory] + [InlineData(00, 00, "meia-noite")] + [InlineData(04, 00, "quatro horas")] + [InlineData(05, 01, "cinco e um")] + [InlineData(06, 05, "seis e cinco")] + [InlineData(07, 10, "sete e dez")] + [InlineData(08, 15, "oito e um quarto")] + [InlineData(09, 20, "nove e vinte")] + [InlineData(10, 25, "dez e vinte e cinco")] + [InlineData(11, 30, "onze e meia")] + [InlineData(12, 00, "meio-dia")] + [InlineData(15, 35, "três e trinta e cinco")] + [InlineData(16, 40, "cinco menos vinte")] + [InlineData(17, 45, "seis menos um quarto")] + [InlineData(18, 50, "sete menos dez")] + [InlineData(19, 55, "oito menos cinco")] + [InlineData(20, 59, "oito e cinquenta e nove")] + public void ConvertToClockNotationTimeOnlyStringWithCulturePtBr(int hours, int minutes, string expectedResult) + { + var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.None, CultureInfo.CreateSpecificCulture("pt")); + Assert.Equal(expectedResult, actualResult); + } + [Theory] [InlineData(00, 00, "meia-noite")] [InlineData(04, 00, "quatro horas")] @@ -52,6 +75,31 @@ public void ConvertToRoundedClockNotationTimeOnlyStringPtBr(int hours, int minut var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.NearestFiveMinutes); Assert.Equal(expectedResult, actualResult); } + + [Theory] + [InlineData(00, 00, "meia-noite")] + [InlineData(04, 00, "quatro horas")] + [InlineData(05, 01, "cinco horas")] + [InlineData(06, 05, "seis e cinco")] + [InlineData(07, 10, "sete e dez")] + [InlineData(08, 15, "oito e um quarto")] + [InlineData(09, 20, "nove e vinte")] + [InlineData(10, 25, "dez e vinte e cinco")] + [InlineData(11, 30, "onze e meia")] + [InlineData(12, 00, "meio-dia")] + [InlineData(13, 23, "uma e vinte e cinco")] + [InlineData(14, 32, "duas e meia")] + [InlineData(15, 35, "três e trinta e cinco")] + [InlineData(16, 40, "cinco menos vinte")] + [InlineData(17, 45, "seis menos um quarto")] + [InlineData(18, 50, "sete menos dez")] + [InlineData(19, 55, "oito menos cinco")] + [InlineData(20, 59, "nove horas")] + public void ConvertToRoundedClockNotationTimeOnlyStringWithCulturePtBr(int hours, int minutes, string expectedResult) + { + var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.NearestFiveMinutes, CultureInfo.CreateSpecificCulture("pt")); + Assert.Equal(expectedResult, actualResult); + } } #endif From 88799dd885e9394a57504f3a57b0d1f76c839696 Mon Sep 17 00:00:00 2001 From: Julian Katzwinkel Date: Sat, 24 Aug 2024 14:50:19 +0200 Subject: [PATCH 3/7] Change PtBr to Pt --- .../Localisation/pt/TimeToClockNotationTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Humanizer.Tests/Localisation/pt/TimeToClockNotationTests.cs b/src/Humanizer.Tests/Localisation/pt/TimeToClockNotationTests.cs index e484c79b7..15e41af4e 100644 --- a/src/Humanizer.Tests/Localisation/pt/TimeToClockNotationTests.cs +++ b/src/Humanizer.Tests/Localisation/pt/TimeToClockNotationTests.cs @@ -22,7 +22,7 @@ public class TimeToClockNotationTests [InlineData(18, 50, "sete menos dez")] [InlineData(19, 55, "oito menos cinco")] [InlineData(20, 59, "oito e cinquenta e nove")] - public void ConvertToClockNotationTimeOnlyStringPtBr(int hours, int minutes, string expectedResult) + public void ConvertToClockNotationTimeOnlyStringPt(int hours, int minutes, string expectedResult) { var actualResult = new TimeOnly(hours, minutes).ToClockNotation(); Assert.Equal(expectedResult, actualResult); @@ -45,7 +45,7 @@ public void ConvertToClockNotationTimeOnlyStringPtBr(int hours, int minutes, str [InlineData(18, 50, "sete menos dez")] [InlineData(19, 55, "oito menos cinco")] [InlineData(20, 59, "oito e cinquenta e nove")] - public void ConvertToClockNotationTimeOnlyStringWithCulturePtBr(int hours, int minutes, string expectedResult) + public void ConvertToClockNotationTimeOnlyStringWithCulturePt(int hours, int minutes, string expectedResult) { var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.None, CultureInfo.CreateSpecificCulture("pt")); Assert.Equal(expectedResult, actualResult); @@ -70,7 +70,7 @@ public void ConvertToClockNotationTimeOnlyStringWithCulturePtBr(int hours, int m [InlineData(18, 50, "sete menos dez")] [InlineData(19, 55, "oito menos cinco")] [InlineData(20, 59, "nove horas")] - public void ConvertToRoundedClockNotationTimeOnlyStringPtBr(int hours, int minutes, string expectedResult) + public void ConvertToRoundedClockNotationTimeOnlyStringPt(int hours, int minutes, string expectedResult) { var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.NearestFiveMinutes); Assert.Equal(expectedResult, actualResult); @@ -95,7 +95,7 @@ public void ConvertToRoundedClockNotationTimeOnlyStringPtBr(int hours, int minut [InlineData(18, 50, "sete menos dez")] [InlineData(19, 55, "oito menos cinco")] [InlineData(20, 59, "nove horas")] - public void ConvertToRoundedClockNotationTimeOnlyStringWithCulturePtBr(int hours, int minutes, string expectedResult) + public void ConvertToRoundedClockNotationTimeOnlyStringWithCulturePt(int hours, int minutes, string expectedResult) { var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.NearestFiveMinutes, CultureInfo.CreateSpecificCulture("pt")); Assert.Equal(expectedResult, actualResult); From 79337a5f2f3a15ab3346cf9e26584040060f8a8e Mon Sep 17 00:00:00 2001 From: Julian Katzwinkel Date: Sat, 24 Aug 2024 15:03:02 +0200 Subject: [PATCH 4/7] Fix tests --- ...cApiApprovalTest.Approve_Public_Api.DotNet8_0.verified.txt | 2 +- .../Localisation/pt/TimeToClockNotationTests.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.Approve_Public_Api.DotNet8_0.verified.txt b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.Approve_Public_Api.DotNet8_0.verified.txt index 63bdf7959..4e2b9b7cd 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.Approve_Public_Api.DotNet8_0.verified.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.Approve_Public_Api.DotNet8_0.verified.txt @@ -367,7 +367,7 @@ namespace Humanizer } public interface ITimeOnlyToClockNotationConverter { - string Convert(System.TimeOnly time, Humanizer.ClockNotationRounding roundToNearestFive); + string Convert(System.TimeOnly time, Humanizer.ClockNotationRounding roundToNearestFive, System.Globalization.CultureInfo? culture); } public interface ITruncator { diff --git a/src/Humanizer.Tests/Localisation/pt/TimeToClockNotationTests.cs b/src/Humanizer.Tests/Localisation/pt/TimeToClockNotationTests.cs index 15e41af4e..a9e449b18 100644 --- a/src/Humanizer.Tests/Localisation/pt/TimeToClockNotationTests.cs +++ b/src/Humanizer.Tests/Localisation/pt/TimeToClockNotationTests.cs @@ -47,7 +47,7 @@ public void ConvertToClockNotationTimeOnlyStringPt(int hours, int minutes, strin [InlineData(20, 59, "oito e cinquenta e nove")] public void ConvertToClockNotationTimeOnlyStringWithCulturePt(int hours, int minutes, string expectedResult) { - var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.None, CultureInfo.CreateSpecificCulture("pt")); + var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.None, new("pt")); Assert.Equal(expectedResult, actualResult); } @@ -97,7 +97,7 @@ public void ConvertToRoundedClockNotationTimeOnlyStringPt(int hours, int minutes [InlineData(20, 59, "nove horas")] public void ConvertToRoundedClockNotationTimeOnlyStringWithCulturePt(int hours, int minutes, string expectedResult) { - var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.NearestFiveMinutes, CultureInfo.CreateSpecificCulture("pt")); + var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.NearestFiveMinutes, new("pt")); Assert.Equal(expectedResult, actualResult); } } From 4143f9453ee29219725513623d93736bd0648ace Mon Sep 17 00:00:00 2001 From: Julian Katzwinkel Date: Sat, 24 Aug 2024 15:03:55 +0200 Subject: [PATCH 5/7] Fix culture missing in ApiApprover --- ...licApiApprovalTest.Approve_Public_Api.DotNet8_0.verified.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.Approve_Public_Api.DotNet8_0.verified.txt b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.Approve_Public_Api.DotNet8_0.verified.txt index 4e2b9b7cd..1b7f1d2f2 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.Approve_Public_Api.DotNet8_0.verified.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.Approve_Public_Api.DotNet8_0.verified.txt @@ -1873,7 +1873,7 @@ namespace Humanizer } public static class TimeOnlyToClockNotationExtensions { - public static string ToClockNotation(this System.TimeOnly input, Humanizer.ClockNotationRounding roundToNearestFive = 0) { } + public static string ToClockNotation(this System.TimeOnly input, Humanizer.ClockNotationRounding roundToNearestFive = 0, System.Globalization.CultureInfo? culture) { } } public static class TimeSpanHumanizeExtensions { From d9bf5a4fc2cf470800d67e20e67e0ba04af90f79 Mon Sep 17 00:00:00 2001 From: Julian Katzwinkel Date: Sat, 24 Aug 2024 15:04:34 +0200 Subject: [PATCH 6/7] Add default null value --- ...licApiApprovalTest.Approve_Public_Api.DotNet8_0.verified.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.Approve_Public_Api.DotNet8_0.verified.txt b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.Approve_Public_Api.DotNet8_0.verified.txt index 1b7f1d2f2..0eb8c183f 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.Approve_Public_Api.DotNet8_0.verified.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.Approve_Public_Api.DotNet8_0.verified.txt @@ -1873,7 +1873,7 @@ namespace Humanizer } public static class TimeOnlyToClockNotationExtensions { - public static string ToClockNotation(this System.TimeOnly input, Humanizer.ClockNotationRounding roundToNearestFive = 0, System.Globalization.CultureInfo? culture) { } + public static string ToClockNotation(this System.TimeOnly input, Humanizer.ClockNotationRounding roundToNearestFive = 0, System.Globalization.CultureInfo? culture = null) { } } public static class TimeSpanHumanizeExtensions { From 590d25b9bd62a03fae98393f21d40b0285ff36f1 Mon Sep 17 00:00:00 2001 From: Julian Katzwinkel Date: Sat, 24 Aug 2024 17:42:05 +0200 Subject: [PATCH 7/7] Fix Benchmark --- ...eOnlyToClockNotationConverterBenchmarks.cs | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Benchmarks/TimeOnlyToClockNotationConverterBenchmarks.cs b/src/Benchmarks/TimeOnlyToClockNotationConverterBenchmarks.cs index 1e2918628..8ede932a5 100644 --- a/src/Benchmarks/TimeOnlyToClockNotationConverterBenchmarks.cs +++ b/src/Benchmarks/TimeOnlyToClockNotationConverterBenchmarks.cs @@ -1,4 +1,4 @@ -[MemoryDiagnoser(false)] +[MemoryDiagnoser(false)] public class TimeOnlyToClockNotationConverterBenchmarks { static TimeOnly time = new(13, 6, 2, 88, 987); @@ -7,39 +7,39 @@ public class TimeOnlyToClockNotationConverterBenchmarks [Benchmark] public void EsClockNotationConverter() { - esConverter.Convert(time, ClockNotationRounding.None); - esConverter.Convert(time, ClockNotationRounding.NearestFiveMinutes); + esConverter.Convert(time, ClockNotationRounding.None, null); + esConverter.Convert(time, ClockNotationRounding.NearestFiveMinutes, null); } static BrazilianPortugueseTimeOnlyToClockNotationConverter brazilianConverter = new(); [Benchmark] public void BrazilianPortugueseClockNotationConverter() { - brazilianConverter.Convert(time, ClockNotationRounding.None); - brazilianConverter.Convert(time, ClockNotationRounding.NearestFiveMinutes); + brazilianConverter.Convert(time, ClockNotationRounding.None, null); + brazilianConverter.Convert(time, ClockNotationRounding.NearestFiveMinutes, null); } static FrTimeOnlyToClockNotationConverter frConverter = new(); [Benchmark] public void FrClockNotationConverter() { - frConverter.Convert(time, ClockNotationRounding.None); - frConverter.Convert(time, ClockNotationRounding.NearestFiveMinutes); + frConverter.Convert(time, ClockNotationRounding.None, null); + frConverter.Convert(time, ClockNotationRounding.NearestFiveMinutes, null); } static LbTimeOnlyToClockNotationConverter lbConverter = new(); [Benchmark] public void LbClockNotationConverter() { - lbConverter.Convert(time, ClockNotationRounding.None); - lbConverter.Convert(time, ClockNotationRounding.NearestFiveMinutes); + lbConverter.Convert(time, ClockNotationRounding.None, null); + lbConverter.Convert(time, ClockNotationRounding.NearestFiveMinutes, null); } static DefaultTimeOnlyToClockNotationConverter defaultConverter = new(); [Benchmark] public void DefaultClockNotationConverter() { - defaultConverter.Convert(time, ClockNotationRounding.None); - defaultConverter.Convert(time, ClockNotationRounding.NearestFiveMinutes); + defaultConverter.Convert(time, ClockNotationRounding.None, null); + defaultConverter.Convert(time, ClockNotationRounding.NearestFiveMinutes, null); } } \ No newline at end of file