我会打电话给Python解释器。采纳
Using Python to count the number of business days in a month?
--
countBusinessDaysPy=$(cat <<'EOF'
import datetime, sys
businessdays = 0
startDate = datetime.date.fromisoformat(sys.argv[1])
endDate = datetime.date.fromisoformat(sys.argv[2])
if endDate < startDate:
(startDate, endDate) = (endDate, startDate)
while startDate <= endDate: # change from <= to < to not count both start and end days
if startDate.weekday() < 5:
businessdays += 1
startDate += datetime.timedelta(days=1)
print(businessdays)
EOF
)
countBusinessDays() { python3 -c "$countBusinessDaysPy" "$@"; }
…提供了一个shell函数,该函数调用Python解释器进行所需的数学运算(注意,这是一个
范围)。此后:
$ countBusinessDays 2019-01-01 2020-01-01
262
$ countBusinessDays 2019-01-01 2019-01-07
5
{
read -r header; printf '%s\n' "$header,TotalDates"
while IFS=, read -r resolved startOfWork rest; do
printf '%s\n' "${resolved},${startOfWork}${rest:+,$rest},$(countBusinessDays "$startOfWork" "$resolved")"
done
} <yourInputFile
…作为输出发射:
Resolved,StartOfWork,TotalDates
2020-01-16,2020-01-10,5
2020-01-13,2020-01-13,1
2020-01-20,2020-01-15,4
2020-01-20,2020-01-14,5
2020-01-14,2020-01-09,4
2020-01-09,2020-01-08,2
2020-01-16,2020-01-14,3
2020-01-09,2020-01-07,3
2020-01-14,2020-01-12,2