How to set the clock/time?


25 posts
by peanutismint » Sat Nov 24, 2012 10:11 pm
I've used a custom image of Raspbian which has a setup window allowing me to select my world location (for setting date/time etc) but after setting it to the right location, the clock is still wrong. It appears that it's getting incorrect date/time info from the internet, but I don't know how to just tell the RasPi what date/time it is as I know it doesn't have any sort of onboard BIOS.....(is that right??)

If so, does that mean it has to manually get the date/time from an online source every time it boots up? Or is there some kind of rudimentary on-board clock with a battery??
Posts: 35
Joined: Thu Nov 22, 2012 7:14 pm
by bulletmark » Sat Nov 24, 2012 10:30 pm
There is no onboard clock. Read the FAQ link above to see why. The ntpd program (from the ntp package) is started at boot to sync time from the internet. You can use the date command to set time manually.
Posts: 83
Joined: Wed Oct 17, 2012 10:10 pm
Location: Brisbane Australia
by peanutismint » Sat Nov 24, 2012 10:35 pm
Ah I see. But if I set the correct date/time using this 'date' command, does that mean it won't 'remember' it the next time it's booted up?

Also, if this is the case - what's the best way for me to find out why it's not getting the correct time from the internet??
Posts: 35
Joined: Thu Nov 22, 2012 7:14 pm
by aaa801 » Sat Nov 24, 2012 10:44 pm
peanutismint wrote:Ah I see. But if I set the correct date/time using this 'date' command, does that mean it won't 'remember' it the next time it's booted up?

Also, if this is the case - what's the best way for me to find out why it's not getting the correct time from the internet??


Prob because you are using the wrong timezone information ;)
Posts: 427
Joined: Mon Jun 04, 2012 9:06 pm
Location: Berkshire
by peanutismint » Sat Nov 24, 2012 10:48 pm
Hahah well in the aforementioned setup program I definitely selected 'London, UK' and I'm almost 100% sure that's where I am..... :lol:
Posts: 35
Joined: Thu Nov 22, 2012 7:14 pm
by bulletmark » Sat Nov 24, 2012 10:49 pm
If the Pi has been cold booted after being down for some time, and you don't have an internet connection, then you just have to set the clock manually.

Just make sure you can ping an internet site from your Pi command line. You could install the ntpdate command to try to sync internet time from the command line and then see what problem it reports. E.g. perhaps the ntp request is being blocked by a firewall? (i.e. UDP blocked outbound on port 123).
Posts: 83
Joined: Wed Oct 17, 2012 10:10 pm
Location: Brisbane Australia
by peanutismint » Sat Nov 24, 2012 10:52 pm
Ok, thanks guys, will give all these things a try. :)
Posts: 35
Joined: Thu Nov 22, 2012 7:14 pm
by sdjf » Sun Nov 25, 2012 2:13 am
Does this all mean that I have will have to reset the time on my Pi when I get it, every time I turn it on, since it will not be on the internet? That is not good news.
FORUM TIP: To view just one person's posting history, sign in, click on their user name, then click on "Search User's Posts." || This Pi owner is running Arch on 512MB Model B.
Posts: 1286
Joined: Fri Mar 16, 2012 5:20 am
Location: California
by aaa801 » Sun Nov 25, 2012 4:22 am
sdjf wrote:Does this all mean that I have will have to reset the time on my Pi when I get it, every time I turn it on, since it will not be on the internet? That is not good news.

You could hook up a rtc to it
http://www.cjemicros.co.uk/micros/indiv ... eClock-RTC
Posts: 427
Joined: Mon Jun 04, 2012 9:06 pm
Location: Berkshire
by itimpi » Sun Nov 25, 2012 6:57 am
sdjf wrote:Does this all mean that I have will have to reset the time on my Pi when I get it, every time I turn it on, since it will not be on the internet? That is not good news.

If the time needs to be accurate, then the answer is yes (or add an external RTC circuit). As has been mentioned the Pi normally relies in an Internet connection to set the time accurately. However if it is just important that time be sequential then you should be OK as on boot the clock should be initialised to the value it had when the Pi was last shut down.
Posts: 1033
Joined: Sun Sep 25, 2011 11:44 am
Location: Potters Bar, United Kingdom
by sdjf » Sun Nov 25, 2012 8:11 am
Thanks guys. About a RTC, it is just for me, so I think I'll just same some money and reset it when I log in once in a while.

Good to hear it will stay sequential, that some kind of time will be stored. I have been lazy and using a GUI on my other system to reset the time, I guess I have to learn how to do it from the command line, since I will be running it headless for quite a while.
FORUM TIP: To view just one person's posting history, sign in, click on their user name, then click on "Search User's Posts." || This Pi owner is running Arch on 512MB Model B.
Posts: 1286
Joined: Fri Mar 16, 2012 5:20 am
Location: California
by sdjf » Sun Nov 25, 2012 8:23 am
About the "date" command, it will not set the date unless you add some parameters. Entering "date" alone will only display the current time stored in the system.

The command to set the date is of the format:

date -s DATESTRING

but I have not figured out what format the date string should be in.

What I will probably be doing since I will be ssh'ing in to my Pi, is write a short script or "alias" that will display the proper date command as of the moment I go to log in, and at the same time, that command will log me in. My job will be to copy and paste the output from that into the terminal on the Pi when I get there, unless I want to for-go resetting the date and time that particular time.
FORUM TIP: To view just one person's posting history, sign in, click on their user name, then click on "Search User's Posts." || This Pi owner is running Arch on 512MB Model B.
Posts: 1286
Joined: Fri Mar 16, 2012 5:20 am
Location: California
by bulletmark » Sun Nov 25, 2012 9:00 am
sdjf wrote:but I have not figured out what format the date string should be in.

On linux if you want to know how to use a command what arguments + options it takes etc, then use the man command. So type "man date".
Posts: 83
Joined: Wed Oct 17, 2012 10:10 pm
Location: Brisbane Australia
by sdjf » Sun Nov 25, 2012 6:02 pm
@bulletmark: yeah, I know that about checking the man page, but every installation I have used, has it's own quirks with regards to what it accepts as a DATESTRING. I do not have my Pi yet and have to wait to see what it will accept. Testing on my current system, I cannot get it working to change date and time, just the time.

Once my elusive Pi arrives and gets up and running, then I can work on changing date as well as time.
FORUM TIP: To view just one person's posting history, sign in, click on their user name, then click on "Search User's Posts." || This Pi owner is running Arch on 512MB Model B.
Posts: 1286
Joined: Fri Mar 16, 2012 5:20 am
Location: California
by startreksteve » Mon Nov 26, 2012 11:40 am
sudo date --s 11:39:00
Posts: 43
Joined: Tue Mar 13, 2012 10:23 am
by Joe Schmoe » Mon Nov 26, 2012 1:24 pm
I note with interest that you say that the Pi is not (will not) be connected to the Internet, but that you are able to (and planning on regularly doing so) SSH in. This tells me that:

1) The Pi is TCP/IP connected - to at least one other computer.
2) That other computer is (also) not Internet connected.

You mentioned about having some sort of kludgey set-up to tell you what to set the time/date on the Pi to (and how to do it) - and others have focused on instructing you about how the Unix/GNU "date" command works.

Well, here I believe I can help. I have a similar situation here - where I have PCs (running Windows, but the same principles will apply if your PCs run some other normal OS) that keep good time - plus I have small Linux boxes (embedded devices) that don't. Now, for reasons that are beyond the scope of this document, these embedded devices cannot use "ntp".

So, what I did was to setup an Expect script (running from a cron scheduler) that telnets into the Linux machines and runs a fabricated "date" command - that sets the time/date on the Linux box to whatever it is on the host machine (using Expect code to fabricate the needed "date" comand).

Quite natty, all around. Write back if you are interested in further details.
Never answer the question you are asked. Rather, answer the question you wish you had been asked.

- Robert S. McNamara - quoted in "Fog of War" -
Posts: 2510
Joined: Sun Jan 15, 2012 1:11 pm
by iainmaoileoin » Tue Nov 27, 2012 10:59 am
I agree with you about the date command on different versions of unix being "quircky".

On my pi
Code: Select all
root@raspberrypi:~/gpio# uname -a
Linux raspberrypi 3.2.27+ #102 PREEMPT Sat Sep 1 01:00:50 BST 2012 armv6l GNU/Linux
root@raspberrypi:~/gpio# date 112710552012.30
Tue Nov 27 10:55:30 UTC 2012


did what it said on the tin.
month day hour minute century year "dot" seconds
Posts: 16
Joined: Tue Jul 31, 2012 10:57 pm
by Joe Schmoe » Tue Nov 27, 2012 11:29 am
Re: The issue of the "date" command.

1) The original format (where the numbers are all mushed together) is weird, but I thought (i.e., assumed, without really knowing) that it was standardized by POSIX somewhere along the way.
That, of course, doesn't mean that there aren't weirdities out there (say, on very old versions of Unix, like, maybe, SunOS)

2) If you are willing to limit yourself to GNU (which means, nowadays, Linux) systems, then you can use the nifty GNU syntax for datetime strings, which eliminates the need to memorize the old (mushed together) format. And this should be portable to any system running GNU date.
Never answer the question you are asked. Rather, answer the question you wish you had been asked.

- Robert S. McNamara - quoted in "Fog of War" -
Posts: 2510
Joined: Sun Jan 15, 2012 1:11 pm
by sdjf » Thu Nov 29, 2012 7:41 am
Joe Schmoe wrote:1) The Pi is TCP/IP connected - to at least one other computer.

Yes it will be connected, but only some of the time. At this point, I only expect to turn the Pi on when I will be ssh'ing in to it from one of my Zaurus PDAs. And the Pi will be running headless, at least for the first several months. Once my Pi has a monitor of it's own, my primary PDA will not necessarily be running or even connected to it very much of the time.
2) That other computer is (also) not Internet connected.

The other computer (my Embedded Linux PDA) is occasionally connected to the Internet, but only when necessary.

Joe Schmoe wrote:So, what I did was to setup an Expect script (running from a cron scheduler) that telnets into the Linux machines and runs a fabricated "date" command - that sets the time/date on the Linux box to whatever it is on the host machine (using Expect code to fabricate the needed "date" comand).


I am not sure I want to mess around with setting up my other computer (which is just a PDA) as a host, but I am curious as to what the Expect script is, and I would guess others may wonder as well. Is it a language or the name of a bash script you wrote?

Quite frankly, although it is less elegant, I think it will be easier and more appropriate for me to write a script or alias that will send the current date and time to the pi immediately before I log in live to my console there. I will enter a one word command, and it will send the current time stamp via ssh and then log me in.
FORUM TIP: To view just one person's posting history, sign in, click on their user name, then click on "Search User's Posts." || This Pi owner is running Arch on 512MB Model B.
Posts: 1286
Joined: Fri Mar 16, 2012 5:20 am
Location: California
by Joe Schmoe » Thu Nov 29, 2012 3:20 pm
Some miscellaneous comments...

1) Expect is a programming language, based on Tcl. Google is your friend.

2) The setup that I am suggesting here is basically an automated way of doing what you are proposing to do manually. You may or may not find it useful to automate the process.

3) I don't know anything about the Zaurus. Apparently, it does have "ssh". Does it have the rest of the usual Unix/Linux tools? If so, it should have Expect. Does it have a C compiler?

4) Note that I did it with telnet - but of course, you could do it exactly the same with ssh. Note also that I did it under Windows, but it was using all Unix-y tools (Expect, telnet, cron).

5) You mentioned setting up a machine as a "server" - indicating you didn't want to do that. But there is no server. Essentially, the only "server" involved is the Pi itself.
Never answer the question you are asked. Rather, answer the question you wish you had been asked.

- Robert S. McNamara - quoted in "Fog of War" -
Posts: 2510
Joined: Sun Jan 15, 2012 1:11 pm
by Jonn » Fri Dec 14, 2012 5:18 am
man fake-hwclock may help.

I imagine steps like:

  • Get the timekeeping computer to construct (maintain) a file.
  • Get the the data into YYYY-MM-DD hh:mm:ss format before the last step.
  • Copy the file to the RaspberryPi.
  • Move the file over fake-hwclock.data file.
  • Load (force) the fake-hwclock.data file into the fake-hwclock.

A login or init file on the RaspberryPi could handle the last two steps with lines like the highlighted commands here:

pi@raspberrypi ~ $ echo 2012-12-13 22:33 > fake-hwclock2.data
pi@raspberrypi ~ $ sudo mv fake-hwclock2.data /etc/fake-hwclock.data
pi@raspberrypi ~ $ cat /etc/fake-hwclock.data
2012-12-13 22:33
pi@raspberrypi ~ $ sudo /sbin/fake-hwclock load force
Thu Dec 13 22:33:00 UTC 2012

For RaspberryPi linux (2012-10-28-wheezy-raspbian), this would provide the right format:
pi@raspberrypi ~ $ date +%F" "%T
2012-12-13 23:16:28

MS Windows' variables would have to be rearranged:
>echo %date% %time%
Thu 12/13/2012 23:43:16.25

Maybe someone else can figure out how to automate the other steps?
Posts: 27
Joined: Wed Aug 01, 2012 12:15 pm
by JPB-HK » Sat Dec 15, 2012 3:12 am
Not sure if this was mentioned yet, but you can set up Raspberry Pi to get time from internet.
sudo nano /etc/rc.conf
Add:
DAEMONS=(!hwclock ntpd ntpdate)

This should update, but not sure how long it takes before updating the Raspberry Pi after booting.
John
"Trust me, I know what I'm doing!"-SH
Posts: 46
Joined: Thu Nov 22, 2012 5:00 pm
by elToro12 » Wed Jan 22, 2014 3:04 am
Joe schmoe, could you elaborate on this process?
So, what I did was to setup an Expect script (running from a cron scheduler) that telnets into the Linux machines and runs a fabricated "date" command - that sets the time/date on the Linux box to whatever it is on the host machine (using Expect code to fabricate the needed "date" comand).
Posts: 1
Joined: Wed Jan 22, 2014 2:56 am
by nuneham » Fri Jul 11, 2014 11:18 pm
Cannot anyone actually provide an answer to this question?

What is the command line on the prompt to set the time and date character by character?
Posts: 2
Joined: Mon Feb 03, 2014 3:49 pm
by Paul Webster » Sat Jul 12, 2014 6:57 am
It is shown in one of the earlier posts in his thread
viewtopic.php?t=23733&p=576776#p222270
and an earlier shows how to find other ways of doing it by typing "man date" (without the quotes).
User avatar
Posts: 377
Joined: Sat Jul 30, 2011 4:49 am
Location: London, UK