]> rtime.felk.cvut.cz Git - orte.git/blob - orte/java/src/org/ocera/orte/types/NtpTime.java
JORTE: fraction computation
[orte.git] / orte / java / src / org / ocera / orte / types / NtpTime.java
1 /* NtpTime.java */
2
3 /**
4  * Class NtpTime substitutes the struct NtpTime from
5  * C-source code defines in: 'typedefs_defines_rtps.h'
6  * 
7  *   typedef struct {
8  *     int32_t    seconds;    // time in seconds
9  *     uint32_t   fraction;   // time in seconds / 2^32
10  *   } NtpTime;
11  *
12  *
13  * @author Lukas Pokorny (lukas_pokorny@centrum.cz)
14  * @author CTU FEE Prague - Department of Control Engineering (dce.felk.cvut.cz)
15  * @author Project ORTE - OCERA Real Time Ethernet (www.ocera.org)
16  * @author dedication to Kj
17  * @version 0.1
18  *
19  *
20  * This program is free software; you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation; either version 2 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28  * GNU General Public License for more details.
29  *
30  */
31
32 package org.ocera.orte.types;
33  
34   /* TODO @fraction: v C pretypovat z long na uint32_t */
35
36 public class NtpTime {
37
38   private int  seconds;  /* time in seconds        */
39   private long fraction; /* time in seconds / 2^32 */
40
41
42   /**
43    * Default constructor. 
44    */
45   public NtpTime() {
46     //System.out.println(":j: instance of NtpTime created..");
47   }
48
49   /**
50    * User constructor of NtpTime with zero fraction part.  
51    */
52   public NtpTime(int sec) {
53     seconds  = sec;
54     fraction = 0;
55     //System.out.println(":j: instance of NtpTime created..");
56   }
57   
58    /**
59     * User constructor full defined.  
60     */
61   public NtpTime(int sec, long fract) {
62     seconds  = sec;
63     fraction = fract;
64     //System.out.println(":j: instance of NtpTime created..");
65   }
66
67   /**
68    * @return String with actual state of NtpTime instance.  
69    */
70   public String toString()
71   {
72     return("sec = " + seconds + " fraction = " + fraction);
73   }
74
75   /**
76    * Get NtpTime. 
77    * @return actual state of NtpTime instance
78    **/
79   public NtpTime get()
80   {
81     return this;
82   }
83
84   /**
85    * Get NtpTime in its decimal form.
86    * @return actual NtpTime's value
87    **/
88   public double getDecimal()
89   {
90     return this.seconds + (double)this.fraction / ((long)1 << 32);
91   }
92     
93   /* ****************************************************************** *
94    *                                                                    *
95    *                         native methods                             *
96    *                                                                    *
97    * ****************************************************************** */
98
99
100   /**
101    * NtpTimeToStringMs - converts NtpTime from number notation into its
102    * string notation in miliseconds
103    *
104    * @param time time given in JNtpTime object
105    * @return NtpTime in string notation in miliseconds
106    */
107    public static native
108    String NtpTimeToStringMs(NtpTime time);
109
110    /**
111    * NtpTimeToStringUs - converts NtpTime from number notation into its
112    * string notation in microseconds
113    *
114    * @param time time given in JNtpTime object
115    * @return NtpTime in string notation in microseconds
116    */
117    public static native
118    String NtpTimeToStringUs(NtpTime time);
119    
120 }