tmf: Allow waiting for a state system's completion
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / event / TmfTimestampFormat.java
CommitLineData
f8177ba2
FC
1/*******************************************************************************
2 * Copyright (c) 2012 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 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.tmf.core.event;
14
15import java.text.DecimalFormat;
16import java.text.ParseException;
17import java.text.SimpleDateFormat;
18import java.util.ArrayList;
19import java.util.Calendar;
20import java.util.Date;
21import java.util.List;
22import java.util.TimeZone;
23import java.util.regex.Matcher;
24import java.util.regex.Pattern;
25
26import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
27import org.eclipse.linuxtools.tmf.core.signal.TmfTimestampFormatUpdateSignal;
28
29/**
30 * A formatting and parsing facility that can handle timestamps that span the
31 * epoch with a precision down to the nanosecond. It can be understood as a
32 * simplified and more constrained version of SimpleDateFormat as it limits the
33 * number of allowed pattern characters and the acceptable timestamp formats.
34 * <p>
35 * The timestamp representation is broken down into a number of optional
36 * components that can be assembled into a fairly simple way.
37 *
38 * <h4>Date Pattern</h4>
39 * <blockquote>
40 * <table border=0 cellspacing=3 cellpadding=0 >
41 * <tr bgcolor="#ccccff">
42 * <th align=left>Format
43 * <th align=left>Description
44 * <th align=left>Value Range
45 * <th align=left>Example
46 * <tr>
47 * <td><code>yyyy</code>
48 * <td>Year
49 * <td><code>1970-...</code>
50 * <td><code>2012</code>
51 * <tr bgcolor="#eeeeff">
52 * <td><code>MM</code>
53 * <td>Month in year
54 * <td><code>01-12</code>
55 * <td><code>09</code>
56 * <tr>
57 * <td><code>dd</code>
58 * <td>Day in month
59 * <td><code>01-31</code>
60 * <td><code>22</code>
61 * </table>
62 * </blockquote>
63 *
64 * <h4>Time Pattern</h4>
65 * <blockquote>
66 * <table border=0 cellspacing=3 cellpadding=0 >
67 * <tr bgcolor="#ccccff">
68 * <th align=left>Format
69 * <th align=left>Description
70 * <th align=left>Value Range
71 * <th align=left>Example
72 * <tr>
73 * <td><code>HH</code>
74 * <td>Hour in day
75 * <td><code>00-23</code>
76 * <td><code>07</code>
77 * <tr bgcolor="#eeeeff">
78 * <td><code>mm</code>
79 * <td>Minute in hour
80 * <td><code>00-59</code>
81 * <td><code>35</code>
82 * <tr>
83 * <td><code>ss</code>
84 * <td>Second in minute
85 * <td><code>00-59</code>
86 * <td><code>41</code>
87 * <tr bgcolor="#eeeeff">
88 * <td><code>T</code>
89 * <td>The seconds since the epoch
90 * <td><code>00-...</code>
91 * <td><code>1332170682</code>
92 * </table>
93 * </blockquote>
94 *
95 * <h4>Sub-Seconds Pattern</h4>
96 * <blockquote>
97 * <table border=0 cellspacing=3 cellpadding=0 >
98 * <tr bgcolor="#ccccff">
99 * <th align=left>Format
100 * <th align=left>Description
101 * <th align=left>Value Range
102 * <th align=left>Example
103 * <tr>
104 * <td><code>SSS</code>
105 * <td>Millisecond in second
106 * <td><code>000-999</code>
107 * <td><code>123</code>
108 * <tr bgcolor="#eeeeff">
109 * <td><code>CCC</code>
110 * <td>Microseconds in ms
111 * <td><code>000-999</code>
112 * <td><code>456</code>
113 * <tr>
114 * <td><code>NNN</code>
115 * <td>Nanosecond in &#181s
116 * <td><code>000-999</code>
117 * <td><code>789</code>
118 * </table>
119 * </blockquote>
120 *
121 * <strong>Note: </strong>If "T" is used, no other Date or Time pattern
122 * can be used. Also, "T" should be used for time intervals.
123 * <p>
124 * <strong>Note: </strong>Each sub-field can be separated by a single,
125 * optional character delimiter. However, the between Date/Time and the
126 * Sub-seconds pattern is mandatory (if there is a fractional part) and
127 * has to be separated from Date/time by "." (period).
128 * <p>
129 * The recognized delimiters are:
130 * <ul>
131 * <li>Space ("<code> </code>")
132 * <li>Period (<code>".</code>")
133 * <li>Comma ("<code>,</code>")
134 * <li>Dash ("<code>-</code>")
135 * <li>Underline ("<code>_</code>")
136 * <li>Colon ("<code>:</code>")
137 * <li>Semicolon ("<code>;</code>")
138 * <li>Slash ("<code>/</code>")
139 * <li>Double-quote ("<code>"</code>")
140 * </ul>
141 *
142 * <h4>Examples</h4>
143 * The following examples show how timestamp patterns are interpreted in
144 * the U.S. locale. The given timestamp is 1332170682539677389L, the number
145 * of nanoseconds since 1970/01/01.
146 *
147 * <blockquote>
148 * <table border=0 cellspacing=3 cellpadding=0>
149 * <tr bgcolor="#ccccff">
150 * <th align=left>Date and Time Pattern
151 * <th align=left>Result
152 * <tr>
153 * <td><code>"yyyy-MM-dd HH:mm:ss.SSS.CCC.NNN"</code>
154 * <td><code>2012-03-19 11:24:42.539.677.389</code>
155 * <tr bgcolor="#eeeeff">
156 * <td><code>"yyyy-MM-dd HH:mm:ss.SSS.CCC"</code>
157 * <td><code>2012-03-19 11:24:42.539.677</code>
158 * <tr>
159 * <td><code>"yyyy-D HH:mm:ss.SSS.CCC"</code>
160 * <td><code>2012-79 11:24:42.539.677</code>
161 * <tr bgcolor="#eeeeff">
162 * <td><code>"ss.SSSCCCNNN"</code>
163 * <td><code>42.539677389</code>
164 * <tr>
165 * <td><code>"T.SSS CCC NNN"</code>
166 * <td><code>1332170682.539 677 389</code>
167 * <tr bgcolor="#eeeeff">
168 * <td><code>"T"</code>
169 * <td><code>1332170682</code>
170 * </table>
171 * </blockquote>
172 * <p>
173 * @version 1.0
174 * @since 2.0
175 * @author Francois Chouinard
176 */
177public class TmfTimestampFormat extends SimpleDateFormat {
178
179 // ------------------------------------------------------------------------
180 // Constants
181 // ------------------------------------------------------------------------
182
183 /**
184 * This class' serialization ID
185 */
186 private static final long serialVersionUID = 2835829763122454020L;
187
188 /**
189 * The default timestamp pattern
190 */
191 public static final String DEFAULT_TIME_PATTERN = "HH:mm:ss.SSS_CCC_NNN"; //$NON-NLS-1$
192
193 /**
194 * The LTTng 0.x legacy timestamp format
195 */
196 public static final String DEFAULT_INTERVAL_PATTERN = "TTT.SSS_CCC_NNN"; //$NON-NLS-1$
197
198 // Fractions of seconds supported patterns
199 private static final String DOT_RE = "\\."; //$NON-NLS-1$
200 private static final String SEP_RE = "[ \\.,-_:;/\\\"]?"; //$NON-NLS-1$
201 private static final String DGTS_3_RE = "(\\d{3})"; //$NON-NLS-1$
202 private static final String DGTS_13_RE = "(\\d{1,3})"; //$NON-NLS-1$
203
204 private static final String MILLISEC_RE = DOT_RE + DGTS_13_RE;
205 private static final String MICROSEC_RE = DOT_RE + DGTS_3_RE + SEP_RE + DGTS_13_RE;
206 private static final String NANOSEC_RE = DOT_RE + DGTS_3_RE + SEP_RE + DGTS_3_RE + SEP_RE + DGTS_13_RE;
207
208 private static final Pattern MILLISEC_PAT = Pattern.compile(MILLISEC_RE);
209 private static final Pattern MICROSEC_PAT = Pattern.compile(MICROSEC_RE);
210 private static final Pattern NANOSEC_PAT = Pattern.compile(NANOSEC_RE);
211
212 // ------------------------------------------------------------------------
213 // Attributes
214 // ------------------------------------------------------------------------
215
216 // The default timestamp pattern
217 private static String fDefaultTimePattern = null;
218 private static TmfTimestampFormat fDefaultTimeFormat = null;
219
220 // The default time interval format
221 private static String fDefaultIntervalPattern = null;
222 private static TmfTimestampFormat fDefaultIntervalFormat = null;
223
224 // The timestamp pattern
225 private String fPattern;
226
227 // The timestamp pattern
228 private List<String> fSupplPatterns = new ArrayList<String>();
229
230 /**
231 * The supplementary pattern letters. Can be redefined by sub-classes
232 * to either override existing letters or augment the letter set.
233 * If so, the format() method must provide the (re-)implementation of the
234 * pattern.
235 */
236 protected String fSupplPatternLetters = "TSCN"; //$NON-NLS-1$
237
238 /**
239 * The bracketing symbols used to mitigate the risk of a format string
240 * that contains escaped sequences that would conflict with our format
241 * extension.
242 */
243 protected String fOpenBracket = "[&"; //$NON-NLS-1$
244 protected String fCloseBracket = "&]"; //$NON-NLS-1$
245
246 // ------------------------------------------------------------------------
247 // Constructors
248 // ------------------------------------------------------------------------
249
250 /**
251 * The default constructor (uses the default pattern)
252 */
253 public TmfTimestampFormat() {
254 this(fDefaultTimePattern);
255 }
256
257 /**
258 * The normal constructor
259 *
260 * @param pattern the format pattern
261 */
262 public TmfTimestampFormat(String pattern) {
263 calendar.setTimeZone(TimeZone.getTimeZone("UTC")); //$NON-NLS-1$
264 applyPattern(pattern);
265 }
266
267 /**
268 * The copy constructor
269 *
270 * @param other the other format pattern
271 */
272 public TmfTimestampFormat(TmfTimestampFormat other) {
273 this(other.fPattern);
274 }
275
276 // ------------------------------------------------------------------------
277 // Getters/setters
278 // ------------------------------------------------------------------------
279
280 /**
281 * @param pattern the new default time pattern
282 */
283 public static void setDefaultTimeFormat(final String pattern) {
284 fDefaultTimePattern = pattern;
285 fDefaultTimeFormat = new TmfTimestampFormat(fDefaultTimePattern);
286 TmfSignalManager.dispatchSignal(new TmfTimestampFormatUpdateSignal(null));
287 }
288
289 /**
290 * @return the default time format pattern
291 */
292 public static TmfTimestampFormat getDefaulTimeFormat() {
293 if (fDefaultTimeFormat == null) {
294 fDefaultTimeFormat = new TmfTimestampFormat(DEFAULT_TIME_PATTERN);
295 }
296 return fDefaultTimeFormat;
297 }
298
299 /**
300 * @param pattern the new default interval pattern
301 */
302 public static void setDefaultIntervalFormat(final String pattern) {
303 fDefaultIntervalPattern = pattern;
304 fDefaultIntervalFormat = new TmfTimestampFormat(fDefaultIntervalPattern);
305 TmfSignalManager.dispatchSignal(new TmfTimestampFormatUpdateSignal(null));
306 }
307
308 /**
309 * @return the default interval format pattern
310 */
311 public static TmfTimestampFormat getDefaulIntervalFormat() {
312 if (fDefaultIntervalFormat == null) {
313 fDefaultIntervalFormat = new TmfTimestampFormat(DEFAULT_INTERVAL_PATTERN);
314 }
315 return fDefaultIntervalFormat;
316 }
317
318 /* (non-Javadoc)
319 * @see java.text.SimpleDateFormat#applyPattern(java.lang.String)
320 */
321 @Override
322 public void applyPattern(String pattern) {
323 fPattern = pattern;
324 String quotedPattern = quoteSpecificTags(pattern);
325 super.applyPattern(quotedPattern);
326 }
327
328 /* (non-Javadoc)
329 * @see java.text.SimpleDateFormat#toPattern()
330 */
331 @Override
332 public String toPattern() {
333 return fPattern;
334 }
335
336 // ------------------------------------------------------------------------
337 // Operations
338 // ------------------------------------------------------------------------
339
340 /**
341 * Format the timestamp according to its pattern.
342 *
343 * @param value the timestamp value to format (in ns)
344 * @return the formatted timestamp
345 */
346 public String format(long value) {
347
348 // Split the timestamp value into its sub-components
349 long sec = value / 1000000000; // seconds
350 long ms = value % 1000000000 / 1000000; // milliseconds
351 long cs = value % 1000000 / 1000; // microseconds
352 long ns = value % 1000; // nanoseconds
353
354 // Let the base class fill the stuff it knows about
355 StringBuffer result = new StringBuffer(super.format(sec * 1000 + ms));
356
357 // In the case where there is no separation between 2 supplementary
358 // fields, the pattern will have the form "..'[pat-1]''[pat-2]'.." and
359 // the base class format() will interpret the 2 adjacent quotes as a
360 // wanted character in the result string as ("..[pat-1]'[pat-2]..").
361 // Remove these extra quotes before filling the supplementary fields.
362 int loc = result.indexOf(fCloseBracket + "'" + fOpenBracket); //$NON-NLS-1$
363 while (loc != -1) {
364 result.deleteCharAt(loc + fCloseBracket.length());
365 loc = result.indexOf(fCloseBracket + "'" + fOpenBracket); //$NON-NLS-1$
366 }
367
368 // Fill in our extensions
369 for (String pattern : fSupplPatterns) {
370 int length = pattern.length();
371
372 // Prepare the format buffer
373 StringBuffer fmt = new StringBuffer(length);
374 for (int i = 0; i < length; i++) {
375 fmt.append("0"); //$NON-NLS-1$
376 }
377 DecimalFormat dfmt = new DecimalFormat(fmt.toString());
378 String fmtVal = ""; //$NON-NLS-1$;
379
380 // Format the proper value as per the pattern
381 switch (pattern.charAt(0)) {
382 case 'T':
383 fmtVal = dfmt.format(sec);
384 break;
385 case 'S':
386 fmtVal = dfmt.format(ms);
387 break;
388 case 'C':
389 fmtVal = dfmt.format(cs);
390 break;
391 case 'N':
392 fmtVal = dfmt.format(ns);
393 break;
394 default:
395 break;
396 }
397
398 // Substitute the placeholder with the formatted value
399 String ph = new StringBuffer(fOpenBracket + pattern + fCloseBracket).toString();
400 loc = result.indexOf(ph);
401 result.replace(loc, loc + length + fOpenBracket.length() + fCloseBracket.length(), fmtVal);
402 }
403
404 return result.toString();
405 }
406
407 /**
408 * Parse a string according to the format pattern
409 *
410 * @param string the source string
411 * @param ref the reference (base) time
412 * @return the parsed value
413 * @throws ParseException if the string has an invalid format
414 */
415 public long parseValue(final String string, final long ref) throws ParseException {
416
417 // Trivial case
418 if (string == null || string.length() == 0) {
419 return 0;
420 }
421
422 // The timestamp sub-components
423 long seconds = -1;
424 long millisec = 0;
425 long microsec = 0;
426 long nanosec = 0;
427
428 // Since we are processing the fractional part, substitute it with
429 // its pattern so the base parser doesn't complain
430 StringBuilder sb = new StringBuilder(string);
431 int dot = string.indexOf('.');
432 if (dot == -1) {
433 sb.append('.');
434 dot = string.length();
435 }
436 sb = new StringBuilder(string.substring(0, dot));
437 String basePattern = super.toPattern();
438 int dot2 = basePattern.indexOf('.');
439 if (dot2 != -1) {
440 sb.append(basePattern.substring(dot2));
441 }
442
443 // Fill in our extensions
444 for (String pattern : fSupplPatterns) {
445 String pat = fOpenBracket + pattern + fCloseBracket;
446 Matcher matcher;
447
448 // Extract the substring corresponding to the extra pattern letters
449 // and replace with the pattern so the base parser can do its job.
450 switch (pattern.charAt(0)) {
451 case 'T':
452 // Remove everything up to the first "." and compute the
453 // number of seconds since the epoch. If there is no period,
454 // assume an integer value and return immediately
455 if (dot < 1) {
456 return new DecimalFormat("0").parse(string).longValue() * 1000000000; //$NON-NLS-1$
457 }
458 seconds = new DecimalFormat("0").parse(string.substring(0, dot)).longValue(); //$NON-NLS-1$
459 sb.delete(0, dot);
460 sb.insert(0, pat);
461 break;
462 case 'S':
463 matcher = MILLISEC_PAT.matcher(string.substring(dot));
464 if (matcher.find()) {
465 millisec = new Long(matcher.group(1));
466 for (int l = matcher.group(1).length(); l < 3; l++) {
467 millisec *= 10;
468 }
469 }
470 stripQuotes(sb, pattern);
471 break;
472 case 'C':
473 matcher = MICROSEC_PAT.matcher(string.substring(dot));
474 if (matcher.find()) {
475 microsec = new Long(matcher.group(2));
476 for (int l = matcher.group(2).length(); l < 3; l++) {
477 microsec *= 10;
478 }
479 }
480 stripQuotes(sb, pattern);
481 break;
482 case 'N':
483 matcher = NANOSEC_PAT.matcher(string.substring(dot));
484 if (matcher.find()) {
485 nanosec = new Long(matcher.group(3));
486 for (int l = matcher.group(3).length(); l < 3; l++) {
487 nanosec *= 10;
488 }
489 }
490 stripQuotes(sb, pattern);
491 break;
492 default:
493 break;
494 }
495 }
496
497 // If there was no "T" (thus not an interval), parse as a date
498 if (seconds == -1) {
499 Date baseDate = super.parse(sb.toString());
500
501 Calendar refTime = Calendar.getInstance(TimeZone.getTimeZone("UTC")); //$NON-NLS-1$
502 refTime.setTimeInMillis(ref / 1000000);
503 Calendar newTime = Calendar.getInstance(TimeZone.getTimeZone("UTC")); //$NON-NLS-1$
504 newTime.setTimeInMillis(baseDate.getTime());
505
506 int[] fields = new int[] { Calendar.YEAR, Calendar.DAY_OF_YEAR, Calendar.MONTH, Calendar.DATE, Calendar.HOUR, Calendar.MINUTE, Calendar.SECOND };
507 for (int field : fields) {
508 int value = newTime.get(field);
509 // Do some adjustments...
510 if (field == Calendar.YEAR) {
511 value -= 1970;
512 } else if (field == Calendar.DATE || field == Calendar.DAY_OF_YEAR) {
513 value -= 1;
514 }
515 // ... and fill-in the empty fields
516 if (value == 0) {
517 newTime.set(field, refTime.get(field));
518 } else if (field == Calendar.DAY_OF_YEAR) {
519 newTime.set(field, value);
520 } else {
521 break; // Get out as soon as we have a significant value
522 }
523 }
524 seconds = newTime.getTimeInMillis() / 1000;
525 }
526
527 // Compute the value in ns
528 return seconds * 1000000000 + millisec * 1000000 + microsec * 1000 + nanosec;
529 }
530
531 /**
532 * Parse a string according to the format pattern
533 *
534 * @param string the source string
535 * @return the parsed value
536 * @throws ParseException if the string has an invalid format
537 */
538 public long parseValue(final String string) throws ParseException {
539 long result = parseValue(string, 0);
540 return result;
541
542 }
543
544 // ------------------------------------------------------------------------
545 // Helper functions
546 // ------------------------------------------------------------------------
547
548 /**
549 * Copy the pattern but quote (bracket with "[&" and "&]") the
550 * TmfTimestampFormat specific tags so these fields are treated as
551 * comments by the base class.
552 *
553 * It also keeps track of the corresponding quoted fields so they can be
554 * properly populated later on (by format()).
555 *
556 * @param pattern the 'extended' pattern
557 * @return the quoted and bracketed pattern
558 */
559 private String quoteSpecificTags(final String pattern) {
560
561 StringBuffer result = new StringBuffer();
562
563 int length = pattern.length();
564 boolean inQuote = false;
565
566 for (int i = 0; i < length; i++) {
567 char c = pattern.charAt(i);
568 result.append(c);
569 if (c == '\'') {
570 // '' is treated as a single quote regardless of being
571 // in a quoted section.
572 if ((i + 1) < length) {
573 c = pattern.charAt(i + 1);
574 if (c == '\'') {
575 i++;
576 result.append(c);
577 continue;
578 }
579 }
580 inQuote = !inQuote;
581 continue;
582 }
583 if (!inQuote) {
584 if (fSupplPatternLetters.indexOf(c) != -1) {
585 StringBuilder pat = new StringBuilder();
586 pat.append(c);
587 result.insert(result.length() - 1, "'" + fOpenBracket); //$NON-NLS-1$
588 while ((i + 1) < length && pattern.charAt(i + 1) == c) {
589 result.append(c);
590 pat.append(c);
591 i++;
592 }
593 result.append(fCloseBracket + "'"); //$NON-NLS-1$
594 fSupplPatterns.add(pat.toString());
595 }
596 }
597 }
598 return result.toString();
599 }
600
601 /**
602 * Remove the quotes from the pattern
603 *
604 * @param sb
605 * @param pattern
606 */
607 private void stripQuotes(StringBuilder sb, String pattern) {
608 String pt = "'" + fOpenBracket + pattern + fCloseBracket + "'"; //$NON-NLS-1$//$NON-NLS-2$
609 int l = sb.indexOf(pt);
610 if (l != -1) {
611 sb.delete(l + pt.length() - 1, l + pt.length());
612 sb.delete(l, l + 1);
613 }
614 }
615
616}
This page took 0.046027 seconds and 5 git commands to generate.