简体   繁体   English

如何获得实时时钟(RTC)的当前时间戳?

[英]How to get current timestamp of realtime-clock (RTC)?

I would like to get the current timestamp of realtime clock in my BASH script. 我想在BASH脚本中获取实时时钟的当前时间戳。 The hwclock command can print out the current time, but it is not timestamp. hwclock命令可以打印出当前时间,但它不是时间戳。

I have considered parsing hwclock's output and then convert the results to a timestamp, but it seems the hwclock output will vary if the current locale gets changed. 我考虑过解析hwclock的输出然后将结果转换为时间戳,但是如果当前的语言环境发生变化,hwclock输出似乎会有所不同。 So if I give my script to my clients, they could use different locales than mine, and my parsing results could be wrong (and of course the timestamp will be wrong). 因此,如果我将我的脚本提供给我的客户,他们可以使用不同于我的语言环境,并且我的解析结果可能是错误的(当然时间戳将是错误的)。

So my question is, what would be the best way to get timestamp from RTC? 所以我的问题是,从RTC获取时间戳的最佳方法是什么? Thanks in advance. 提前致谢。

When you call hwclock -D , you get an output like the following which contains the timestamp: 当你调用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

The following command parses this output to extract the actual timestamp: 以下命令解析此输出以提取实际时间戳:

sudo hwclock -D | grep "Hw clock time" | sed "s/.* = \([0-9]\+\) seconds .*/\1/"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM