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

Handling Equal Start and End Times in IsBetween Method #152

Open
BaptisteSE opened this issue Feb 12, 2024 · 2 comments · May be fixed by #204
Open

Handling Equal Start and End Times in IsBetween Method #152

BaptisteSE opened this issue Feb 12, 2024 · 2 comments · May be fixed by #204

Comments

@BaptisteSE
Copy link

Greetings,

Using the DateTimeExtensions library, I noticed a potentially ambiguous case in the "IsBetween" method of the "TimeExtensions.cs" file. When the "startTime" and "endTime" are the same, the method does not explicitly specify the expected behavior. This could lead to different interpretations among library users.

Proposal:
To clearly handle this case, it would be helpful to add clarification in the method documentation or adjust the logic. A suggestion would be to consider this condition to cover the whole day, which could be a logical interpretation in many usage scenarios.

A suggested code change to reflect this logic is shown below :

public static bool IsBetween(this DateTime dateTime, Time startTime, Time endTime) {
    var currentTime = dateTime.TimeOfTheDay();
    // If startTime and endTime are equal, consider it as covering the entire day
    if (startTime.Equals(endTime)) {
        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 time and start time
        if (currentTime.CompareTo(startTime) >= 0 || currentTime.CompareTo(endTime) <= 0) {
            return true;
        }
        return false;
    }
}
@geesadbandara
Copy link

It would work fine if we made the change in
if (startTime.Equals(endTime)) { return true; }

to
if (startTime.Equals(endTime)) { if (currentTime.CompareTo(startTime) >= 0 && currentTime.CompareTo(endTime) <= 0) { return true; } }

@AhmedFaizanDev
Copy link

Hey is this issue resolved? Is not could I be assigned to this ?

@drippypop drippypop linked a pull request Oct 18, 2024 that will close this issue
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 a pull request may close this issue.

3 participants