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).

4 Answers 4

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

5
  • 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
    Commented Feb 13, 2013 at 9:24
  • That's a Debian package... can it work for Arch? Commented Feb 13, 2013 at 11:26
  • The code should, yes. I don't know if there's a package in arch.
    – XTL
    Commented Feb 17, 2013 at 17:25
  • 2
    for Arch, use this: archplusplus.co.uk/post/40202081414/…
    – levinalex
    Commented Apr 8, 2013 at 12:20
  • Note that fake-hwclock is installed by default on Raspbian now.
    – Deanna
    Commented Jan 6, 2016 at 0: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 [email protected]

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;
1
  • 1
    How is the licensing change now? Commented Feb 13, 2013 at 12:21
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.

4
  • So there is no daemon that writes the current time to disk, and then reads in again if there is no network connection? Commented Feb 8, 2013 at 11:35
  • @T.C. My experience with Raspbian suggests it does exactly that. Commented Feb 8, 2013 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. Commented Feb 8, 2013 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
    Commented Feb 13, 2013 at 9:25
2

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

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.