bash – UNIX日期:如何将周数转换为日期范围(Mon-Sun)?

我有从大日志文件提取的周数列表,它们是使用语法提取的:
$date --date="Wed Mar 20 10:19:56 2012" +%W;
12

我想创建一个简单的bash函数,可以将这些周数转换为日期范围.我想函数应该接受2个参数:$number和$year,例如:

$week() { ......... }
$number=12; year=2012
$week $number $year
"Mon Mar 19 2012" - "Sun Mar 25 2012"
使用GNU日期:
$cat weekof.sh
function weekof()
{
    local week=$1 year=$2
    local week_num_of_Jan_1 week_day_of_Jan_1
    local first_Mon
    local date_fmt="+%a %b %d %Y"
    local mon sun

    week_num_of_Jan_1=$(date -d $year-01-01 +%W)
    week_day_of_Jan_1=$(date -d $year-01-01 +%u)

    if ((week_num_of_Jan_1)); then
        first_Mon=$year-01-01
    else
        first_Mon=$year-01-$((01 + (7 - week_day_of_Jan_1 + 1) ))
    fi

    mon=$(date -d "$first_Mon +$((week - 1)) week" "$date_fmt")
    sun=$(date -d "$first_Mon +$((week - 1)) week + 6 day" "$date_fmt")
    echo "\"$mon\" - \"$sun\""
}

weekof $1 $2
$bash weekof.sh 12 2012
"Mon Mar 19 2012" - "Sun Mar 25 2012"
$bash weekof.sh 1 2018
"Mon Jan 01 2018" - "Sun Jan 07 2018"
$

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...