I need to check if the current time is in timerange. The most simple case time_end > time_start:
if time(6,0) <= now.time() <= time(12,00): print \'1\'
Calculations involving date/time can be very tricky because you must consider timezone, leap years, day-light-savings and lots of corner cases. There is an enlightening video from the talk by Taavi Burns at PyCon2012 entitled "What you need to know about datetimes":
What you need to know about datetimes:
time,datetime, andcalendarfrom the standard library are a bit messy. Find out: what to use where and how (particularly when you have users in many timezones), and what extra modules you might want to look into.Event: PyCon US 2012 / Speakers: Taavi Burns / Recorded: March 10, 2012
The concept of a datetime.time for tomorrow is void, because datetime.time lacks any date information. You probably want to convert everything to timezone-aware datetime.datetime before comparing:
def time_in_range(start, end, x):
today = timezone.localtime().date()
start = timezone.make_aware(datetime.datetime.combine(today, start))
end = timezone.make_aware(datetime.datetime.combine(today, end))
x = timezone.make_aware(datetime.datetime.combine(today, x))
if end <= start:
end += datetime.timedelta(days=1) # tomorrow!
if x <= start
x += datetime.timedelta(days=1) # tomorrow!
return start <= x <= end