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

added German TimeOnlyToClockNotation #1249

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<Compile Include="$(MSBuildThisFileDirectory)DateHumanizeDefaultStrategyTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FluentDate\InDateTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FluentDate\OnDateTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Localisation\de\TimeToClockNotationTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Localisation\en-IN\NumberToWordsTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Localisation\en\TimeToClockNotationTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Localisation\es\DateToOrdinalWordsTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#if NET6_0_OR_GREATER

using System;
using System.Diagnostics.CodeAnalysis;

using Humanizer;
using Humanizer.Tests;

using Xunit;

namespace org.SpocWeb.root.NaturalLang.Tests.de;
SpocWeb marked this conversation as resolved.
Show resolved Hide resolved

[UseCulture("de-DE")]
[SuppressMessage("ReSharper", "StringLiteralTypo")]
public static class TimeToClockNotationTests
{
#region [InlineData(0, 0, "")]

[Theory]
[InlineData(0, 0, "Mitternacht")]
[InlineData(0, 7, "12 Uhr 7 nachts")]
[InlineData(1, 11, "1 Uhr 11 nachts")]
[InlineData(4, 0, "4 Uhr nachts")]
[InlineData(5, 1, "5 Uhr 1 nachts")]
[InlineData(6, 0, "6 Uhr morgens")]
[InlineData(6, 5, "fünf nach 6 morgens")]
[InlineData(7, 10, "zehn nach 7 morgens")]
[InlineData(8, 15, "Viertel nach 8 morgens")]
[InlineData(9, 20, "zwanzig nach 9 morgens")]
[InlineData(10, 25, "fünf vor halb 11 morgens")]
[InlineData(11, 30, "halb 12 morgens")]
[InlineData(12, 00, "Mittag")]
[InlineData(12, 38, "12 Uhr 38 nachmittags")]
[InlineData(12, 35, "fünf nach halb 1 nachmittags")]
[InlineData(15, 40, "zwanzig vor 4 nachmittags")]
[InlineData(17, 45, "Viertel vor 6 nachmittags")]
[InlineData(19, 50, "zehn vor 8 abends")]
[InlineData(21, 0, "9 Uhr abends")]
[InlineData(21, 55, "fünf vor 10 abends")]
[InlineData(22, 59, "10 Uhr 59 abends")]
[InlineData(23, 43, "11 Uhr 43 abends")]

#endregion [InlineData(0, 0, "")]

public static void ConvertToClockNotationTimeOnlyStringDe(int hours, int minutes, string expectedResult)
{
var actualResult = new TimeOnly(hours, minutes).ToClockNotation();
Assert.Equal(expectedResult, actualResult);
}


#region [InlineData(0, 0, "")]

[Theory]
[InlineData(0, 0, "Mitternacht")]
[InlineData(0, 7, "fünf nach 12 nachts")]
[InlineData(1, 11, "zehn nach 1 nachts")]
[InlineData(4, 0, "4 Uhr nachts")]
[InlineData(5, 1, "5 Uhr nachts")]
[InlineData(6, 0, "6 Uhr morgens")]
[InlineData(6, 5, "fünf nach 6 morgens")]
[InlineData(7, 10, "zehn nach 7 morgens")]
[InlineData(8, 15, "Viertel nach 8 morgens")]
[InlineData(9, 20, "zwanzig nach 9 morgens")]
[InlineData(10, 25, "fünf vor halb 11 morgens")]
[InlineData(11, 30, "halb 12 morgens")]
[InlineData(12, 00, "Mittag")]
[InlineData(12, 38, "zwanzig vor 1 nachmittags")]
[InlineData(12, 35, "fünf nach halb 1 nachmittags")]
[InlineData(15, 40, "zwanzig vor 4 nachmittags")]
[InlineData(17, 45, "Viertel vor 6 nachmittags")]
[InlineData(19, 50, "zehn vor 8 abends")]
[InlineData(21, 0, "9 Uhr abends")]
[InlineData(21, 55, "fünf vor 10 abends")]
[InlineData(22, 59, "11 Uhr abends")]
[InlineData(23, 43, "Viertel vor 12 abends")]
[InlineData(23, 58, "Mitternacht")]

#endregion [InlineData(0, 0, "")]

public static void ConvertToRoundedClockNotationTimeOnlyStringPtBr(int hours, int minutes, string expectedResult)
{
var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.NearestFiveMinutes);
Assert.Equal(expectedResult, actualResult);
}
}

#endif
2 changes: 1 addition & 1 deletion src/Humanizer.Tests.Shared/NumberToWordsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void ToTuple(int number, string expected)
[InlineData(11, "ta", "பதினொன்று")]
[InlineData(12, "ta", "பனிரெண்டு")]
[InlineData(555, "ta", "ஐந்நூற்று ஐம்பத்து ஐந்து")]
public void ToWords_CanSpecifyCultureExplicitly(int number, string culture, string expected)
public void b(int number, string culture, string expected)
SpocWeb marked this conversation as resolved.
Show resolved Hide resolved
{
Assert.Equal(expected, number.ToWords(new CultureInfo(culture)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1985,6 +1985,22 @@ namespace Humanizer.Localisation.Ordinalizers
}
namespace Humanizer.Localisation.TimeToClockNotation
{
public class DeTimeOnlyToClockNotationConverter : Humanizer.Localisation.TimeToClockNotation.ITimeOnlyToClockNotationConverter
{
public bool AsWords;
public System.Nullable<bool> PrePendQuarter;
public DeTimeOnlyToClockNotationConverter() { }
public string Convert(System.TimeOnly time, Humanizer.ClockNotationRounding roundToNearestFive) { }
}
public class static German
Copy link
Contributor

Choose a reason for hiding this comment

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

Someone that knows the API policies of the project better than me should decide whether this change is appropriate, considering the existing public API.

Copy link
Collaborator

Choose a reason for hiding this comment

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

does it need to be public? if so can you add some docs that explain the use cases

Copy link
Member

Choose a reason for hiding this comment

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

I would suggest to make it internal for now.

{
public const string MidDay = "Mittag";
public const string MidNight = "Mitternacht";
public static readonly System.Collections.Generic.IReadOnlyList<string> QuarterDay;
public static string AsClockDe(this System.TimeOnly time, Humanizer.ClockNotationRounding round, bool asWords = False) { }
public static string GetDayQuarterGerman(this System.TimeOnly time) { }
public static string GetDayQuarterGermanGenitive(this System.TimeOnly time) { }
}
public interface ITimeOnlyToClockNotationConverter
{
string Convert(System.TimeOnly time, Humanizer.ClockNotationRounding roundToNearestFive);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public TimeOnlyToClockNotationConvertersRegistry() : base(new DefaultTimeOnlyToC
Register("pt-BR", new BrazilianPortugueseTimeOnlyToClockNotationConverter());
Register("fr", new FrTimeOnlyToClockNotationConverter());
Register("es", new EsTimeOnlyToClockNotationConverter());
Register("de", new DeTimeOnlyToClockNotationConverter());
}
}
}
Expand Down
23 changes: 8 additions & 15 deletions src/Humanizer/Localisation/Ordinalizers/EnglishOrdinalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,18 @@ public override string Convert(int number, string numberString)
{
var nMod100 = number % 100;

if (nMod100 >= 11 && nMod100 <= 13)
if (nMod100 is >= 11 and <= 13)
{
return numberString + "th";
}

switch (number % 10)
return (number % 10) switch
{
case 1:
return numberString + "st";

case 2:
return numberString + "nd";

case 3:
return numberString + "rd";

default:
return numberString + "th";
}
1 => numberString + "st",
2 => numberString + "nd",
3 => numberString + "rd",
_ => numberString + "th"
};
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#if NET6_0_OR_GREATER

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace Humanizer.Localisation.TimeToClockNotation;

public class DeTimeOnlyToClockNotationConverter : ITimeOnlyToClockNotationConverter
SpocWeb marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary> Switch to output Digits as Words </summary>
public bool AsWords;

/// <summary> Used to pre-pend, append or omit the Quarter of the Day </summary>
public bool? PrePendQuarter = false;

public string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive)
{
var ret = time.AsClockDe(roundToNearestFive, AsWords);
if (!PrePendQuarter.HasValue
|| ret == German.MidNight
|| ret == German.MidDay)
return ret;

return PrePendQuarter.Value switch
{
false => ret + ' ' + time.GetDayQuarterGermanGenitive(),
true => time.GetDayQuarterGermanGenitive() + ' ' + ret
};
}
}

public static class German
SpocWeb marked this conversation as resolved.
Show resolved Hide resolved
{
public const string MidNight = "Mitternacht";
public const string MidDay = "Mittag";

public static readonly IReadOnlyList<string> QuarterDay = new[] {"Nacht", "Morgen", "Nachmittag", "Abend"};

public static string GetDayQuarterGerman(this TimeOnly time) => QuarterDay[time.Hour / 6];
public static string GetDayQuarterGermanGenitive(this TimeOnly time) => GetDayQuarterGerman(time).ToLower() + "s";

/// <summary> Use 12 Hours but avoid 0 </summary>
private static int NormalizeHour(TimeOnly time) => time.Hour % 12 != 0 ? (time.Hour % 12) : 12;

[SuppressMessage("ReSharper", "StringLiteralTypo")]
public static string AsClockDe(this TimeOnly time, ClockNotationRounding round, bool asWords = false)
{
if (round == ClockNotationRounding.NearestFiveMinutes)
{
var ticks = 5 * TimeSpan.TicksPerMinute;
var quotient = (time.Ticks + ticks / 2) / ticks;
var total = quotient * ticks;
if (total >= TimeSpan.TicksPerDay)
{
total -= TimeSpan.TicksPerDay;
}
time = new TimeOnly(total);
}
switch (time)
{
case { Hour: 0, Minute: 0 }: return MidNight;
case { Hour: 12, Minute: 0 }: return MidDay;
}

var addHour = time.AddHours(1);
var hours = NormalizeHour(time);
var hour = asWords ? hours.ToWords() : hours.ToString();
var nextHours = NormalizeHour(addHour);
var nextHour = asWords ? nextHours.ToWords() : nextHours.ToString();

return time.Minute switch
{
00 => $"{hour} Uhr",
05 => $"fünf nach {hour}",
10 => $"zehn nach {hour}",
15 => $"Viertel nach {hour}",
20 => $"zwanzig nach {hour}",
25 => $"fünf vor halb {nextHour}",
30 => $"halb {nextHour}",
35 => $"fünf nach halb {nextHour}",
40 => $"zwanzig vor {nextHour}",
45 => $"Viertel vor {nextHour}",
50 => $"zehn vor {nextHour}",
55 => $"fünf vor {nextHour}",
60 => $"{nextHour} Uhr",
_ => $"{hour} Uhr {time.Minute}" //.AsWords()
};
}

}

#endif