Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何根据时隙持续时间和中断将datetime范围划分为时隙_Python_Datetime_Python Datetime - Fatal编程技术网

Python 如何根据时隙持续时间和中断将datetime范围划分为时隙

Python 如何根据时隙持续时间和中断将datetime范围划分为时隙,python,datetime,python-datetime,Python,Datetime,Python Datetime,我有一个有趣的问题要解决。我需要把一个给定的时间范围划分成几段,比如说1个小时,也可以有一个固定的休息时间 因此,假设一家公司在一天中的工作时间为上午9点到下午5点,他们希望将工作时间划分为1小时。另外,公司的每个工作时间通常都有休息时间,比如说,休息时间是从下午12:00到下午1:00。这是不断的突破。另一个限制是,每工作1小时,休息15分钟。因此,考虑到所有这些信息,我需要决定在给定的一天中可以制作多少个插槽 让我们用一个例子来理解这一点: Working hours: 9 AM to 5

我有一个有趣的问题要解决。我需要把一个给定的时间范围划分成几段,比如说1个小时,也可以有一个固定的休息时间

因此,假设一家公司在一天中的工作时间为上午9点到下午5点,他们希望将工作时间划分为1小时。另外,公司的每个工作时间通常都有休息时间,比如说,休息时间是从下午12:00到下午1:00。这是不断的突破。另一个限制是,每工作1小时,休息15分钟。因此,考虑到所有这些信息,我需要决定在给定的一天中可以制作多少个插槽

让我们用一个例子来理解这一点:

Working hours: 9 AM to 5 PM  
Constant break: 12:00 PM to 1:00 PM  
Max duration of one slot: 1-hour  
Break between each slot: 15 mins  
产生的插槽应为:

Slot#1: 9 AM to 10 AM
Slot#2: 10:15 AM to 11:15 AM  
Slot#3: 1:00 PM to 2:00 PM  
Slot#4: 2:15 PM to 3:15 PM  
Slot#5: 3:30 PM to 4:30 PM
<> P>只是让问题变得更容易,随着时间的推移,我们可以考虑今天的日期。 我试图使用python的本机datetime和calendar库解决上述问题,但未能做到这一点。有人能帮我解决这个问题吗?
谢谢

以下是我的解决方案:

首先,我创建了一个名为
Day
的新对象,因此我可以轻松地计算小时和分钟:

from functools import total_ordering

@total_ordering
class Day():
    def __init__(self, h=0, m=0):
        self.h = (h + m // 60) % 24
        self.m = m % 60

    def __add__(self, other):
        return Day(self.h + other.h, self.m + other.m)

    def __iadd__(self, other):
        return self.__add__(other)

    def __sub__(self, other):
        return Day(self.h - other.h, self.m - other.m)

    def __isub__(self, other):
        return self.__sub__(other)

    def __repr__(self):
        return f'h: {self.h}, m:{self.m}'

    def __str__(self):
        return self.__repr__()

    def __eq__(self, other):
        return self.h == other.h and self.m == other.m

    def __gt__(self, other):
        return self.h > other.h or (self.h == other.h and self.m > other.m)

    def __copy__(self):
        return Day(self.h, self.m)

    def copy(self):
        return self.__copy__()
然后我以
Day
对象的形式保存了示例中的数据

start_day = Day(9) # starting working day
end_day = Day(17) # end working day

start_break = Day(12)
end_break = Day(13)

slot_duration = Day(1)
interbreak_duration = Day(0, 15) # break between slots
现在有了这个设置,编写算法来进行计算应该更容易了

这是我的解决方案草案,但效果不太好,我建议您编写一个新的算法,以适应您所需的输出

def calculate(start_day, end_day, start_break, end_break, slot_duration, interbreak_duration):
    now = start_day.copy()
    break_done = False # mid day break
    interbreak_done = True # break between slots

    while True:
        print(now, end=' ')
        if interbreak_done: 
            print('\tslot', end='')
        else:
            print('\tbreak', end='')
        if break_done:
            if interbreak_done:
                now += slot_duration
                if now == end_day:
                    print(now)
                    break
                if now >= end_day:
                    print('\r', end='')
                    break
                interbreak_done = False
            else:
                now += interbreak_duration
                if now == end_day:
                    print(now)
                    break
                if now > end_day:
                    print('\r', end='')
                    break
                interbreak_done = True
        else:
            if interbreak_done:
                now += slot_duration
                interbreak_done = False
                if now == start_break:
                    print('slot')
                if now >= start_break:
                    print('\r', end='')
                    now = end_break
                    interbreak_done = True
                    break_done = True
                    continue
            else:
                now += interbreak_duration
                if now >= start_break:
                    now = end_break
                    break_done = True
                interbreak_done = True


        print('\t', now)
输出为:

calculate(start_day, end_day, start_break, end_break, slot_duration, interbreak_duration)

h: 9, m:0       slot     h: 10, m:0
h: 10, m:0      break    h: 10, m:15
h: 10, m:15     slot     h: 11, m:15
h: 11, m:15     break    h: 11, m:30
h: 13, m:0      slot     h: 14, m:0
h: 14, m:0      break    h: 14, m:15
h: 14, m:15     slot     h: 15, m:15
h: 15, m:15     break    h: 15, m:30
h: 15, m:30     slot     h: 16, m:30
h: 16, m:30     break    h: 16, m:45
h: 16, m:45     slot

虽然有点小问题,但应该可以帮助您开始

为什么11:15到12:00之间没有空位?没有一个最小持续时间slot@HoxhaAlban因为每个时段的持续时间为1小时,我们不能有任何时段的持续时间小于1小时。所以,下午12:00开始休息,从上午11:15开始我们不能有45分钟的时间。欢迎来到So。这不是一个讨论论坛或教程。请花时间阅读和阅读该页面上的其他链接。