Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the current time is in range in python?

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' 

But troubles begin when user enters a time range when the end time is smaller than the start time, e.g. "23:00 - 06:00". A time like '00:00' will be in this range. About 5 years ago I wrote this PHP function:

function checkInterval($start, $end)   {         $dt = date("H:i:s");          $tstart = explode(":", $start);     $tend =   explode(":", $end);     $tnow =   explode(":", $dt);      if (!$tstart[2])       $tstart[2] = 0;      if (!$tend[2])       $tend[2] = 0;        $tstart = $tstart[0]*60*60 + $tstart[1]*60 + $tstart[2];     $tend   = $tend[0]*60*60   + $tend[1]*60   + $tend[2];     $tnow   = $tnow[0]*60*60   + $tnow[1]*60   + $tnow[2];      if ($tend < $tstart)       {         if ($tend - $tnow > 0 && $tnow > $tstart)           return true;         else if ($tnow - $tstart > 0 && $tnow > $tend)           return true;         else if ($tend > $tnow && $tend < $tstart && $tstart > $tnow)           return true;         else return false;       } else       {         if ($tstart < $tnow && $tend > $tnow)           return true;         else           return false;       } 

Now I need to do the same thing, but I want to make it good looking. So, what algorithm should I use to determine if the current time '00:00' is in reversed range e.g. ['23:00', '01:00']?

like image 772
night-crawler Avatar asked May 25 '12 03:05

night-crawler


People also ask

How do you check if an element is in a range Python?

You can check if a number is present or not present in a Python range() object. To check if given number is in a range, use Python if statement with in keyword as shown below. number in range() expression returns a boolean value: True if number is present in the range(), False if number is not present in the range.

How do you set a time range in Python?

DateTimeRange is a Python library to handle a time range. e.g. check whether a time is within the time range, get the intersection of time ranges, truncating a time range, iterate through a time range, and so forth.

How do I get the current time format in Python?

Use strftime() to display Time and Date The strftime() method returns a string displaying date and time using date, time or datetime object.


2 Answers

The Python solution is going to be much, much shorter.

def time_in_range(start, end, x):     """Return true if x is in the range [start, end]"""     if start <= end:         return start <= x <= end     else:         return start <= x or x <= end 

Use the datetime.time class for start, end, and x.

>>> import datetime >>> start = datetime.time(23, 0, 0) >>> end = datetime.time(1, 0, 0) >>> time_in_range(start, end, datetime.time(23, 30, 0)) True >>> time_in_range(start, end, datetime.time(12, 30, 0)) False 
like image 119
Dietrich Epp Avatar answered Sep 22 '22 08:09

Dietrich Epp


Date/time is trickier than you think

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, and calendar from 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

Use timezone-aware datetime for calculations

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 
like image 37
Paulo Scardine Avatar answered Sep 21 '22 08:09

Paulo Scardine