Iglo
Iglo

Reputation: 706

Best way to work out if a store is open within a 24 hour period

Let's say I know the hour a store opens, the hour a store closes and the current hour.

opens = 9
closes = 21
currentHour = 4

I need a function something like..

isStoreOpen(opens, closes, currentHour) == false

If I do something like this..

currentHour >= opens and currentHour < closes

and the store opens at hour 1:00 and closes at 13:00 this will not work. It only works if the closes number is greater than opening.

I can imagine this has a simple solution that's been resolved before but I cannot find anything online because I'm not sure how to describe my problem properly.

Upvotes: 0

Views: 77

Answers (1)

Putnam
Putnam

Reputation: 714

For your simple case, this will work:

function isStoreOpen(opens,closes,currentHour)
    if closes < opens then
        closes = closes + 24
        currentHour = currentHour + 24
    end
    return currentHour >= opens and currentHour < closes
end

In general, though, prefer to use libraries for time stuff.

Upvotes: 2

Related Questions