GNU日期是否有错误? (计算偏移量)

时间:2018-11-15 18:58:59

标签: date offset gnu

我试图按周对日志文件条目进行分组,并使用GNU日期查找时间戳偏移以与日志条目进行比较:

$ date --version
date (GNU coreutils) 8.25

$ today=$(date +%F)
$ ts_sow=$(date -d "$today - $(( $(date -d $today +%u) - 1) days" +%F)

这给出了当前星期星期一的日期。

$ echo $today
2018-11-15
$ echo $ts_sow
2018-11-12

现在,当我想返回上一周时,我可以从本周开始起偏移7天...

$ ts_sopw=$(date -d "$ts_sow - 7 days" +%F)
$ echo $ts_sopw
2018.11-05

但是,如果我尝试将两个偏移量都应用到原始开始日期,则会得出错误的结果...

$ ts_sopw=$(date -d "$today - $(( $(date -d $today +%u) - 1 - 7 )) days" +$F)
$ echo $ts_sopw
2018-11-11

这是一个错误,还是还有我不理解的事情?

1 个答案:

答案 0 :(得分:0)

With the correct arithmetic, I can get the correct result:

$ ts_sopw=$(date -d "$today - $(( $(date -d $today +%u) - 1 + 7 )) days" +$F)
$ echo $ts_sopw
2018-11-05

I was the bug!