-
Notifications
You must be signed in to change notification settings - Fork 1k
added German TimeOnlyToClockNotation #1249
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
151a0d8
7b6f44b
8b16f94
d41f0bf
0df5da4
9021c0b
c035163
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
|
|
||
| [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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
|
||
| 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.
Outdated
|
||
| { | ||
| /// <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.
Outdated
|
||
| { | ||
| 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 | ||
Uh oh!
There was an error while loading. Please reload this page.