重击:如何找到没有.两个日期之间的天数(仅排除“网络/工作日")(即不包括周六/周日的周末) [英] BASH: How to find no. of days (considering only "Network / Business Days") between two dates (i.e. exclude weekends Saturday/Sunday)

查看:82
本文介绍了重击:如何找到没有.两个日期之间的天数(仅排除“网络/工作日")(即不包括周六/周日的周末)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RHEL 7.5

BASH GNU bash,版本4.2.46(2)-发行版(x86_64-redhat-linux-gnu)

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

MS Excel 中,我可以使用网络天数"查找号码.两个日期之间的天数.想知道是否有可能将bash用作第一偏好(或Linux上支持的其他任何预安装语言,可能使用单线解决-这是我的第二偏好).我不确定Linux中是否存在任何计算该值的库或自定义工具/实用程序.

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.

  • 要计算两个日期之间的工作日数,可以使用NETWORKDAYS函数.NETWORKDAYS自动排除周末,也可以选择排除自定义的假期列表.请注意,NETWORKDAYS如果是工作日,则在计算中同时包含开始日期和结束日期.

我有一个file.txt,其中包含2个以 YYYY-mm-dd 格式显示的列字段,用于已解决开始日期(您可以暂时忽略标题行):

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

对于每一行,我要计算 no.这两个日期之间的NETWORK(即仅限WEEK DAYS)(已解决/开始工作"日期是否在周末不重要:周六/周日).

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 :就此帖子而言,我的问题与该帖子要的内容不同:如何找到两个日期之间天数的差异?

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?

推荐答案

这可能是车轮的重新发明,但这是一个bash解决方案(如果有兴趣的话).
请注意,它需要 date 命令的 -d 选项.

It may be the reinvention of the wheel but here's a bash solution (if interested).
Note that it requires the -d option to the date command.

while IFS="," read -r endday startday; do
    if (( lineno++ == 0 )); then                # handle header line
        echo "Resolved,StartOfWork,TotalDates"
        continue
    fi
    startsec=$(date -d "$startday" +%s)
    startdayofweek=$(date -d "$startday" +%w)   # 0 for Sun, ... 6 for Sat
    endsec=$(date -d "$endday" +%s)
    days=$(( (endsec - startsec) / 86400 + 1 )) # calendar days
    weeks=$(( days / 7 ))                       # number of weeks
    frac=$(( days % 7 ))                        # fraction mod 7
    if (( startdayofweek == 0 )); then          # case of starting on Sunday
        if (( frac > 0 )); then
            add=1                               # additional number of holidays
        else
            add=0
        fi
    else
        magic=$(( frac + (startdayofweek + 6) % 7 ))
                                                # calculate number of holidays
                                                # in the fraction period
        if (( magic < 6 )); then
            add=0
        elif (( magic == 6 )); then
            add=1
        else
            add=2
        fi
    fi
    holidays=$(( weeks * 2 + add ))             # total number of holidays
    workdays=$(( days - holidays ))             # subtract the holidays
    echo "$endday,$startday,$workdays"
done < inputfile

这篇关于重击:如何找到没有.两个日期之间的天数(仅排除“网络/工作日")(即不包括周六/周日的周末)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆