I know you can get the present time this way:

import time
now = time.asctime(time.localtime())
print now  # Mon Feb 19 10:41:32 2007

How can I make sure that 'now' does not fall on our lunchtime break, let's say 11:30AM to 12:45PM?


lunch_start = time.mktime(time.strptime("11:00-20.02.2007","%H:%M-%d.%m.%Y"))
lunch_end = time.mktime(time.strptime("12:30-20.02.2007", "%H:%M-%d.%m.%Y"))
now = time.mktime(time.localtime())

if lunch_start < now < lunch_end:
    print "Hey, it's lunch time!!

I modified mawe's code so you don't have to worry about the date of the day:

# using just time and not date ...
import datetime

lunch_start = datetime.time(11, 30)
lunch_end = datetime.time(12, 45)
now = datetime.datetime.now().time()

# testing
print lunch_start  # 11:30:00
print lunch_end    # 12:45:00
print now          # eg. 09:05:59.750000

# you can compare type 'datetime.time' directly
if lunch_start < now < lunch_end:
    print "Hey, it's lunch time!  Mahlzeit!!"
elif now < lunch_start:
    print "Lunch is pretty soon!"
    print "Darn, missed lunch time!!"

Thanks mawe and uran, for your very useful information! I figured Python could do that easily!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.