简体   繁体   中英

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

I would like to get the current timestamp of realtime clock in my BASH script. The hwclock command can print out the current time, but it is not timestamp.

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. 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? Thanks in advance.

When you call hwclock -D , you get an output like the following which contains the timestamp:

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/"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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