Commit | Line | Data |
---|---|---|
5d10d135 ASL |
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.lttng.event; | |
14 | ||
15 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; | |
16 | ||
17 | /** | |
146a887c FC |
18 | * <b><u>LttngTimestamp</u></b> |
19 | * <p> | |
20 | * Lttng specific implementation of the TmfTimestamp | |
21 | * <p> | |
22 | * The Lttng implementation is the same as the basic Tmf Implementation but allow construction with a TmfTimestamp and a long | |
5d10d135 ASL |
23 | */ |
24 | public class LttngTimestamp extends TmfTimestamp { | |
25 | ||
5d10d135 | 26 | /** |
146a887c FC |
27 | * Copy Constructor<br> |
28 | * <br> | |
29 | * Note : this constructor require a TmfTimestamp instead of a LttngTimestamp to save us some casts | |
5d10d135 | 30 | * |
146a887c | 31 | * @param newEventTime The TmfTimestamp object we want to copy |
5d10d135 | 32 | */ |
146a887c FC |
33 | public LttngTimestamp(TmfTimestamp newEventTime) { |
34 | super(newEventTime); | |
35 | } | |
36 | ||
37 | /** | |
38 | * Constructor with parameters | |
5d10d135 | 39 | * |
146a887c | 40 | * @param newEventTime JniTime as long, unit expected to be nanoseconds. |
5d10d135 ASL |
41 | */ |
42 | public LttngTimestamp(long newEventTime) { | |
43 | super(newEventTime, (byte) -9); | |
44 | } | |
45 | ||
46 | /** | |
146a887c | 47 | * toString() method. |
5d10d135 | 48 | * |
146a887c | 49 | * @return String Attributes of this object. |
5d10d135 | 50 | */ |
146a887c FC |
51 | public String toString() { |
52 | // String returnData = ""; | |
53 | // | |
54 | // returnData += "[lttng_Timestamp: " + getValue() / Jni_C_Common.NANO; | |
55 | // returnData += "." + getValue() % Jni_C_Common.NANO; | |
56 | // returnData += " ]"; | |
57 | // | |
58 | // return returnData; | |
59 | ||
60 | // If we are dealing with units of seconds (or higher), | |
61 | // use the plain formatter | |
62 | if (fScale >= 0) { | |
63 | Double value = fValue * Math.pow(10, fScale); | |
64 | return value.toString(); | |
65 | } | |
66 | ||
67 | // Define a format string | |
68 | String format = String.format("%%1d.%%0%dd", -fScale); | |
69 | ||
70 | // And format the timestamp value | |
71 | double scale = Math.pow(10, fScale); | |
72 | long seconds = (long) (fValue * scale); | |
73 | long fracts = fValue - (long) ((double) seconds / scale); | |
74 | String result = String.format(format, seconds, fracts); | |
75 | ||
76 | return result; | |
5d10d135 | 77 | } |
146a887c | 78 | |
5d10d135 | 79 | /** |
146a887c | 80 | * This method does not use scale and assumes contents to be in nano seconds |
5d10d135 | 81 | * |
146a887c | 82 | * @return String Attributes of this object. |
5d10d135 ASL |
83 | */ |
84 | public String getSeconds() { | |
85 | return formatSecs(fValue); | |
86 | } | |
87 | ||
88 | /** | |
146a887c | 89 | * This method does not use scale and assumes contents to be in nano seconds |
5d10d135 | 90 | * |
146a887c | 91 | * @return String Attributes of this object. |
5d10d135 ASL |
92 | */ |
93 | public String getNanoSeconds() { | |
94 | return formatNs(fValue); | |
95 | } | |
146a887c FC |
96 | |
97 | /** | |
98 | * @param time | |
99 | * @return | |
5d10d135 ASL |
100 | */ |
101 | private String formatSecs(long time) { | |
102 | long sec = (long) (time * 1E-9); | |
103 | return String.valueOf(sec); | |
104 | } | |
105 | ||
146a887c | 106 | /** |
5d10d135 | 107 | * Obtains the remainder fraction on unit Seconds of the entered value in |
146a887c FC |
108 | * nanoseconds. e.g. input: 1241207054171080214 ns The number of fraction |
109 | * seconds can be obtained by removing the last 9 digits: 1241207054 the | |
110 | * fractional portion of seconds, expressed in ns is: 171080214 | |
111 | * | |
112 | * @param time | |
113 | * @param res | |
114 | * @return | |
5d10d135 ASL |
115 | */ |
116 | private String formatNs(long time) { | |
117 | boolean neg = time < 0; | |
118 | if (neg) { | |
119 | time = -time; | |
120 | } | |
121 | // The following approach could be used although performance | |
122 | // decreases in half. | |
123 | // String strVal = String.format("%09d", time); | |
146a887c FC |
124 | // String tmp = strVal.substring(strVal.length() - 9); |
125 | ||
5d10d135 ASL |
126 | StringBuffer temp = new StringBuffer(); |
127 | long ns = time; | |
128 | ns %= 1000000000; | |
129 | if (ns < 10) { | |
130 | temp.append("00000000"); | |
131 | } else if (ns < 100) { | |
132 | temp.append("0000000"); | |
133 | } else if (ns < 1000) { | |
134 | temp.append("000000"); | |
135 | } else if (ns < 10000) { | |
136 | temp.append("00000"); | |
137 | } else if (ns < 100000) { | |
138 | temp.append("0000"); | |
139 | } else if (ns < 1000000) { | |
140 | temp.append("000"); | |
141 | } else if (ns < 10000000) { | |
142 | temp.append("00"); | |
143 | } else if (ns < 100000000) { | |
144 | temp.append("0"); | |
145 | } | |
146 | ||
147 | temp.append(ns); | |
148 | return temp.toString(); | |
149 | } | |
150 | ||
5d10d135 | 151 | } |