-
-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
4c766f6
commit ec45fc2
Showing
1 changed file
with
65 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#region License | ||
#region License | ||
|
||
// | ||
// Copyright (c) 2011-2012, João Matos Silva <[email protected]> | ||
|
@@ -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 | ||
{ | ||
/// <summary> | ||
/// Adds a specified Time to a DateTime. | ||
/// </summary> | ||
/// <param name="dateTime">The base DateTime.</param> | ||
/// <param name="timeToAdd">The Time to add.</param> | ||
/// <returns>A DateTime with the added time.</returns> | ||
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); | ||
} | ||
|
||
/// <summary> | ||
/// Sets the time of a DateTime to a specified Time, keeping the original date. | ||
/// </summary> | ||
/// <param name="dateTime">The base DateTime.</param> | ||
/// <param name="timeToAdd">The Time to set.</param> | ||
/// <returns>A DateTime with the set time.</returns> | ||
public static DateTime SetTime(this DateTime dateTime, Time timeToAdd) | ||
{ | ||
if (timeToAdd == null) throw new ArgumentNullException(nameof(timeToAdd)); | ||
return AddTime(dateTime.Date, timeToAdd); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the Time representation of a DateTime's time component. | ||
/// </summary> | ||
/// <param name="dateTime">The DateTime to extract the time from.</param> | ||
/// <returns>A Time instance representing the time component.</returns> | ||
public static Time TimeOfTheDay(this DateTime dateTime) | ||
{ | ||
return new Time(dateTime.Hour, dateTime.Minute, dateTime.Second); | ||
} | ||
|
||
/// <summary> | ||
/// Checks if a DateTime's time is between two specified times. | ||
/// </summary> | ||
/// <param name="dateTime">The DateTime to check.</param> | ||
/// <param name="startTime">The start Time of the range.</param> | ||
/// <param name="endTime">The end Time of the range.</param> | ||
/// <returns>True if the time is between startTime and endTime, otherwise false.</returns> | ||
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; | ||
} | ||
|
||
/// <summary> | ||
/// Checks if a DateTime's time is before a specified time. | ||
/// </summary> | ||
/// <param name="dateTime">The DateTime to check.</param> | ||
/// <param name="time">The Time to compare against.</param> | ||
/// <returns>True if the DateTime's time is before the specified time, otherwise false.</returns> | ||
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; | ||
} | ||
|
||
/// <summary> | ||
/// Checks if a DateTime's time is after a specified time. | ||
/// </summary> | ||
/// <param name="dateTime">The DateTime to check.</param> | ||
/// <param name="time">The Time to compare against.</param> | ||
/// <returns>True if the DateTime's time is after the specified time, otherwise false.</returns> | ||
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; | ||
} | ||
|
||
/// <summary> | ||
/// Parses a string to a Time instance. | ||
/// </summary> | ||
/// <param name="timeValueString">The string representing the time.</param> | ||
/// <returns>A Time instance parsed from the string.</returns> | ||
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}"); | ||
} | ||
} | ||
} | ||
} |