Reputation:
Consider a store that open at 9:15AM and closes at 3:30PM. Based on the urrent time i would like to print if the Store is Open or Closed. I am having difficulty with the logic. In my code if the time is 9:45AM the output is Closed.
import time
timenow = time.localtime()
print(timenow)
print(timenow.tm_hour)
print(timenow.tm_min)
if ((timenow.tm_hour >= 9) & (timenow.tm_min >= 15)):
if((timenow.tm_hour <= 15) & (timenow.tm_min <= 30)):
print('Store open.')
else:
print('Store closed.')
If there is a better way to do this, i would really appreciate your help.
Upvotes: 1
Views: 6872
Reputation: 7343
First you work with the time module. Which in this case is not the best solution below I will explain why. The time module in most cases calls the C library of the platform host. That is, the behavior and results of the same function will be different on different operating systems.
Secondly, the code does not use the time zone, which of course will lead to gross application errors.
Example:
In [38]: time.localtime()
Out[38]: time.struct_time(tm_year=2018, tm_mon=2, tm_mday=16, tm_hour=5, tm_min=56, tm_sec=49, tm_wday=4, tm_yday=47, tm_isdst=0)
Displays in GMT but my actual time is
root@xxx# date
Fri Feb 16 13:57:55 +08 2018
Here we meet with timezones.
A time zone is a region of the globe that observes a uniform standard time for legal, commercial, and social purposes. Time zones tend to follow the boundaries of countries and their subdivisions because it is convenient for areas in close commercial or other communication to keep the same time.
In Python the best approach to work with time and dates it's datetime module for operations and pytz for timezones.
Pytz doesn't standard Python's lib, so don't forget to install it
pip install pytz
There are two kinds of date and time objects: “naive” and “aware”.
An aware object has sufficient knowledge of applicable algorithmic and political time adjustments, such as time zone and daylight saving time information, to locate itself relative to other aware objects. An aware object is used to represent a specific moment in time that is not open to interpretation.
A naive object does not contain enough information to unambiguously locate itself relative to other date/time objects. Whether a naive object represents Coordinated Universal Time (UTC), local time, or time in some other timezone is purely up to the program, just like it is up to the program whether a particular number represents metres, miles, or mass. Naive objects are easy to understand and to work with, at the cost of ignoring some aspects of reality
And in real world application we always work with "aware" datetime objects.
Example:
import datetime
import pytz
tz = pytz.timezone('Singapore')
datetime.datetime.now(tz)
datetime.datetime(2018, 2, 16, 14, 15, 6, 822038, tzinfo=<DstTzInfo 'Singapore' +08+8:00:00 STD>)
Now we have correct time with a proper timezone. We don't need to take care about operations under datetime objects.
For example, you want to show the flight time of Singapore-Bangkok. Time difference between SNG and BKK timezones is 1 hour. So when 14:00 in Singapore it's 13:00 in Bangkok. All airlines adhere to the same rule. Time of departure and arrival are indicated in the local time with timezone GMT offset. Let's assume what the real flight time between SNG and BKK is 1 hour we will have SNG departure 14:00 +8 - BKK arrival 14:00 +7. So, in a case if we ignore timezones difference between departure and arrival
will be 0 hours hence and our displaying flight time will be 0 hours. That is a mistake of course. Also don't forget about DST rules, leap years and etc. But if you use datetime "aware" objects you don't have to worry about anything.
Summing up all the above, let's rewrite our code
import datetime
import pytz
def shop_is_open(tz='Singapore'):
tz = pytz.timezone(tz)
time_now = datetime.datetime.now(tz).time()
#here we could apply any timezone according shop geo location
time_open = datetime.time(9, 45, tzinfo=tz)
time_close = datetime.time(15, 30, tzinfo=tz)
is_open = False
if time_now >= time_open and time_now <= time_close:
is_open = True
return is_open
Upvotes: 4
Reputation: 1922
Using datetime, which i find easier:
import datetime
# define the opening hours
startTime = datetime.time(9, 15, 0)
endTime = datetime.time(15, 30, 0)
# function that compares the given time against opening and closing
def isOpen(startTime, endTime, x):
if startTime <= endTime:
return startTime <= x <= endTime
else:
return startTime <= x or x <= endTime
# Call isOpen function ( datetime.time is the "current time" )
print isOpen(startTime, endTime, datetime.time(14, 30, 0))
print isOpen(startTime, endTime, datetime.time(20, 30, 0))
If you want to run it against the current time just call the function like so:
currentTime = datetime.datetime.now().time()
print isOpen(startTime, endTime, currentTime)
Upvotes: 0
Reputation: 161
I came up with a very simple and non fancy code. I could just comment it but I still don't have enough reputation for that. Just convert your hour to minutes like this
import time
def time_to_min(hour, minute):
return hour*60+minute
timenow = time.localtime()
open_time = time_to_min(9, 15)
close_time = time_to_min(15, 30)
current_time = time_to_min(timenow.tm_hour, timenow.tm_min)
if current_time >= open_time and current_time <= close_time:
print 'Store opened'
else:
print 'Store closed'
Upvotes: 2