5d6380843dff710e64ade845b424cf49a9970e2a
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.core / src / org / eclipse / linuxtools / internal / lttng / core / event / LttngTimestamp.java
1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * William Bourque (wbourque@gmail.com) - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.internal.lttng.core.event;
14
15 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
16 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
17
18 /**
19 * <b><u>LttngTimestamp</u></b><p>
20 *
21 * Lttng specific implementation of the TmfTimestamp.<p>
22 *
23 * The Lttng implementation is the same as the basic Tmf Implementation but allow construction with a TmfTimestamp or a long.
24 */
25 public class LttngTimestamp extends TmfTimestamp {
26
27 /**
28 * Default Constructor.<p>
29 *
30 */
31 public LttngTimestamp() {
32 this(Long.MIN_VALUE);
33 }
34
35 /**
36 * Constructor with parameters.<p>
37 *
38 * @param newEventTime Time as long, unit expected to be nanoseconds
39 */
40 public LttngTimestamp(final long newEventTime) {
41 super(newEventTime, -9, 0);
42 }
43
44 /**
45 * Copy Constructor.<p>
46 *
47 * @param oldEventTime The timestamp object we want to copy from
48 */
49 public LttngTimestamp(final ITmfTimestamp oldEventTime) {
50 this(oldEventTime.getValue());
51 }
52
53 public void setValue(final long newValue) {
54 fValue = newValue;
55 }
56
57 /**
58 * Get the second part in timestamp.<p>
59 *
60 * Note : We do not use scale and assumes contents to be in nano seconds.
61 *
62 * @return Seconds in the object, in string.
63 */
64 public String getSeconds() {
65 return formatSecs(fValue);
66 }
67
68 /**
69 * Get the nanosecond part in timestamp.<p>
70 *
71 * Note : We do not use scale and assumes contents to be in nanoseconds.
72 *
73 * @return Seconds in the object, in string.
74 */
75 public String getNanoSeconds() {
76 return formatNs(fValue);
77 }
78
79 /*
80 * Use the exponent to format the second in the correct format.
81 */
82 private String formatSecs(final long time) {
83 final long sec = (long) (time * 1E-9);
84 return String.valueOf(sec);
85 }
86
87 /*
88 * Obtains the remainder fraction on unit Seconds of the entered value in
89 * nanoseconds. e.g. input: 1241207054171080214 ns.
90 * The number of fraction seconds can be obtained by removing the last 9 digits:
91 * In 1241207054, the fractional portion of seconds, expressed in ns is: 171080214
92 */
93 private String formatNs(long time) {
94 final boolean neg = time < 0;
95 if (neg)
96 time = -time;
97 // The following approach could be used although performance
98 // decreases in half.
99 // String strVal = String.format("%09d", time);
100 // String tmp = strVal.substring(strVal.length() - 9)
101 final StringBuffer temp = new StringBuffer();
102 long ns = time;
103 ns %= 1000000000;
104 if (ns < 10)
105 temp.append("00000000"); //$NON-NLS-1$
106 else if (ns < 100)
107 temp.append("0000000"); //$NON-NLS-1$
108 else if (ns < 1000)
109 temp.append("000000"); //$NON-NLS-1$
110 else if (ns < 10000)
111 temp.append("00000"); //$NON-NLS-1$
112 else if (ns < 100000)
113 temp.append("0000"); //$NON-NLS-1$
114 else if (ns < 1000000)
115 temp.append("000"); //$NON-NLS-1$
116 else if (ns < 10000000)
117 temp.append("00"); //$NON-NLS-1$
118 else if (ns < 100000000)
119 temp.append("0"); //$NON-NLS-1$
120
121 temp.append(ns);
122 return temp.toString();
123 }
124
125
126 /**
127 * toString() method.
128 *
129 * @return timestamp, as string
130 */
131 @Override
132 @SuppressWarnings("nls")
133 public String toString() {
134
135 long value = fValue;
136 if (fValue < 0)
137 value = -fValue;
138
139 final StringBuilder sb = new StringBuilder(String.valueOf(value));
140
141 // Prepend the correct number of "0" so we can insert a "." at the right location
142 final int nbZeroes = (-fScale) - sb.length() + 1;
143 for (int i = 0; i < nbZeroes; i++)
144 sb.insert(i, "0");
145 sb.insert(sb.length() + fScale, ".");
146
147 // Prepend "-" if negative
148 if (fValue < 0)
149 sb.insert(0, "-");
150
151 return sb.toString();
152 }
153
154 @Override
155 public LttngTimestamp clone() {
156 return (LttngTimestamp) super.clone();
157 }
158
159 /**
160 * Compute the delta between two timestamps (adjusted to scale of current timestamp).
161 *
162 * @param reference the reference timestamp to synchronize with
163 * @return the delta timestamp
164 * @throws ArithmeticException
165 */
166 @Override
167 public LttngTimestamp getDelta(final ITmfTimestamp other) {
168 final TmfTimestamp delta = (TmfTimestamp) super.getDelta(other);
169 return new LttngTimestamp(delta);
170 }
171 }
This page took 0.034647 seconds and 4 git commands to generate.