I have a project that uses Raspberry in offline mode. We know it doesn't have a clock when it powers off.

The router, even in offline mode, have a clock when it power off. So, i have 2 questions:

1 - It's possible configure raspberry to get a hour of router at starting?

2 - How i set a specific datetime in raspberry manually, via command prompt for example?

Thanks a lot!

  • 1
    If time is important spend $2 on a RTC. Otherwise the answer depends on your external hardware i.e. it is not a Pi question. – Milliways Dec 7 '17 at 11:56

There's a program called fake-hwclock. You can install it using:

apt-get install fake-hwclock

It runs as service when the unit boots which restores the last known timestamp. This ensures the system time is not reset to the default beginning of UNIX EPOCH of 1970.

The above program combined with the ntpdate package, should keep a RPi reasonably close to the actual time depending on network access and reboot cycles.

However, as pointed out in the comments, adding a RTC module will ensure accurate time keeping. So, it depends on your particular use case.

Use of ntpdate

If you do use ntpdate, it's probably best to use the ntp pool servers, and set a cron job to run every hour or so to check the pool for the correct time.

#crontab -e

0 * * * *   /usr/sbin/ntpdate pool.ntp.org
  • If using ntpdate, as above, I'd prefer to use the following in the crontab so that the time is correct from the outstet: @reboot /usr/sbin/ntpdate pool.ntp.org – agurney Dec 14 '17 at 0:03

For the second question:

Use date -s:

date -s '2014-12-25 12:34:56'

Run that as root or under sudo. Changing only one of the year/month/day is more of a challenge and will involve repeating bits of the current date. There are also GUI date tools built in to the major desktop environments, usually accessed through the clock.

To change only part of the time, you can use command substitution in the date string:

date -s "2014-12-25 $(date +%H:%M:%S)"

will change the date, but keep the time. See man date for formatting details to construct other combinations: the individual components are %Y, %m, %d, %H, %M, and %S.


Your Answer

