在UNIX中处理日期 [英] Manipulate date in UNIX
问题描述
我想找到一种在UNIX中列出当前星期的所有日期的方法。
例如,如果今天是2018年7月17日,那么整周如何获取日期16-07、18-07、19-07等?即昨天,明天,后天等等。
由于未安装GNU,我无法在 date 命令中使用--date / d选项。其他任何命令或用户定义的函数来获取给定的所有日期(当前日期或任何日期)?
I want to find a way to list all the dates of the current week in UNIX. For example if today is 17-07-2018 then how to get dates 16-07, 18-07, 19-07 and so on for the whole week? I.e., yesterday, tomorrow, day after tomorrow etc. I cannot use --date/d options with date command as GNU is not installed. Any other commands or a user defined function to get all the dates of the given (current or any date)?
推荐答案
假设日期
可以识别小时数的TZ偏移量,适用于当前日期:
Assuming your date
recognizes TZ offsets in hours, this works for the current date:
#!/bin/sh
case $(date +%A) in
(Monday) off=" +0 -24 -48 -72 -96 -120 -144";;
(Tuesday) off=" +24 +0 -24 -48 -72 -96 -120";;
(Wednesday) off=" +48 +24 +0 -24 -48 -72 -96";;
(Thursday) off=" +72 +48 +24 +0 -24 -48 -72";;
(Friday) off=" +96 +72 +48 +24 +0 -24 -48";;
(Saturday) off="+120 +96 +72 +48 +24 +0 -24";;
(Sunday) off="+144 +120 +96 +72 +48 +24 +0";;
esac
for o in $off; do
TZ="$o" date +%d-%m
done
示例运行:
$ date
Sun Jul 15 23:13:43 CEST 2018
$ ./x.sh
09-07
10-07
11-07
12-07
13-07
14-07
15-07
leap年,下溢/上溢没有问题,因为它使用 date
的内置知识。
This has no problem with leap years, under/overflow, since it uses date
's built-in knowledge.
PS:我刚刚发现我的 date
(在FreeBSD上)采用UTC的偏移量,而不是系统时区的偏移量;您应该针对AIX进行调查,如果是这样,请通过将您当地时区的偏移量调整为UTC来相应地调整偏移量。
PS: I just discovered that my date
(on FreeBSD) takes these offsets from UTC, not the systems time zone; you should investigate this for AIX and if so, adjust the offsets accordingly by your local time zone offset to UTC.
这篇关于在UNIX中处理日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!