0

Background: I successfully configured a DS3231 RTC to keep time and I can correctly read it with hwclock -r or hwclock -D commands.

Question: How do I store timestamp value (e.g. 1500075260) into a bash variable from hwclock command?

Note: Output from hwclock -D command looks something like the next paragraph. I guess one way would be to store this output into a TXT file and get the timestamp by using text manipulation commands. I'd like to know if there is an easier way.

hwclock from util-linux 2.25.2
Using the /dev interface to the clock.
Last drift adjustment done at 1500051827 seconds after 1969
Last calibration done at 1500051827 seconds after 1969
Hardware clock is on UTC time
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
/dev/rtc does not have interrupt functions. Waiting in loop for time from /dev/rtc to change
...got clock tick
Time read from Hardware Clock: 2017/07/14 23:34:20
Hw clock time : 2017/07/14 23:34:20 = 1500075260 seconds since 1969
Fri Jul 14 18:34:20 2017  -0.947188 seconds
2
  • 1
    Why do you need to read the value from the clock instead of the system, since the clock is being set from the RTC? Commented Jul 14, 2017 at 23:50
  • @SteveRobillard First I'd like to note that my RPi is not gonna have access to internet. Second, I'm using timestamp to keep track of some sensors data. Last, I don't want to have two different devices counting time (RTC and RPi) because that might damage time continuity in my sensor data (Reason for this is the drift in time that there would be between RPi and RTC over long periods of time) Commented Jul 14, 2017 at 23:53

1 Answer 1

1

Use this simple bash script to get timestamp from hwclock -D command.

function timestamp_from_rtc_get {   
    tmp="$(sudo hwclock -D)"  
    tmp=${tmp##*= } 
    out=${tmp%% seconds*} 
    echo $out
}
timestamp_from_rtc_get

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.