How to set (Linux) system time

I'm surprised I don't find anything on changing the (Linux) system time from Rust.

Specifically, right now I have a chrono::NaiveDateTime struct which I want to set as my new system time, then update the RTC. On the command line, I would do:

date "2019-08-30 08:32:12"
hwclock -w

Command

Oh well why not. I just hoped to avoid this route.

Reason?

I'm not sure about this settimeofday.

2 Likes

Changing the time of day is a privileged operation (the exact privilege is CAP_SYS_TIME on Linux).
Just call a function won't work as it will error out with EPERM.

If you really want to do this, I recommend you use setcap to give you programs only CAP_SYS_TIME instead of setid-ing it, as setid give all permission. But I honestly, I strongly recommend against messing with privileged stuff without a really good reason.

2 Likes