Search code examples
pythondatetypesfastapipydantic

Time Intervals In Python


I am making an app to schedule tasks (to actual people), and as part of it, I want them to be able to enter unavailability times in which they cannot be assigned tasks.

So I am looking for a notion of a time interval, i.e "Every week on Sunday between 1-2PM" or "Every month between the 4th at 3AM to the 9th at 6AM"

Something like a cron expression, except the fact it's not a single point in time but a continuous range in time.

And then I could tell if the task overlaps with this time frame and if so not assign it to that person.

While I can implement something like that myself, I would much rather use an existing builtin, or a pydantic type, because I want this unavailability interval to be passed by the API (I use FastAPI) instead of creating a million models for all the possible interval types allowed, including all the validation that comes with it.

Is something like that possible?

Edit: This type is also useful for me for other places in the application. For example, the tasks themselves can reoccur every week on Sunday, and should be done between 8-9PM


Solution

  • the dateutil package might solve your problem, particularly rrule