在Linux中获取给定日期的前一个工作日的功能

时间:2018-11-30 02:53:56

标签: linux bash function date

给定输入日期,我想编写一个bash函数,该函数将输出前一个工作日。 我的意思是指之前的 天(星期一至星期五); 我不需要考虑假期。 因此,例如,给定“ 2018年1月2日”,结果应为“ 2018年1月1日” (即使那是个假期), 但给定的是“ 2018年1月1日”,则结果应为“ 2017年12月29日” (因为12月30日和31日分别是星期六和星期日)。 我不需要任何特殊格式; date -d可以理解的东西。

我尝试了以下操作,但似乎未正确考虑输入日期:

function get_previous_busday()
{
    DAY_OF_WEEK=`$1 +%w`
    if [ $DAY_OF_WEEK -eq 0 ] ; then
        LOOKBACK=-2
    elif [ $DAY_OF_WEEK -eq 1 ] ; then
        LOOKBACK=-3
    else
        LOOKBACK=-1
    fi
    PREVDATE=date -d "$1 $LOOKBACK day"
}

我想今天申请它:

PREVDATE=$(get_previous_busday $(date)) 
echo $PREVDATE

还有昨天:

PREVDATE=$(get_previous_busday (date -d "$(date) -1 day")) 
echo $PREVDATE

但是它不起作用:

main.sh: line 3: Fri: command not found 
main.sh: line 4: [: -eq: unary operator expected 
main.sh: line 6: [: -eq: unary operator expected 
main.sh: line 11: -d: command not found 
main.sh: command substitution: line 20: syntax error near unexpected token `date'
main.sh: command substitution: line 20: `get_previous_busday (date -d "$(date) -1 day"))'

2 个答案:

答案 0 :(得分:3)

执行所需操作的功能是:

get_previous_busday() {
        if [ "$1" = "" ]
        then
                printf 'Usage: get_previous_busday (base_date)\n' >&2
                return 1
        fi
        base_date="$1"
        if ! day_of_week="$(date -d "$base_date" +%u)"
        then
                printf 'Apparently "%s" was not a valid date.\n' "$base_date" >&2
                return 2
        fi
        case "$day_of_week" in
          (0|7)         # Sunday should be 7, but apparently some people
                        # expect it to be 0.
                offset=-2       # Subtract 2 from Sunday to get Friday.
                ;;
          (1)   offset=-3       # Subtract 3 from Monday to get Friday.
                ;;
          (*)   offset=-1       # For all other days, just go back one day.
        esac
        if ! prev_date="$(date -d "$base_date $offset day")"
        then
                printf 'Error calculating $(date -d "%s").\n' "$base_date $offset day"
                return 3
        fi
        printf '%s\n' "$prev_date"
}

例如,

$ get_previous_busday
Usage: get_previous_date (base_date)
$ get_previous_busday foo
date: invalid date ‘foo’
Apparently "foo" was not a valid date.
$ get_previous_busday today
Fri, Nov 30, 2018  1:52:15 AM
$ get_previous_busday "$(date)"
Fri, Nov 30, 2018  1:52:51 AM
$ PREVDATE=$(get_previous_busday $(date))
$ echo "$PREVDATE"
Fri, Nov 30, 2018 12:00:00 AM
$ get_previous_busday "$PREVDATE"
Thu, Nov 29, 2018 12:00:00 AM
$ PREVPREVDATE=$(get_previous_busday "$PREVDATE")
$ printf '%s\n' "$PREVPREVDATE"
Thu, Nov 29, 2018 12:00:00 AM
$ get_previous_busday "$PREVPREVDATE"
Wed, Nov 28, 2018 12:00:00 AM

答案 1 :(得分:1)

有几种方法可以获取回到工作日所需的补偿。

  1. 您可以编写一个案例声明:

    case $dow in
        0|7)    backday=2;;   # For Sunday (either named 0 or 7) go back 2 days
        1)      backday=3;;   # For monday go back three (3) days.
        *)      backday=1;;   # For the rest, just one day.
    esac
    
  2. 您可以使用数学:

    backday=$(( ((dow%7)>1) ? 1 : (dow%7)+2 ))
    
  3. 或查找数组:

    a=(0 1 2 3 4 5 6 7)
    b=(2 3 1 1 1 1 1 2)
    backday=${b[dow]}
    

对于其他选择,您可以使用此功能(无错误检测),
和一些测试:

#!/bin/bash
get_previous_busday() {  dow=$(date -d "$*" '+%w')
                         backday=$(( ((dow%7)>1) ? 1 : (dow%7)+2 ))
                         prev_date="$(date -d "$* -$backday day")"
                         printf '%s\n' "$prev_date"
                      }

get_previous_busday "$(date)"
get_previous_busday $(date -d "-1 day")
get_previous_busday $(date -d "-2 day")
get_previous_busday $(date -d "-3 day")
get_previous_busday $(date -d "-4 day")

将打印:

Fri Nov 30 10:03:45 UTC 2018
Fri Nov 30 10:03:45 UTC 2018
Fri Nov 30 10:03:45 UTC 2018
Thu Nov 29 10:03:45 UTC 2018
Wed Nov 28 10:03:45 UTC 2018