/Users/lyon/j4p/src/net/date/AtomicClock.java

1    package net.date; 
2     
3    /** 
4     * Class:   GetTimeConsole 
5     * @author  James Linn, D. Lyon 
6     * 
7     * Date:    1/20/2001 
8     * Time:    11:14:12 PM 
9     */ 
10    
11   import java.io.BufferedReader; 
12   import java.io.IOException; 
13   import java.io.InputStreamReader; 
14   import java.io.File; 
15   import java.net.Socket; 
16   import java.net.UnknownHostException; 
17   import java.text.ParseException; 
18   import java.text.SimpleDateFormat; 
19   import java.util.GregorianCalendar; 
20   import java.util.StringTokenizer; 
21   import java.util.TimeZone; 
22    
23   /** Get the time from the NIST atomic clock 
24    * 
25    * @author Douglas Lyon 
26    * @version 1.0 
27    */ 
28   public class AtomicClock { 
29     /** get a reader that reads the time 
30      * 
31      * @since 9/4/02 
32      * @exception java.net.UnknownHostException It cannot connect to NIST 
33      * @exception java.io.IOException When we cannot set the time 
34      * @return the buffered reader 
35      */ 
36     public static BufferedReader getReader() 
37         throws UnknownHostException, 
38             IOException { 
39       Socket s = new Socket( 
40           "time-A.timefreq.bldrdoc.gov", 13); 
41       return new BufferedReader( 
42           new InputStreamReader( 
43               s.getInputStream())); 
44     } 
45    
46     /** set the system clock 
47      * 
48      * @since 9/4/02 
49      * @param c A calender instance that is used to set the time 
50      * @exception java.io.IOException When we cannot set the time 
51      */ 
52     public static void setSystemTime(GregorianCalendar c) 
53         throws IOException { 
54       // Now set the computer to this time: 
55       Runtime rt = Runtime.getRuntime(); 
56       StringTokenizer st = new StringTokenizer( 
57           c.getTime().toString(), " "); 
58       for (int i = 0; i < 3; i++) 
59         st.nextToken(); 
60    
61       String[] execArgArray = new String[2]; 
62       execArgArray[0] = "c:\\SW409\\settime.bat"; 
63       execArgArray[1] = st.nextToken(); 
64       rt.exec(execArgArray); 
65     } 
66    
67     /** Convert the NIST time to an instance of a calendar 
68      * 
69      * @param line The raw string returned from NIST 
70      * @return a calendar instance 
71      */ 
72     public static GregorianCalendar parseDate(String line) { 
73       StringTokenizer sTok = 
74           new StringTokenizer(line, " "); 
75       sTok.nextToken(); 
76       // Get # at the beginning, 
77       // whatever it represents 
78       String sDate = sTok.nextToken(); 
79       String sTime = sTok.nextToken(); 
80       SimpleDateFormat df = 
81           new SimpleDateFormat("yy-MM-dd hh:mm:ss"); 
82       df.setTimeZone(TimeZone.getTimeZone("GMT")); 
83       GregorianCalendar cal = new GregorianCalendar(); 
84       cal.setTimeZone(TimeZone.getTimeZone("GMT")); 
85       try { 
86         cal.setTime(df.parse(sDate + " " + sTime)); 
87       } catch (ParseException e) { 
88         System.out.println(e.getMessage()); 
89       } 
90       System.out.println(cal.getTime()); 
91       return cal; 
92     } 
93    
94     /** @param s 
95      * @exception java.io.IOException 
96      */ 
97     public static void processLine(String s) 
98         throws IOException { 
99       if (s.length() == 0) return; 
100      System.out.println(s); 
101      setSystemTime(parseDate(s)); 
102      sleep(); 
103    } 
104   
105    /** Reads in the time to internal state variables 
106     * 
107     * @exception java.net.UnknownHostException 
108     * @exception java.io.IOException 
109     */ 
110    public static void getTime() 
111        throws UnknownHostException, IOException { 
112      BufferedReader br = getReader(); 
113      String s = null; 
114      while ((s = br.readLine()) != null) 
115        processLine(s); 
116    } 
117   
118    public static void sleep() { 
119      try { 
120        Thread.sleep(5000); 
121        // Gives time to read msg 
122      } catch (InterruptedException ie) { 
123      } 
124    } 
125   
126    /** A unit test 
127     * 
128     * @since 1.0 
129     * @param args not used 
130     */ 
131    public static void main(String[] args) { 
132      try { 
133        AtomicClock.getTime(); 
134      } catch (Exception e) { 
135        e.printStackTrace(); 
136      } 
137    } 
138  } 
139