Take the 2-minute tour ×
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

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

share|improve this question

4 Answers 4

up vote 6 down vote accepted

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

share|improve this answer
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. –  ppumkin Feb 13 '13 at 9:24
    
That's a Debian package... can it work for Arch? –  T.C. 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: archplusplus.co.uk/post/40202081414/… –  levinalex Apr 8 '13 at 12:20

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;
share|improve this answer
1  
How is the licensing change now? –  T.C. Feb 13 '13 at 12:21
    
+1 Ahhh. Much better. Thank you kind sir. –  ppumkin Feb 13 '13 at 13:44

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.

share|improve this answer
    
So there is no daemon that writes the current time to disk, and then reads in again if there is no network connection? –  T.C. Feb 8 '13 at 11:35
    
@T.C. My experience with Raspbian suggests it does exactly that. –  RedGrittyBrick Feb 8 '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. –  T.C. Feb 8 '13 at 21:42
1  
-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. –  ppumkin Feb 13 '13 at 9:25

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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