2

我想在我的 BASH 脚本中获取实时时钟的当前时间戳。hwclock 命令可以打印出当前时间,但不是时间戳。

我考虑过解析 hwclock 的输出,然后将结果转换为时间戳,但如果当前语言环境发生更改,hwclock 输出似乎会有所不同。因此,如果我将脚本提供给我的客户,他们可能会使用与我不同的语言环境,并且我的解析结果可能是错误的(当然时间戳也是错误的)。

所以我的问题是,从 RTC 获取时间戳的最佳方法是什么?提前致谢。

4

1 回答 1

1

当您调用 时hwclock -D,您会得到如下输出,其中包含时间戳:

hwclock from util-linux 2.27.1
Using the /dev interface to the clock.
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
...got clock tick
Time read from Hardware Clock: 2018/09/27 09:03:46
Hw clock time : 2018/09/27 09:03:46 = 1538039026 seconds since 1969
Time since last adjustment is 1538039026 seconds
Calculated Hardware Clock drift is 0.000000 seconds
Thu 27 Sep 2018 11:03:45 CEST  .749554 seconds

以下命令解析此输出以提取实际时间戳:

sudo hwclock -D | grep "Hw clock time" | sed "s/.* = \([0-9]\+\) seconds .*/\1/"
于 2018-09-27T09:06:37.983 回答