1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
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
10 * William Bourque (wbourque@gmail.com) - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.lttng
.core
.event
;
15 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
16 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
19 * <b><u>LttngTimestamp</u></b><p>
21 * Lttng specific implementation of the TmfTimestamp.<p>
23 * The Lttng implementation is the same as the basic Tmf Implementation but allow construction with a TmfTimestamp or a long.
25 public class LttngTimestamp
extends TmfTimestamp
{
28 * Default Constructor.<p>
31 public LttngTimestamp() {
36 * Constructor with parameters.<p>
38 * @param newEventTime Time as long, unit expected to be nanoseconds
40 public LttngTimestamp(final long newEventTime
) {
41 super(newEventTime
, -9, 0);
45 * Copy Constructor.<p>
47 * @param oldEventTime The timestamp object we want to copy from
49 public LttngTimestamp(final ITmfTimestamp oldEventTime
) {
50 this(oldEventTime
.getValue());
53 public void setValue(final long newValue
) {
54 setValue(newValue
, -9, 0);
58 * Get the second part in timestamp.<p>
60 * Note : We do not use scale and assumes contents to be in nano seconds.
62 * @return Seconds in the object, in string.
64 public String
getSeconds() {
65 return formatSecs(getValue());
69 * Get the nanosecond part in timestamp.<p>
71 * Note : We do not use scale and assumes contents to be in nanoseconds.
73 * @return Seconds in the object, in string.
75 public String
getNanoSeconds() {
76 return formatNs(getValue());
80 * Use the exponent to format the second in the correct format.
82 private String
formatSecs(final long time
) {
83 final long sec
= (long) (time
* 1E-9);
84 return String
.valueOf(sec
);
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
93 private String
formatNs(long time
) {
94 final boolean neg
= time
< 0;
97 // The following approach could be used although performance
99 // String strVal = String.format("%09d", time);
100 // String tmp = strVal.substring(strVal.length() - 9)
101 final StringBuffer temp
= new StringBuffer();
105 temp
.append("00000000"); //$NON-NLS-1$
107 temp
.append("0000000"); //$NON-NLS-1$
109 temp
.append("000000"); //$NON-NLS-1$
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$
122 return temp
.toString();
129 * @return timestamp, as string
132 @SuppressWarnings("nls")
133 public String
toString() {
135 int scale
= getScale();
136 long value
= getValue();
140 final StringBuilder sb
= new StringBuilder(String
.valueOf(value
));
142 // Prepend the correct number of "0" so we can insert a "." at the right location
143 final int nbZeroes
= (-scale
) - sb
.length() + 1;
144 for (int i
= 0; i
< nbZeroes
; i
++)
146 sb
.insert(sb
.length() + scale
, ".");
148 // Prepend "-" if negative
152 return sb
.toString();
156 public LttngTimestamp
clone() {
157 return (LttngTimestamp
) super.clone();
161 * Compute the delta between two timestamps (adjusted to scale of current timestamp).
163 * @param reference the reference timestamp to synchronize with
164 * @return the delta timestamp
165 * @throws ArithmeticException
168 public LttngTimestamp
getDelta(final ITmfTimestamp other
) {
169 final TmfTimestamp delta
= (TmfTimestamp
) super.getDelta(other
);
170 return new LttngTimestamp(delta
);