Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 views
in Technique[技术] by (71.8m points)

linux - BASH: How to find no. of days (considering only "Network / Business Days") between two dates (i.e. exclude weekends Saturday/Sunday)

RHEL 7.5

BASH GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)

In MS Excel, I'm able to use Network days to find no. of days between two dates. Wondering if it's possible using bash as first preference (--or any other pre-installed language on Linux supporting to solve this possibly using a one-liner - my second preference). I'm not sure if there exists any library or custom tool/utility in Linux which calculates this value.

  • To calculate the number of workdays between two dates, you can use the NETWORKDAYS function. NETWORKDAYS automatically excludes weekends, and it can optionally exclude a custom list of holidays as well. Note that NETWORKDAYS includes both the start and end dates in the calculation if they are workdays.

I have a file.txt containing 2 column fields in YYYY-mm-dd format for Resolved and Start dates (you can ignore header line for now):

Resolved,StartOfWork
2020-01-16,2020-01-10
2020-01-13,2020-01-13
2020-01-20,2020-01-15
2020-01-20,2020-01-14
2020-01-14,2020-01-09
2020-01-09,2020-01-08
2020-01-16,2020-01-14
2020-01-09,2020-01-07
2020-01-14,2020-01-12

For each row, I want to calculate no. of NETWORK i.e. WEEK DAYS only between these 2 dates (doesn't matter if Resolved/StartOfWork dates were on weekend days: Saturday/Sunday).

  • The calculation of no. of days SHOULD NOT include 'weekend-days i.e. Saturday/Sunday in it.

PS: For the purpose of this post, my question is very different than what this post is asking for: How to find the difference in days between two dates?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'd call through to a Python interpreter for this. Adopting the accepted answer from 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" "$@"; }

...gives you a shell function that calls a Python interpreter to do the math you need (note that this is an inclusive range). Thereafter:

$ countBusinessDays 2019-01-01 2020-01-01
262
$ countBusinessDays 2019-01-01 2019-01-07
5

Calling this looping over your file (note that in the real world, I'd do the looping in Python, not in bash) might look like:

{
  read -r header; printf '%s
' "$header,TotalDates"
  while IFS=, read -r resolved startOfWork rest; do
    printf '%s
' "${resolved},${startOfWork}${rest:+,$rest},$(countBusinessDays "$startOfWork" "$resolved")"
  done
} <yourInputFile

...which emits as output:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...