我想使用BusyBox的date
命令(BusyBox v1.21.0)设置日期。我要设置计算机的自定义日期为:
Tue, 15 Jan 2019 10:46:13 GMT
我的date
命令能够使用以下字符串以相同格式打印日期:
date +"%a, %d %b %Y %T %Z"
它以与上面完全相同的格式返回日期。但是,当我使用-s
选项设置日期时,它不会接受这一点。
例如,这是失败的:
date -u +"%a, %d %b %Y %T %Z" -s "Wed, 17 Feb 2010 19:14:32 UTC"
date: invalid date 'Wed, 17 Feb 2010 19:14:32 UTC'
我知道busybox命令在函数上减少了,但我设想当它能够处理格式字符串以以所需的形式打印当前日期时,它还应该能够使用它来解释输入字符串。
发布于 2019-01-15 11:40:39
busybox date
只接受非常特定的时间格式,而不是任意的时间格式。
$ busybox date --help
[...]
[-s,--set] TIME Set time to TIME
[...]
Recognized TIME formats:
hh:mm[:ss]
[YYYY.]MM.DD-hh:mm[:ss]
YYYY-MM-DD hh:mm[:ss]
[[[[[YY]YY]MM]DD]hh]mm[.ss]
'date TIME' form accepts MMDDhhmm[[YY]YY][.ss] instead
因此,您只需编写它,比如date -s 2010.02.17-19:14:32
(或您喜欢的任何格式)。
发布于 2021-04-01 13:39:58
某些版本的busybox日期使用-D
选项接受自定义格式作为输入。但是,在花了很长时间之后,这个选项不支持时区格式%Z
。
C strftime
:C库函数- strftime()支持的所有格式列表
注意:需要从输出格式的开头删除+
,以便与-D
一起用作输入格式。"+%FT%T%z"
变成-D "%FT%T%z"
。
## Date context for other commands
# date -Iminutes
2021-04-01T13:30+0000
# date -u -D '%b %e %Y' -d "Apr 1 2021"
Thu Apr 1 00:00:00 UTC 2021
# date -u -d "2021 04xx25" -D '%Y %mxx%d'
Sun Apr 25 00:00:00 UTC 2021
# date -u -d "5 12:35:58" -D "%e %H:%M:%S"
Mon Apr 5 12:35:58 UTC 2021
带有时区
## Format returned by `openssl x509 -enddate`
# date -u +"%b %e %H:%M:%S %Y %Z"
Apr 1 13:33:58 2021 UTC
# # date -u -D "%b %e %H:%M:%S %Y %Z" -d "Apr 1 13:33:58 2021 UTC"
date: invalid date 'Apr 1 13:33:58 2021 UTC'
## removing the %Z timezone from the input:
# date -u -D "%b %e %H:%M:%S %Y" -d "Apr 1 13:33:58 2021 UTC"
Thu Apr 1 13:33:58 UTC 2021
## yet providing %Z in output format works well
# date -u -D "%b %e %H:%M:%S %Y" -d "Apr 1 13:33:58 2021 UTC" +"%F %T %Z"
2021-04-01 13:33:58 UTC
运行高寒linux busybox
v1.32.1
的容器
# uname -a
Linux f4d750b1edf8 4.19.121-linuxkit #1 SMP Thu Jan 21 15:36:34 UTC 2021 x86_64 Linux
# busybox date --help
BusyBox v1.32.1 () multi-call binary.
Usage: date [OPTIONS] [+FMT] [TIME]
Display time (using +FMT), or set time
[-s,--set] TIME Set time to TIME
-u,--utc Work in UTC (don't convert to local time)
-R,--rfc-2822 Output RFC-2822 compliant date string
-I[SPEC] Output ISO-8601 compliant date string
SPEC='date' (default) for date only,
'hours', 'minutes', or 'seconds' for date and
time to the indicated precision
-r,--reference FILE Display last modification time of FILE
-d,--date TIME Display TIME, not 'now'
-D FMT Use FMT (strptime format) for -d TIME conversion
Recognized TIME formats:
hh:mm[:ss]
[YYYY.]MM.DD-hh:mm[:ss]
YYYY-MM-DD hh:mm[:ss]
[[[[[YY]YY]MM]DD]hh]mm[.ss]
'date TIME' form accepts MMDDhhmm[[YY]YY][.ss] instead
发布于 2021-12-20 07:16:18
在通过netinstall安装Debian11.2时,由于系统日期比实际日期提前45天,我遇到了一个问题。我需要通过以下命令date +"2021-12-20 12:37:00"
来更新日期--由于busybox的原因,它与通常的日期命令略有不同
https://unix.stackexchange.com/questions/494581
复制相似问题