From e95fcc07300dd4ba51147822316724de8f9416da Mon Sep 17 00:00:00 2001 From: Blake Niemyjski Date: Fri, 8 Sep 2023 06:20:40 -0500 Subject: [PATCH] To switch expressions --- .../FormatParsers/ExplicitDateFormatParser.cs | 16 +++++--------- .../FormatParsers/FormatParsers/Helper.cs | 22 ++++++++----------- .../PartParsers/ExplicitDatePartParser.cs | 16 +++++--------- 3 files changed, 21 insertions(+), 33 deletions(-) diff --git a/src/Exceptionless.DateTimeExtensions/FormatParsers/FormatParsers/ExplicitDateFormatParser.cs b/src/Exceptionless.DateTimeExtensions/FormatParsers/FormatParsers/ExplicitDateFormatParser.cs index dd0cddb..10b0ef2 100644 --- a/src/Exceptionless.DateTimeExtensions/FormatParsers/FormatParsers/ExplicitDateFormatParser.cs +++ b/src/Exceptionless.DateTimeExtensions/FormatParsers/FormatParsers/ExplicitDateFormatParser.cs @@ -22,16 +22,12 @@ public DateTimeRange Parse(string content, DateTimeOffset relativeBaseTime) { return null; date = date.ChangeOffset(relativeBaseTime.Offset); - switch (content.Length) { - case 10: - return new DateTimeRange(date, date.EndOfDay()); - case 13: - return new DateTimeRange(date, date.EndOfHour()); - case 16: - return new DateTimeRange(date, date.EndOfMinute()); - default: - return new DateTimeRange(date, date.EndOfSecond()); - } + return content.Length switch { + 10 => new DateTimeRange(date, date.EndOfDay()), + 13 => new DateTimeRange(date, date.EndOfHour()), + 16 => new DateTimeRange(date, date.EndOfMinute()), + _ => new DateTimeRange(date, date.EndOfSecond()) + }; } } } \ No newline at end of file diff --git a/src/Exceptionless.DateTimeExtensions/FormatParsers/FormatParsers/Helper.cs b/src/Exceptionless.DateTimeExtensions/FormatParsers/FormatParsers/Helper.cs index b903802..53967d6 100644 --- a/src/Exceptionless.DateTimeExtensions/FormatParsers/FormatParsers/Helper.cs +++ b/src/Exceptionless.DateTimeExtensions/FormatParsers/FormatParsers/Helper.cs @@ -11,19 +11,15 @@ internal static class Helper { internal static readonly List MonthNames = new(new[] { "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" }); internal static TimeSpan GetTimeSpanFromName(string name) { - switch (name.ToLower()) { - case "minutes": - case "minute": - return TimeSpan.FromMinutes(1); - case "hours": - case "hour": - return TimeSpan.FromHours(1); - case "days": - case "day": - return TimeSpan.FromDays(1); - default: - return TimeSpan.Zero; - } + return name.ToLower() switch { + "minutes" => TimeSpan.FromMinutes(1), + "minute" => TimeSpan.FromMinutes(1), + "hours" => TimeSpan.FromHours(1), + "hour" => TimeSpan.FromHours(1), + "days" => TimeSpan.FromDays(1), + "day" => TimeSpan.FromDays(1), + _ => TimeSpan.Zero + }; } internal static int GetMonthNumber(string name) { diff --git a/src/Exceptionless.DateTimeExtensions/FormatParsers/FormatParsers/PartParsers/ExplicitDatePartParser.cs b/src/Exceptionless.DateTimeExtensions/FormatParsers/FormatParsers/PartParsers/ExplicitDatePartParser.cs index 26be473..608fd58 100644 --- a/src/Exceptionless.DateTimeExtensions/FormatParsers/FormatParsers/PartParsers/ExplicitDatePartParser.cs +++ b/src/Exceptionless.DateTimeExtensions/FormatParsers/FormatParsers/PartParsers/ExplicitDatePartParser.cs @@ -21,16 +21,12 @@ public class ExplicitDatePartParser : IPartParser { if (!isUpperLimit) return date; - switch (match.Length) { - case 10: - return date.EndOfDay(); - case 13: - return date.EndOfHour(); - case 16: - return date.EndOfMinute(); - default: - return date; - } + return match.Length switch { + 10 => date.EndOfDay(), + 13 => date.EndOfHour(), + 16 => date.EndOfMinute(), + _ => date + }; } } } \ No newline at end of file