Path: utzoo!utgpu!watserv1!watmath!att!tut.cis.ohio-state.edu!zaphod.mps.ohio-state.edu!wuarchive!uunet!viusys!alembic!csu From: csu@alembic.acs.com (Dave Mack) Newsgroups: comp.sources.d Subject: Re: Obtaining time info from Naval Observatory Message-ID: <1991Jan11.040746.7981@alembic.acs.com> Date: 11 Jan 91 04:07:46 GMT References: <1991Jan09.003434.5379@esleng.uucp> Organization: Alembic Computer Services McLean VA Lines: 154 In article <1991Jan09.003434.5379@esleng.uucp> dag@esleng.uucp (David A. Gilmour) writes: >I have seen references to a program which can automatically obtain >the time from the Naval Observatory and update the system clock. > >If anyone knows where I can get the source for this program I'd >appreciate a note via mail. > >Thanks in advance. Warren Tucker's nbstime code is pretty specific to Xenix on a PC clone. Here's what I hacked together using that as a starting point. I have a shar file of Warren's original if you need it. This should run on nearly any BSD system and shouldn't be too hard to port (back) to System V. put this in /etc/remote (after modifying for your modem) : # Naval Observatory Time Standard 1200 baud (202)653-0351 notime:pn=12026530351:tc=unixPEP: then compile the following code and put the executable somewhere in your path (I call it bsdnbs - it's a very hacked up version of Warren Tucker's code. If there are problems, blame me, not Warren.) Then try: tip notime | bsdnbs It should tell you by how many seconds your system time was changed. Then a "~." will kill tip and hang you up. Since it resets the system time, you have to run it as root. bsdnbs.c: -------------------------------- cut here -------------------------------- /* Warren H. Tucker, 150 West Lake Drive, Mountain Park, GA 30075 (404)587-5766 */ /* * BSD modifications, hacks to accept the Naval Obs. signal from * stdin, etc., due to Dave Mack, csu@alembic.acs.com. * * Compile with: cc -o bsdnbs -O bsdnbs.c * * Runs on a Sun-4, SunOS4.1 */ #include #include #include #include #include #include #include #include #include #include #include #ifndef ushort #define ushort unsigned short #endif #ifndef uchar #define uchar unsigned char #endif #ifndef uint #define uint unsigned int #endif #ifndef ulong #define ulong unsigned long #endif #define EPOCH 40587 /* UNIX starts JD 2440587, */ #define leap(y, m) ((y+m-1 - 70%m) / m) /* also known as 1/1/70 */ #define TONE '*' /* #define TIME "\n%05ld %03d %02d%02d%02d UTC" */ #define TIME "%05ld %03d %02d%02d%02d UTC" /* for better source line utilization, frequent use of 'fprintf' and 'stderr' warrants the following */ #define pf printf #define ff fprintf #define se stderr #define so stdout main(argc,argv,envp) int argc; char **argv; char **envp; { char *cptr; int iargv; int swchar; int itmp; char rd_buf[64]; long now; long julian; long time_at_connect; int day_of_year; int year; int month; int day; int hour; int min; int sec; int fdcmos; int have_time = 0; int max_tries; struct tm *lt; char c; struct timeval tp; struct timezone tzp; long oldsecs; for (;;) { /* find the designator */ while ((c = getchar()) != '*') ; /* skip the whitespace (CRLF) */ getchar(); getchar(); /* get the time */ cptr = rd_buf; while ((*cptr = getchar()) != 'C') cptr++; *(++cptr) = '\0'; if(sscanf(rd_buf,TIME,&julian,&day_of_year,&hour,&min,&sec) != 5) { fprintf(stderr,"missed\n"); continue; } else { have_time = 1; break; } } if(have_time) { now = (((julian - EPOCH) * 24 + hour) * 60 + min) * 60 + sec; printf("Time retrieved from standard: %s\n",ctime(&now)); gettimeofday(&tp,&tzp); oldsecs = tp.tv_sec - now; tp.tv_sec = now; settimeofday(&tp,&tzp); printf("System time reset by %d seconds\n",oldsecs); exit(0); } else { printf("Did not get time ... sorry\n"); exit(254); } } /* end of main */