Page 2 of 2

Re: How to Update Date and Time With out Internet

Posted: Mon Aug 25, 2014 1:35 pm
by Joe Schmoe
sprinkmeier wrote: Start with the PC's OS :-)

http://www.debian.org/
That's the point. If the source OS wasn't Windows, we wouldn't be having this conversation.
Note that I've said since the beginning that it (getting the "ntp" server running correctly) almost certainly *is* doable under Windows, but it will be painful (as the text since then in this thread bears out). And you'll end up feeling dirty.

In fact, probably the most straightforward way to do it under Windows is to run some kind of emulator (QEMU, VirtualBox, coLinux, whatever) on the Windows box and then run the normal Linux stuff there.

And finally, when all is said and done, the best solution is probably to buy an RTC board for the Pi. As I understand it, the following are true:
  • 1) The needed hardware can be bought for (literally) $2 or $3.

    2) However, installing it on the Pi is not trivial. Note that I didn't say it was hard or complicated, but it is not trivial.
It seems to me that someone could make a nice little profit by taking one of the available RTC board, bundling it with the needed cables and good instructions and sell it for something in the $5-$10 range. I'd certainly buy one if such were available.

Re: How to Update Date and Time With out Internet

Posted: Mon Aug 25, 2014 1:49 pm
by sprinkmeier
Joe Schmoe wrote:1) The needed hardware can be bought for (literally) $2 or $3.
That may be, but I still like the camera + sundial approach.
The only potential downside would be the need for a second, time-synchronised Pi to drive a motorised light for indoor use...

Re: How to Update Date and Time With out Internet

Posted: Mon Aug 25, 2014 6:36 pm
by hippy
One pleasant surprise I got was that a Wi-Fi dongle connected to an open BT Fon access point ( a public phone box at the end of the street ) automatically gets the date and time set even though I don't have a BT account.

A further thought on simple home grown solutions other than getting an NTP server running on Windows; if you can code a Windows HTTP server you can force that to return a shell script to set the date and time, use a simple script to wget it then execute it.

I already have a self-written HTTP server running on my PC so that was an easy mod to make and seems to work.

Re: How to Update Date and Time With out Internet

Posted: Mon Aug 25, 2014 7:28 pm
by Joe Schmoe
I already have a self-written HTTP server running on my PC so that was an easy mod to make and seems to work.
That's quite interesting. Without going into *too* much detail, what mechanism did you use to automate the server side - i.e., to get it to run the code to look up the current time and return it as text?

A couple of notes related to this:
  • 1) You don't really need to return a full shell script. Just the raw data - in whatever format you prefer - in fact, best would be the raw Epoch number (e.g., 1408994312). Then the client can do something like: date -s @$(wget …)

    2) Another way, that works well on Unix hosts (not sure about Windows, although Cygwin probably supplies something usable) is to run the "daytime" server on port 13. Then you use a similar "date" command like in the previous note to grab the output and set the local time accordingly. This is based on the fact that GNU date can easily "de-parse" the output of "daytime".

    I've used this hack in the past and I think another poster mentioned it upthread.

Re: How to Update Date and Time With out Internet

Posted: Sun Sep 07, 2014 3:27 pm
by hippy
Joe Schmoe wrote:
I already have a self-written HTTP server running on my PC so that was an easy mod to make and seems to work.
That's quite interesting. Without going into *too* much detail, what mechanism did you use to automate the server side - i.e., to get it to run the code to look up the current time and return it as text?
I just looked for a "GET /datexxx.sh" and returned a CGI-style response with content of ...

sudo date --set YYYY-MM-DD
sudo date --set HH:MM:SS

substituting YYYY-MM-DD and HH:MM:SS with appropriate values from the PC's local time. And on the Pi I have a 'settime.sh' script of ...

rm datexxx.sh
wget 172.16.0.206/datexxx.sh
chmod +x datexxx.sh
./datexxx.sh

Re: How to Update Date and Time With out Internet

Posted: Sun Sep 07, 2014 3:44 pm
by Joe Schmoe
I just looked for a "GET /datexxx.sh" and returned a CGI-style response with content of ...
Meaning no disrespect, of course, but your post told me all the parts of the process that I already know. All those parts are "standard Unix". The part that I *don't* understand is what piece of software "looks for a 'GET…". What are you running as your HTTP server? What configuration needs to be done on that piece of software to get it to "look" for a "GET…" ?

Note that some people know a lot about web server processing but don't know much about Unix, while other people know a lot about Unix, but don't know much about web stuff. I'm in the later category, so go easy on me.

Re: How to Update Date and Time With out Internet

Posted: Sun Sep 07, 2014 4:59 pm
by jojopi
hippy wrote:rm datexxx.sh
wget 172.16.0.206/datexxx.sh
chmod +x datexxx.sh
./datexxx.sh
Even if you control both machines, it is the worst possible practice to confuse data with code like this. The client needs to trust the server to provide an accurate timestamp; it should not trust the server to decide what commands to run.

Apart from the security implications, it is simply not the server's job to know how to set the client's clock, or whether sudo will be required.

Also, it is important to get the date and time simultaneously, rather than independently. It is more efficient, and it avoids a bug where the clock rolls over midnight between the two operations.

Finally, you should always use UTC between machines. Otherwise, even if they do have the same local timezone, there is still an ambiguity if you try to set the clock during the hour that is repeated at the end of summer time.

So, if the server returns a timestamp in the form given by "date -u +%Y-%m-%dT%H:%M:%SZ", the client should do something like:

Code: Select all

time="$(wget -qO- 172.16.0.206/datexxx.txt)"
case "$time" in
  2???-??-??T??:??:??Z) sudo date --set "$time" ;;
  *) echo "received bad timestamp: $time" >&2 ;;
esac

Re: How to Update Date and Time With out Internet

Posted: Sun Sep 07, 2014 5:10 pm
by Joe Schmoe
Even if you control both machines, it is the worst possible practice to confuse data with code like this. The client needs to trust the server to provide an accurate timestamp; it should not trust the server to decide what commands to run.
I believe I already made this point, a few responses back, without all the hyperbole.

Re: How to Update Date and Time With out Internet

Posted: Sun Sep 07, 2014 10:31 pm
by hippy
@ jojopi: And not forgetting the bad practice of using a hard-wired IP address.

It was however only proof of concept code, just a quick, minimal effort solution to not having anything better. It has been working well enough for me since I wrote it but it could be improved greatly. It works but there are far better alternatives.

I'm also no expert in Linux so "date -s @$(wget …)" tricks are things I am not familiar with, nor could I figure how to set data and time atomically, so I just threw out what I could make work.

@ Joe: My server is written in VB6 running on a Windows XP box. That uses a Winsock control which receives the command, I simply parse that, extract the filename, decide what to do, send the data back. Not sure further detail is useful as I doubt many here are using VB6 - It was written a long while ago, works so I never ported it to anything else, I just keep tweaking it as needs arise!

I am however wanting to move that set-up onto a Pi and re-writing in Python seems the sensible move. Googling for Simple Python Web Server shows some useful code examples. My server is pretty much as the code on the page below but in VB6 and with a far more complicated Do_GET() routine -

http://fragments.turtlemeat.com/pythonwebserver.php

Something like that is a good starting point. Then reading up on HTTP requests and replies, tailoring it to have better parsing, security and to do what it needs to do.

Re: How to Update Date and Time With out Internet

Posted: Sun Sep 07, 2014 10:47 pm
by Joe Schmoe
OK. Got ya.

FWIW, I'm not a fan of Python - for a variety of reasons, just never got around to learning it.

You can put me in the "Significant whitespace is evil" camp.

Anyway, if I were going to do this, I think I'd do it in GAWK, using the built-in networking capabilities.

Thanks for the pointers, though. Quite helpful.

BTW, I have written an RDate server in GAWK (several years ago) - developed and tested under Linux, but should also work under Windows (with a suitable GAWK binary). IMHO, that's actually the best way to answer this thread.