how do you validate time in python?

You would do yourself (and anyone reading your code, your future self included) a favour by writing functions that have one, clearly defined purpose.

For example:

def get_status(self, user_time, start, end, breaks):
    if not (start <= user_time <= end):
        return 'time out'
    else:
        for start_break, end_break in breaks:
            if start_break <= user_time <= end_break:
                return 'break out'
        return 'time in'

If I understand your logic correctly, this gives the correct status, given some times, for example:

my_object.get_status(time(12, 10), 
                     time(5, 0), time(17, 0), 
                     [(time(10, 0), time(10, 15)), 
                      (time(12, 0), time(13, 00)), 
                      (time(15, 0), time(15, 15))])

(line breaks just for clarity)

This would return 'break out'. You can then use this method from another method that does something with the value that got returned through some web form – separating the code that deals with the form from the code that tests some business rule (i.e. what is the status that goes with some time).

Since it appears you don’t really need the start and end times of the work day to be variable, nor the breaks, you probably want to move that to the constructor instead:

from datetime import time


class MyClass:
    def __init__(self, start, end, breaks):
        self.start = start
        self.end = end
        self.breaks = breaks

    def get_status(self, user_time):
        if not (self.start <= user_time <= self.end):
            return 'time out'
        else:
            for start_break, end_break in self.breaks:
                if start_break <= user_time <= end_break:
                    return 'break out'
        return 'time in'


obj = MyClass(time(5, 0), time(17, 0),
              [(time(10, 0), time(10, 15)),
               (time(12, 0), time(13, 00)),
               (time(15, 0), time(15, 15))])

print(obj.get_status(time(12, 10)))

Result:

break out

In your situation probably something like:

from datetime import datetime, time


class MyClass:
    def __init__(self, start, end, breaks):
        self.start = start
        self.end = end
        self.breaks = breaks

        # you probable have some code that sets this to a valid request
        self.request = None

    def get_status(self, user_time):
        if not (self.start <= user_time <= self.end):
            return 'time out'
        else:
            for start_break, end_break in self.breaks:
                if start_break <= user_time <= end_break:
                    return 'break out'
        return 'time in'

    def set_time(self, fk):
        user_time = datetime.strptime(self.request.POST['time'], '%I:%M:%S %p').time()
        status = self.get_status(user_time)
        # assuming this exists and works
        TimeLogsModel.objects.create(date=fk,
                                     time=self.request.POST['time'],
                                     status=status)
        # just returning the status because you did, not sure you even want that
        return status

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top