Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change TimeOnly.ToClockNotation to accept culture #1537

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/Benchmarks/TimeOnlyToClockNotationConverterBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[MemoryDiagnoser(false)]
[MemoryDiagnoser(false)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first line of every file you modified seems to be changed. Is this a modified BOM? I'm not sure if there's a preferred version in the project, but you might want to check this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All I know is I didn't actually touch that part. 🤔
Has VS some kind of default setting? If yes then that's what changed.
I don't see any problems happening here though and kinda don't know how I could change that. Git inside VS doesn't show them as changed.
grafik

public class TimeOnlyToClockNotationConverterBenchmarks
{
static TimeOnly time = new(13, 6, 2, 88, 987);
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 = null) { }
}
public static class TimeSpanHumanizeExtensions
{
Expand Down
50 changes: 49 additions & 1 deletion src/Humanizer.Tests/Localisation/en/TimeToClockNotationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NET6_0_OR_GREATER
#if NET6_0_OR_GREATER

namespace en;

Expand Down Expand Up @@ -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")]
Expand All @@ -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
60 changes: 59 additions & 1 deletion src/Humanizer.Tests/Localisation/es/TimeToClockNotationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NET6_0_OR_GREATER
#if NET6_0_OR_GREATER

namespace es;

Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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
55 changes: 54 additions & 1 deletion src/Humanizer.Tests/Localisation/fr/TimeToClockNotationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NET6_0_OR_GREATER
#if NET6_0_OR_GREATER

namespace fr;

Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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
61 changes: 60 additions & 1 deletion src/Humanizer.Tests/Localisation/lb/TimeToClockNotationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NET6_0_OR_GREATER
#if NET6_0_OR_GREATER

namespace lb;

Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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
Loading