6

Is there a way to sync the clock with the internet (as easily done) upon one startup, then shutdown, move the computer to another place WITHOUT an internet connection, and get a reasonable time?

I'm not looking for "This is the real time as of right now", I'm looking for "Timestamps that are newer than 1970", and preferably accurate time (no re-living yesterday, please. Once was enough).

Piotr Kula
  • 17,168
  • 6
  • 63
  • 103
Aviator45003
  • 205
  • 1
  • 2
  • 7

4 Answers4

8

As mentioned in another question, the fake-hwclock package can help make sure that time keeps ticking forward and not resetting to 1970.

Edit: if you are not using Debian or Raspbian, but are playing with Arch Linux (Or anything with systemd), this site has a service that will work essentially the same as the package mentioned above

XTL
  • 1,358
  • 9
  • 19
  • 1
    +1 - This is definitely the easiest thing to do for the OP. Obviously the Pi needs to be moved quickly so it is stays as accurate as possible. Waiting days will not help. It would be like removing a battery from an analogue clock and then putting it back in a day or later. That period of time will be lost. – Piotr Kula Feb 13 '13 at 09:24
  • That's a Debian package... can it work for Arch? – Aviator45003 Feb 13 '13 at 11:26
  • The code should, yes. I don't know if there's a package in arch. – XTL Feb 17 '13 at 17:25
  • 2
    for Arch, use this: http://archplusplus.co.uk/post/40202081414/fake-hwclock-for-arch-linux-arm-on-raspberry-pi-using – levinalex Apr 08 '13 at 12:20
  • Note that `fake-hwclock` is installed by default on Raspbian now. – Deanna Jan 06 '16 at 00:18
4

I wrote a little script that does the time work.

This I hope works for all rPi users who want to have kind of accurate times, at least not wildly innacurate ones, and need to deal without an internet connection at times.

For having this run regularly, I have it run as a cron job every minute, so if I wait a minute after bootup, it'll update. Much easier if my Raspberry Pi crashes than using a service.

It also doesn't fight with network time, so if your network time is working (or will), this script won't care.

#!/bin/bash
# Version 1.0.
# By Aviator 45003, Aviator45003 [at] gmail.com
# This program is distributed under the GPL-3.0 Public License.
# However, it would be nice if you, being a programmer, if you make changes,
#               would send them to the original programmer so 
#               he could improve his style.

# Program must be run as root if the user wouldn't have permissions to
#               edit the PATH_TO_TIME_FILE

PATH_TO_TIME_FILE="/etc/time_rPi_last";
THRESHHOLD=1971;

# Any changes past this point should be submitted to Aviator45003@gmail.com

touch $PATH_TO_TIME_FILE;

CUR_DATE=$(date +"%Y");

# Check that we have a date to fall back on.
if ( (($CUR_DATE < $THRESHHOLD)) && [ -e $PATH_TO_TIME_FILE ] ) ; then
        NEW_DATE_STRING=$(cat $PATH_TO_TIME_FILE);
        date -u $NEW_DATE_STRING;
# If we don't have a fallback date, and we need one, just ask!
# If this is being called regularly, there is a problem.
elif (( $CUR_DATE < $THRESHHOLD )); then
        echo "Enter a new date: Month Day Hour Minute Year: MMDDhhmmYYYY";
        read NEW_DATE_STRING;
        date -u $NEW_DATE_STRING;
fi

# Write the fallback date. Also updates the file every time this script is run.
if (( $CUR_DATE > $THRESHHOLD)); then
        echo $(date +"%m%d%H%M%Y") > $PATH_TO_TIME_FILE;
fi

exit 0;
Aviator45003
  • 205
  • 1
  • 2
  • 7
3

You can set the clock with a keyboard or ssh session. For example,

sudo date --set='2013-02-07 23:51:50'

but you can't automatically set the correct time without a network connection. There is no real-time clock on the Raspberry Pi.

If by "reasonable time" you mean "timestamps that are newer than 1970" (but still wrong), I suppose you could put a date-set command with a later date in an init script.

swagstaff
  • 39
  • 1
  • So there is no daemon that writes the current time to disk, and then reads in again if there is no network connection? – Aviator45003 Feb 08 '13 at 11:35
  • @T.C. My experience with Raspbian suggests it does exactly that. – RedGrittyBrick Feb 08 '13 at 16:47
  • My experience with RaspRazor and Arch say they don't. I'm not looking for a pretty computer, I'm looking for more power and exactly what I need, and I LOVE the CLI. – Aviator45003 Feb 08 '13 at 21:42
  • 2
    -1 ; This is not the easiest thing to do! I do not want to type that in everytime my headless Pi starts up. The answer with fake-hwclock is the easiest! The most accurate is using a RTC with battery.Anything in-between is just experiential. – Piotr Kula Feb 13 '13 at 09:25
2

You can add many different types of I²C Real Time Clocks to the Raspberry Pi, f'rinstance RasClock/DS1307/PCF8563/DS3231.

scruss
  • 8,928
  • 1
  • 23
  • 34