当前位置: 代码迷 >> python >> 将6天的时间表映射到工作周的算法?
  详细解决方案

将6天的时间表映射到工作周的算法?

热度:63   发布时间:2023-06-13 17:16:38.0

将6天时间表映射到工作周(不包括周末)的最有效方法是什么? 是否有适合该目的的现有算法? 用例是采用当前日期执行功能并输出相应的Day。

例如:

今天2/15/2017将是第1 day(2/15/2017) = "Day 1"
明天,2/16将是第2 day(2/16/2017) = "Day 2"
星期五将是第3 day(2/17/2017) = "Day 3"
星期一将是第4 day(2/20/2017) = "Day 4"
等等

理想情况下,这可以在Python中执行,但任何实现都可以

您可以轻松地创建一个从开始返回日期的生成器。 然后,您可以使用过滤掉周末, 结果包装在以获得每天的索引:

from datetime import date, timedelta
from itertools import count
import calendar

WORK_WEEK = 6

today = date.today()

# Generator producing successive dates starting from today
dates = (today + timedelta(days=i) for i in count())

# Generator filtering weekends
work_dates = (d for d in dates if d.weekday() <= calendar.FRIDAY)

for i, d in enumerate(work_dates):
    # Print only 10 first days
    if i == 10: 
        break
    print('{} -> Day {}'.format(d, i % WORK_WEEK + 1))

输出:

2017-02-16 -> Day 1
2017-02-17 -> Day 2
2017-02-20 -> Day 3
2017-02-21 -> Day 4
2017-02-22 -> Day 5
2017-02-23 -> Day 6
2017-02-24 -> Day 1
2017-02-27 -> Day 2
2017-02-28 -> Day 3
2017-03-01 -> Day 4

给定日期为“第1天”,它将返回任何给定日期的计划日期:

import datetime

def day_number(day1, target_date):
  days = (target_date - day1).days
  weeks = days // 7
  workdays = weeks * 5
  whole_week = day1 + datetime.timedelta(days=weeks*7)
  for d in range(days % 7):
    day = whole_week + datetime.timedelta(days=d)
    if day.weekday() < 5:
      workdays += 1
  return workdays % 6 + 1

如果我对您的理解正确,则可以实例化日期对象,然后使用timedelta添加相应的日期,使用列表推导过滤掉周末。 另外,您可以先检查今天是否是周末的一部分:

import datetime

today = datetime.date.today()

if today.weekday() < 5:  # this if-else ensures today isn't a weekend
    first = today
else:
    delta = 7 - today.weekday() 
    first = today + datetime.timedelta(days=delta)

work_days = [first]
num = 1
while len(work_days) < 6:  # get the next 5 week days, since we have the first already
    curr = first + datetime.timedelta(days=num)
    if curr.weekday() < 5:
        work_days.append(curr)
    num += 1

从您对问题的看法来看,您不仅想要星期几,而且还想要自开始日期算起的工作日数。 这将是我的解决方案。 这不包括多余的例外,例如假期。

from datetime import date


def day(day, month, year):
    # Create starting date, in this case 10th Feb, 2017
    # Let's assume the starting date is a weekday
    startingDate = date(2017, 2, 10)

    # Create ending date based on user input
    endingDate = date(year, month, day)

    # First check if the user input is a weekend
    if endingDate.weekday() == 5 or endingDate.weekday() == 6:
        return "This is not a workday"

    # Calculate the total days in between the two dates
    totalDays = (endingDate - startingDate).days

    #Subtract two days for every weekend
    totalDays -= int(totalDays/7)*2

    return "Day " + str(totalDays)


def main():
    print(day(14, 2, 2017))


main()

您可以使用momentjs,它是一个很好的库,可以帮助您处理像这样的日历

moment().subtract(10, 'days').calendar(); // 02/05/2017
moment().subtract(6, 'days').calendar();  // Last Thursday at 9:06 PM
moment().subtract(3, 'days').calendar();  // Last Sunday at 9:06 PM
moment().subtract(1, 'days').calendar();  // Yesterday at 9:06 PM
moment().calendar();                      // Today at 9:06 PM
moment().add(1, 'days').calendar();       // Tomorrow at 9:06 PM
moment().add(3, 'days').calendar();       // Saturday at 9:06 PM
moment().add(10, 'days').calendar();      // 02/25/2017

而且安装非常容易Bower安装时刻--save#bower npm安装时刻--save#npm Install-Package Moment.js#NuGet spm安装时刻--save#spm流星添加一下js:moment#流星

  相关解决方案