UNIX日期:如何星期数转换为日期范围内(周一至周日)? [英] UNIX date: How to convert week number to a date range (Mon-Sun)?
本文介绍了UNIX日期:如何星期数转换为日期范围内(周一至周日)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有巨大的日志文件中抽取一周号码列表,它们使用的语法提取
$日期--date =周三3月20日十点19分56秒2012+%的W;
12
我想创建一个简单的bash功能,可以将这些周数转换为日期范围。我想功能应该接受2个参数:$数量和$的一年,例如:
$周(){} .........
$数= 12;年= 2012
$ $每周$数年
星期一2012年3月19日 - 太阳2012年3月25日
解决方案
通过 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"
$
这篇关于UNIX日期:如何星期数转换为日期范围内(周一至周日)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文