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
.lttng
.event
;
15 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
18 * <b><u>LttngTimestamp</u></b><p>
20 * Lttng specific implementation of the TmfTimestamp.<p>
22 * The Lttng implementation is the same as the basic Tmf Implementation but allow construction with a TmfTimestamp or a long.
24 public class LttngTimestamp
extends TmfTimestamp
{
26 // Required by Serializable
27 private static final long serialVersionUID
= -7016853105162491273L;
30 * Default Constructor.<p>
33 public LttngTimestamp() {
34 super(Long
.MIN_VALUE
, (byte) -9);
38 * Constructor with parameters.<p>
40 * @param newEventTime Time as long, unit expected to be nanoseconds
42 public LttngTimestamp(long newEventTime
) {
43 super(newEventTime
, (byte) -9);
47 * Copy Constructor.<p>
49 * @param oldEventTime The timestamp object we want to copy from
51 public LttngTimestamp(TmfTimestamp oldEventTime
) {
52 this(oldEventTime
.getValue());
56 public long getValue() {
60 public void setValue(long newValue
) {
65 * Get the second part in timestamp.<p>
67 * Note : We do not use scale and assumes contents to be in nano seconds.
69 * @return Seconds in the object, in string.
71 public String
getSeconds() {
72 return formatSecs(fValue
);
76 * Get the nanosecond part in timestamp.<p>
78 * Note : We do not use scale and assumes contents to be in nanoseconds.
80 * @return Seconds in the object, in string.
82 public String
getNanoSeconds() {
83 return formatNs(fValue
);
87 * Use the exponent to format the second in the correct format.
89 private String
formatSecs(long time
) {
90 long sec
= (long) (time
* 1E-9);
91 return String
.valueOf(sec
);
95 * Obtains the remainder fraction on unit Seconds of the entered value in
96 * nanoseconds. e.g. input: 1241207054171080214 ns.
97 * The number of fraction seconds can be obtained by removing the last 9 digits:
98 * In 1241207054, the fractional portion of seconds, expressed in ns is: 171080214
100 private String
formatNs(long time
) {
101 boolean neg
= time
< 0;
105 // The following approach could be used although performance
106 // decreases in half.
107 // String strVal = String.format("%09d", time);
108 // String tmp = strVal.substring(strVal.length() - 9)
109 StringBuffer temp
= new StringBuffer();
113 temp
.append("00000000"); //$NON-NLS-1$
114 } else if (ns
< 100) {
115 temp
.append("0000000"); //$NON-NLS-1$
116 } else if (ns
< 1000) {
117 temp
.append("000000"); //$NON-NLS-1$
118 } else if (ns
< 10000) {
119 temp
.append("00000"); //$NON-NLS-1$
120 } else if (ns
< 100000) {
121 temp
.append("0000"); //$NON-NLS-1$
122 } else if (ns
< 1000000) {
123 temp
.append("000"); //$NON-NLS-1$
124 } else if (ns
< 10000000) {
125 temp
.append("00"); //$NON-NLS-1$
126 } else if (ns
< 100000000) {
127 temp
.append("0"); //$NON-NLS-1$
131 return temp
.toString();
138 * @return timestamp, as string
141 @SuppressWarnings("nls")
142 public String
toString() {
149 StringBuilder sb
= new StringBuilder(String
.valueOf(value
));
151 // Prepend the correct number of "0" so we can insert a "." at the right location
152 int nbZeroes
= (-fScale
) - sb
.length() + 1;
153 for (int i
= 0; i
< nbZeroes
; i
++) {
156 sb
.insert(sb
.length() + fScale
, ".");
158 // Prepend "-" if negative
163 return sb
.toString();
167 public LttngTimestamp
clone() {
168 return (LttngTimestamp
) super.clone();
172 * Compute the delta between two timestamps (adjusted to scale of current timestamp).
174 * @param reference the reference timestamp to synchronize with
175 * @return the delta timestamp
176 * @throws ArithmeticException
179 public LttngTimestamp
getDelta(TmfTimestamp other
) throws ArithmeticException
{
180 TmfTimestamp delta
= super.getDelta(other
);
181 return new LttngTimestamp(delta
);