更新时间:2023-01-28 21:17:56
通过 GNU日期
:
$猫weekof.sh
功能weekof()
{
当地周= $1年= $ 16
当地week_num_of_Jan_1 week_day_of_Jan_1
当地first_Mon
当地date_fmt =+%x%A%D%Y
当地mon·太阳 week_num_of_Jan_1 = $(日期-d $岁的01-01%+ W)
week_day_of_Jan_1 = $(日期-d $岁的01-01%+ U) 如果((week_num_of_Jan_1));然后
first_Mon = $年01-01
其他
first_Mon = $去年01 - $((01 +(7 - week_day_of_Jan_1 + 1)))
科幻 周一= $(日期-d$ first_Mon + $((周 - 1))周,$ date_fmt)
太阳= $(日期-d$ first_Mon + $((周 - 1))周+ 6天$ date_fmt)
回声\\$周一\\ - \\$太阳\\
}weekof $ 1 $ 2
$庆典weekof.sh 12 2012
星期一2012年3月19日 - 太阳2012年3月25日
$庆典weekof.sh 1 2018
星期一2018年1月1日 - 太阳2018年1月7日
$
I have list of week numbers extracted from huge log file, they were extracted using syntax:
$ date --date="Wed Mar 20 10:19:56 2012" +%W;
12
I want to create a simple bash function which can convert these week numbers to a date range. I suppose function should accept 2 arguments: $number and $year, example:
$ week() { ......... }
$ number=12; year=2012
$ week $number $year
"Mon Mar 19 2012" - "Sun Mar 25 2012"
With GNU date
:
$ 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"
$