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

Fix IsBetween method in TimeExtensions.cs #204

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

drippypop
Copy link

This pull request addresses an issue with the IsBetween method in the TimeExtensions class. The current implementation does not correctly handle the case where startTime and endTime are equal. This fix adds a specific condition check to ensure accurate results.

Changes:

  • Updated the IsBetween method to handle the case where startTime and endTime are equal.

Before:

public static bool IsBetween(this DateTime dateTime, Time startTime, Time endTime)
{
    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 and start time
        if (currentTime.CompareTo(startTime) >= 0 || currentTime.CompareTo(endTime) <= 0) {
            return true;
        }
        return false;
    }
}

After:

public static bool IsBetween(this DateTime dateTime, Time startTime, Time endTime)
{
    var currentTime = dateTime.TimeOfTheDay();
    // If startTime and endTime are equal
    if (startTime.Equals(endTime)) {
        if (currentTime.CompareTo(startTime) >= 0 && currentTime.CompareTo(endTime) <= 0) {
            return true;
        }
    }
    // 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 and start time
        if (currentTime.CompareTo(startTime) >= 0 || currentTime.CompareTo(endTime) <= 0) {
            return true;
        }
        return false;
    }
}

Testing:

  • Added unit tests to cover the new condition where startTime and endTime are equal.
  • Verified that all existing tests pass.

Related Issues:

@joaomatossilva
Copy link
Owner

Added unit tests to cover the new condition where startTime and endTime are equal.

no tests on the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handling Equal Start and End Times in IsBetween Method
3 participants