From ec45fc2d353d6f35216cfe8f6212901e57027d3d Mon Sep 17 00:00:00 2001 From: Ambar Date: Wed, 13 Nov 2024 10:27:07 +0530 Subject: [PATCH] TimeExtensions.cs Added null checks and exception handling to prevent runtime errors. Improved conditional logic in the IsBetween method. Added XML documentation comments for better code readability and maintainability. Improved string parsing in ToTimeOfDay with robust error handling. --- src/DateTimeExtensions/TimeExtensions.cs | 97 ++++++++++++++++-------- 1 file changed, 65 insertions(+), 32 deletions(-) diff --git a/src/DateTimeExtensions/TimeExtensions.cs b/src/DateTimeExtensions/TimeExtensions.cs index 73d120c..9c4df02 100644 --- a/src/DateTimeExtensions/TimeExtensions.cs +++ b/src/DateTimeExtensions/TimeExtensions.cs @@ -1,4 +1,4 @@ -#region License +#region License // // Copyright (c) 2011-2012, João Matos Silva @@ -19,71 +19,104 @@ #endregion using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using DateTimeExtensions.TimeOfDay; namespace DateTimeExtensions { public static class TimeExtensions { + /// + /// Adds a specified Time to a DateTime. + /// + /// The base DateTime. + /// The Time to add. + /// A DateTime with the added time. public static DateTime AddTime(this DateTime dateTime, Time timeToAdd) { - return dateTime.AddSeconds(timeToAdd.Second).AddMinutes(timeToAdd.Minute).AddHours(timeToAdd.Hour); + if (timeToAdd == null) throw new ArgumentNullException(nameof(timeToAdd)); + return dateTime.AddSeconds(timeToAdd.Second) + .AddMinutes(timeToAdd.Minute) + .AddHours(timeToAdd.Hour); } + /// + /// Sets the time of a DateTime to a specified Time, keeping the original date. + /// + /// The base DateTime. + /// The Time to set. + /// A DateTime with the set time. public static DateTime SetTime(this DateTime dateTime, Time timeToAdd) { + if (timeToAdd == null) throw new ArgumentNullException(nameof(timeToAdd)); return AddTime(dateTime.Date, timeToAdd); } + /// + /// Gets the Time representation of a DateTime's time component. + /// + /// The DateTime to extract the time from. + /// A Time instance representing the time component. public static Time TimeOfTheDay(this DateTime dateTime) { return new Time(dateTime.Hour, dateTime.Minute, dateTime.Second); } + /// + /// Checks if a DateTime's time is between two specified times. + /// + /// The DateTime to check. + /// The start Time of the range. + /// The end Time of the range. + /// True if the time is between startTime and endTime, otherwise false. public static bool IsBetween(this DateTime dateTime, Time startTime, Time endTime) { + if (startTime == null || endTime == null) throw new ArgumentNullException(); + var currentTime = dateTime.TimeOfTheDay(); - //start time is lesser or equal than end time - if (startTime.CompareTo(endTime) <= 0) - { - //currentTime should be between start time and end time - if (currentTime.CompareTo(startTime) >= 0 && currentTime.CompareTo(endTime) <= 0) - { - return true; - } - return false; - } - else - { - //currentTime should be between end time time and start time - if (currentTime.CompareTo(startTime) >= 0 || currentTime.CompareTo(endTime) <= 0) - { - return true; - } - return false; - } + + // Simplified conditional logic + return startTime.CompareTo(endTime) <= 0 + ? currentTime.CompareTo(startTime) >= 0 && currentTime.CompareTo(endTime) <= 0 + : currentTime.CompareTo(startTime) >= 0 || currentTime.CompareTo(endTime) <= 0; } + /// + /// Checks if a DateTime's time is before a specified time. + /// + /// The DateTime to check. + /// The Time to compare against. + /// True if the DateTime's time is before the specified time, otherwise false. public static bool IsBefore(this DateTime dateTime, Time time) { - var currentTime = dateTime.TimeOfTheDay(); - //currentTime should be lesser than time - return currentTime.CompareTo(time) < 0; + if (time == null) throw new ArgumentNullException(nameof(time)); + return dateTime.TimeOfTheDay().CompareTo(time) < 0; } + /// + /// Checks if a DateTime's time is after a specified time. + /// + /// The DateTime to check. + /// The Time to compare against. + /// True if the DateTime's time is after the specified time, otherwise false. public static bool IsAfter(this DateTime dateTime, Time time) { - var currentTime = dateTime.TimeOfTheDay(); - //currentTime should be greater than time - return currentTime.CompareTo(time) > 0; + if (time == null) throw new ArgumentNullException(nameof(time)); + return dateTime.TimeOfTheDay().CompareTo(time) > 0; } + /// + /// Parses a string to a Time instance. + /// + /// The string representing the time. + /// A Time instance parsed from the string. public static Time ToTimeOfDay(this string timeValueString) { - return Time.Parse(timeValueString); + if (string.IsNullOrWhiteSpace(timeValueString)) + throw new ArgumentException("Time string cannot be null or whitespace.", nameof(timeValueString)); + + return Time.TryParse(timeValueString, out var parsedTime) + ? parsedTime + : throw new FormatException($"Invalid time format: {timeValueString}"); } } -} \ No newline at end of file +}