-
Notifications
You must be signed in to change notification settings - Fork 965
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1134 from strobelt/clocktime-timeonly-strategy
- Loading branch information
Showing
12 changed files
with
344 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/Humanizer.Tests.Shared/Localisation/en/TimeToClockNotationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#if NET6_0_OR_GREATER | ||
|
||
using System; | ||
using Xunit; | ||
using Humanizer.Localisation.TimeToClockNotation; | ||
using Humanizer; | ||
|
||
namespace Humanizer.Tests.Localisation.en | ||
{ | ||
[UseCulture("en")] | ||
public class TimeToClockNotationTests | ||
{ | ||
[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 ConvertToClockNotationTimeOnlyStringEnUs(int hours, int minutes, string expectedResult) | ||
{ | ||
var actualResult = new TimeOnly(hours, minutes).ToClockNotation(); | ||
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 ConvertToRoundedClockNotationTimeOnlyStringEnUs(int hours, int minutes, string expectedResult) | ||
{ | ||
var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.NearestFiveMinutes); | ||
Assert.Equal(expectedResult, actualResult); | ||
} | ||
} | ||
} | ||
|
||
#endif |
63 changes: 63 additions & 0 deletions
63
src/Humanizer.Tests.Shared/Localisation/pt-BR/TimeToClockNotationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#if NET6_0_OR_GREATER | ||
|
||
using System; | ||
using Xunit; | ||
using Humanizer.Localisation.TimeToClockNotation; | ||
using Humanizer; | ||
|
||
namespace Humanizer.Tests.Localisation.ptBR | ||
{ | ||
[UseCulture("pt-BR")] | ||
public class TimeToClockNotationTests | ||
{ | ||
[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 ConvertToClockNotationTimeOnlyStringPtBr(int hours, int minutes, string expectedResult) | ||
{ | ||
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 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 ConvertToRoundedClockNotationTimeOnlyStringPtBr(int hours, int minutes, string expectedResult) | ||
{ | ||
var actualResult = new TimeOnly(hours, minutes).ToClockNotation(ClockNotationRounding.NearestFiveMinutes); | ||
Assert.Equal(expectedResult, actualResult); | ||
} | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Humanizer | ||
{ | ||
public enum ClockNotationRounding | ||
{ | ||
None, | ||
NearestFiveMinutes | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Humanizer/Configuration/TimeOnlyToClockNotationConvertersRegistry.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#if NET6_0_OR_GREATER | ||
|
||
using Humanizer.Localisation.TimeToClockNotation; | ||
|
||
namespace Humanizer.Configuration | ||
{ | ||
internal class TimeOnlyToClockNotationConvertersRegistry : LocaliserRegistry<ITimeOnlyToClockNotationConverter> | ||
{ | ||
public TimeOnlyToClockNotationConvertersRegistry() : base(new DefaultTimeOnlyToClockNotationConverter()) | ||
{ | ||
Register("pt-BR", new BrazilianPortugueseTimeOnlyToClockNotationConverter()); | ||
} | ||
} | ||
} | ||
|
||
#endif |
41 changes: 41 additions & 0 deletions
41
...r/Localisation/TimeToClockNotation/BrazilianPortugueseTimeOnlyToClockNotationConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#if NET6_0_OR_GREATER | ||
|
||
using System; | ||
|
||
using Humanizer; | ||
|
||
namespace Humanizer.Localisation.TimeToClockNotation | ||
{ | ||
internal class BrazilianPortugueseTimeOnlyToClockNotationConverter : ITimeOnlyToClockNotationConverter | ||
{ | ||
public virtual string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive) | ||
{ | ||
switch (time) | ||
{ | ||
case { Hour: 0, Minute: 0 }: | ||
return "meia-noite"; | ||
case { Hour: 12, Minute: 0 }: | ||
return "meio-dia"; | ||
} | ||
|
||
var normalizedHour = time.Hour % 12; | ||
var normalizedMinutes = (int)(roundToNearestFive == ClockNotationRounding.NearestFiveMinutes | ||
? 5 * Math.Round(time.Minute / 5.0) | ||
: time.Minute); | ||
|
||
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()}" | ||
}; | ||
} | ||
} | ||
} | ||
|
||
#endif |
46 changes: 46 additions & 0 deletions
46
src/Humanizer/Localisation/TimeToClockNotation/DefaultTimeOnlyToClockNotationConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#if NET6_0_OR_GREATER | ||
|
||
using System; | ||
|
||
using Humanizer; | ||
|
||
namespace Humanizer.Localisation.TimeToClockNotation | ||
{ | ||
internal class DefaultTimeOnlyToClockNotationConverter : ITimeOnlyToClockNotationConverter | ||
{ | ||
public virtual string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive) | ||
{ | ||
switch (time) | ||
{ | ||
case { Hour: 0, Minute: 0 }: | ||
return "midnight"; | ||
case { Hour: 12, Minute: 0 }: | ||
return "noon"; | ||
} | ||
|
||
var normalizedHour = time.Hour % 12; | ||
var normalizedMinutes = (int)(roundToNearestFive == ClockNotationRounding.NearestFiveMinutes | ||
? 5 * Math.Round(time.Minute / 5.0) | ||
: time.Minute); | ||
|
||
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()}" | ||
}; | ||
} | ||
} | ||
} | ||
|
||
#endif |
24 changes: 24 additions & 0 deletions
24
src/Humanizer/Localisation/TimeToClockNotation/ITimeOnlyToClockNotationConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#if NET6_0_OR_GREATER | ||
|
||
using System; | ||
|
||
using Humanizer; | ||
|
||
namespace Humanizer.Localisation.TimeToClockNotation | ||
{ | ||
/// <summary> | ||
/// The interface used to localise the ToClockNotation method. | ||
/// </summary> | ||
public interface ITimeOnlyToClockNotationConverter | ||
{ | ||
/// <summary> | ||
/// Converts the time to Clock Notation | ||
/// </summary> | ||
/// <param name="time"></param> | ||
/// <param name="roundToNearestFive"></param> | ||
/// <returns></returns> | ||
string Convert(TimeOnly time, ClockNotationRounding roundToNearestFive); | ||
} | ||
} | ||
|
||
#endif |
Oops, something went wrong.