Python Forum

Full Version: Newbie question to find the seconds between now and midnight
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello:

I need a function to tell me at now, if a specific market is open, if open return True, if not, return False.
There are 3 different markets, market A runs in 2 time slots: first time slot, from 9:00AM to 11:00AM; the second time slot, from 13:00PM to 15:00PM.
market B runs in 2 time slots: first time slot, from 9:00AM to 11:00AM; the second time slot, from 13:00PM to 15:15PM.
market C runs in 3 time slots: first time slot, from 9:00AM to 10:30AM; the second time slot, from 11:00AM to 13:00PM; the third time slot from 14:00PM to 15:00PM.
I need to check this almost every second during the market hours, and after the last market closes, the program can exit.
Note: I am thinking about compare the now and midnight of today to get the difference in seconds, and compare the number of seconds, if the seconds are in specific range, then the market is open, otherwise, it is closed.
However, I can't figure out how to find the seconds between now and the midnight of today.
Any advices are appreciated.
Thanks,
>>> import datetime as dt
>>> now = dt.datetime.now()
>>> today = dt.datetime(now.year, now.month, now.day)
>>> difference = now - today
>>> difference
datetime.timedelta(0, 63325, 381547)
>>> difference.seconds
63325
Here is a quick OOP example of your market opening hours checking:

from datetime import datetime, time
import time as time_time


class MarketOpeningHours:
    """Representation of market opening hours (e.g. 13:00 - 14:00)"""

    def __init__(self, opening_time: time, closing_time: time):
        if closing_time <= opening_time:
            raise Exception("Closing time must be later than opening time!")
        self.opening_time = opening_time
        self.closing_time = closing_time

    def is_open(self) -> bool:
        return self.opening_time <= datetime.now().time() <= self.closing_time


class Market:
    """Representation of market"""

    def __init__(self):
        self.opening_hours_list = []

    def add_opening_hours(self, opening_hours: MarketOpeningHours):
        self.opening_hours_list.append(opening_hours)

    def is_open(self) -> bool:
        for opening_hours in self.opening_hours_list:
            if opening_hours.is_open():
                return True
        return False


class MarketsList:
    """Representation of markets list"""

    def __init__(self):
        self.markets = []

    def add_market(self, market: Market):
        self.markets.append(market)

    def is_any_market_open(self) -> bool:
        for market in self.markets:
            if market.is_open():
                return True
        return False


market_a = Market()
market_a.add_opening_hours(MarketOpeningHours(time(9, 0), time(11, 0)))
market_a.add_opening_hours(MarketOpeningHours(time(13, 0), time(15, 0)))

market_b = Market()
market_b.add_opening_hours(MarketOpeningHours(time(9, 0), time(11, 0)))
market_b.add_opening_hours(MarketOpeningHours(time(13, 0), time(15, 15)))

market_c = Market()
market_c.add_opening_hours(MarketOpeningHours(time(9, 0), time(10, 30)))
market_c.add_opening_hours(MarketOpeningHours(time(11, 0), time(13, 0)))
market_c.add_opening_hours(MarketOpeningHours(time(14, 0), time(15, 0)))

markets_list = MarketsList()
markets_list.add_market(market_a)
markets_list.add_market(market_b)
markets_list.add_market(market_c)

while markets_list.is_any_market_open():
    print(str(datetime.now().time()) + ": some market is still open - waiting")
    time_time.sleep(1)