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,336
  • 6
  • 66
  • 105
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,389
  • 9
  • 22
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
2

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

scruss
  • 9,117
  • 1
  • 25
  • 36