Skip to content

Instantly share code, notes, and snippets.

@AspireOne
Last active November 14, 2020 15:15
Show Gist options
  • Save AspireOne/de192bbf0f85e93096a0a35c50623f0c to your computer and use it in GitHub Desktop.
Save AspireOne/de192bbf0f85e93096a0a35c50623f0c to your computer and use it in GitHub Desktop.
This method will return a correctly inclined czech month based on the passed month number and case (C# 8 and C#9 ver.)
static readonly string[] DeclensedCzechMonthNamesBase = { "ledn", "únor", "březn", "dubn", "květn", "červn", "červenc",
"srpn", "září", "říjn", "listopad", "prosinc"};
static readonly string[] FullCzechMonthNames = { "leden", "únor", "březen", "duben", "květen", "červen", "červenec",
"srpen", "září", "říjen", "listopad", "prosinec" };
// C#8
public static string GetCzechMonthName(byte month, byte @case = 1)
{
if (@case < 1 || @case > 7)
throw new ArgumentException($"case must be within the range 1-7 (was {@case})");
if (month < 1 || month > 12)
throw new ArgumentException($"Month must be within the range 1-12 (was {month})");
string @base = DeclensedCzechMonthNamesBase[month - 1];
if (month == 9)
return @base + (@case == 7 ? "m" : "");
// C# 8 switch expression - change to if-else or switch statement if your c# version is < 8.
return @case switch
{
var x when x == 3 || x == 6 => @base + (month == 7 || month == 12 ? "i" : "u"),
2 => @base + (month == 9 || month == 12 ? "e" : (month == 11 ? "u" : "a")),
7 => @base + (month == 9 ? "m" : "em"),
_ => FullCzechMonthNames[month - 1]
};
}
// C#9
public static string GetCzechMonthName(byte month, byte @case = 1)
{
if (@case is < 1 or > 7)
throw new ArgumentException($"case must be within the range 1-7 (was {@case})");
if (month is < 1 or > 12)
throw new ArgumentException($"Month must be within the range 1-12 (was {month})");
string @base = DeclensedCzechMonthNamesBase[month - 1];
if (month is 9)
return @base + (@case is 7 ? "m" : "");
// C# 8 switch expression and C#9 pattern matching.
return @case switch
{
byte x when x is 3 or 6 => @base + (month is 7 or 12 ? "i" : "u"),
2 => @base + (month is 9 or 12 ? "e" : (month is 11 ? "u" : "a")),
7 => @base + (month is 9 ? "m" : "em"),
_ => FullCzechMonthNames[month - 1]
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment