Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimeExtensions.cs #213

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 65 additions & 32 deletions src/DateTimeExtensions/TimeExtensions.cs
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]>
Expand All @@ -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}");
}
}
}
}