How do I get certain Time Range in Python

I have Start and End Time.Like shown below

def overlap(start1, end1, start2, end2):
    pass

So with both timing there will be a meeting scheduled, I want to make sure that the no meeting is overlapped with each other. So if the two meeting overlaps then it will return True else False.

I have done it in JavaScript and I want to do it in Python as I'm new in Python I'm not aware of what module should I use and how to use them. Here goes my implementation on JavaScript.

// time of first timespan
var x = new Date('01/01/2001 8:30:00').getTime();
var y = new Date('01/01/2001 9:30:00').getTime();

// time of second timespan
var a = new Date('01/01/2001 8:54:00').getTime();
var b = new Date('01/01/2001 9:00:00').getTime();

if (Math.min(x, y) <= Math.max(a, b) && Math.max(x, y) >= Math.min(a, b)) {
    // between
}

Upvotes: 0

Views: 1485

Answers (4)

Rafael Aguilar
Rafael Aguilar

Reputation: 3279

This is the closest interpretation of your JS code I could do in this short time:

from datetime import datetime

#assuming time format comes in a string like this: '8:30:00'
#assumming provided time has sense, i.e. end1 > start1 and end2 > start2

TIME_FORMAT = '%H:%M:%S'

def overlap(start1, end1, start2, end2):
    #transform time        
    start1_time = datetime.strptime(start1, TIME_FORMAT )
    end1_time = datetime.strptime(end1, TIME_FORMAT )
    start2_time = datetime.strptime(start2, TIME_FORMAT )
    end2_time = datetime.strptime(end2, TIME_FORMAT )

    #checking conditions
    if (min(start1_time, end1_time) <= max(start2_time, end2_time)) \
    and \
    (max(start1_time, end1_time) >= min(start2_time, end2_time)):
        return True
    else:
        return False

>>>overlap('8:30:00','9:30:00','8:54:00','9:00:00')
True

>>>overlap('8:30:00','9:30:00','9:54:00','10:00:00')
False

PS: the only module you would is datetime as Python provide min and max in its core.

EDIT1: Using only time and not date.

EDIT1: Indentation Update.

Upvotes: 0

asongtoruin
asongtoruin

Reputation: 10359

How about this:

from dateutil import parser

def date_overlap(date1, date2):
    obj_1 = map(parser.parse, date1)
    obj_2 = map(parser.parse, date2)

    # is latest start before earliest end? If so, overlap!
    if max(obj_1[0], obj_2[0]) < min(obj_1[1], obj_2[1]):
        return True
    else:
        return False

date_1 = ('01/01/2001 8:30:00', '01/01/2001 9:30:00')
date_2 = ('01/01/2001 8:54:00', '01/01/2001 9:00:00')

print date_overlap(date_1, date_2)

dateutil.parser converts strings to dates. If our latest start is before our earliest end, there is an overlap.

Note: if you are using Python 3.x, you'll need to wrap both of our map outputs as lists like so:

from dateutil import parser

def date_overlap(date1, date2):
    obj_1 = list(map(parser.parse, date1))
    obj_2 = list(map(parser.parse, date2))

    # is latest start before earliest end? If so, overlap!
    if max(obj_1[0], obj_2[0]) < min(obj_1[1], obj_2[1]):
        return True
    else:
        return False

date_1 = ('01/01/2001 8:30:00', '01/01/2001 9:30:00')
date_2 = ('01/01/2001 8:54:00', '01/01/2001 9:00:00')

print date_overlap(date_1, date_2)

Upvotes: 1

FcoRodr
FcoRodr

Reputation: 1633

def overlap(start1, end1, start2, end2):
    return (start1 <= start2 <= end1) or (start2 <= start1 <= end2)

Upvotes: 0

mrCarnivore
mrCarnivore

Reputation: 5088

import datetime

def overlap(start1, end1, start2, end2):
    if start1 < start2 and end1 > start2 or \
       start1 > start2 and end1 < end2 or \
       start1 > start2 and start1 < end2: 
        return True
    else:
        return False

start_date1 = datetime.datetime(2017,1,1)
end_date1 = datetime.datetime(2017,1,3)
start_date2 = datetime.datetime(2017,1,2)
end_date2 = datetime.datetime(2017,1,4)

print(overlap(start_date1, end_date1, start_date2, end_date2))

You can also use times as well.

Upvotes: 0

Related Questions