1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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 * Francois Chouinard - Initial API and implementation
11 * Marc-Andre Laperle - Add time zone preference
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.timestamp
;
16 import java
.text
.DecimalFormat
;
17 import java
.text
.ParseException
;
18 import java
.text
.SimpleDateFormat
;
19 import java
.util
.ArrayList
;
20 import java
.util
.Calendar
;
21 import java
.util
.Date
;
22 import java
.util
.List
;
23 import java
.util
.TimeZone
;
24 import java
.util
.regex
.Matcher
;
25 import java
.util
.regex
.Pattern
;
28 * A formatting and parsing facility that can handle timestamps that span the
29 * epoch with a precision down to the nanosecond. It can be understood as a
30 * simplified and more constrained version of SimpleDateFormat as it limits the
31 * number of allowed pattern characters and the acceptable timestamp formats.
33 * The timestamp representation is broken down into a number of optional
34 * components that can be assembled into a fairly simple way.
36 * <h4>Date Pattern</h4>
38 * <table border=0 cellspacing=3 cellpadding=0 >
39 * <tr bgcolor="#ccccff">
40 * <th align=left>Format
41 * <th align=left>Description
42 * <th align=left>Value Range
43 * <th align=left>Example
45 * <td><code>yyyy</code>
47 * <td><code>1970-...</code>
48 * <td><code>2012</code>
49 * <tr bgcolor="#eeeeff">
52 * <td><code>01-12</code>
57 * <td><code>01-31</code>
62 * <h4>Time Pattern</h4>
64 * <table border=0 cellspacing=3 cellpadding=0 >
65 * <tr bgcolor="#ccccff">
66 * <th align=left>Format
67 * <th align=left>Description
68 * <th align=left>Value Range
69 * <th align=left>Example
73 * <td><code>00-23</code>
75 * <tr bgcolor="#eeeeff">
78 * <td><code>00-59</code>
82 * <td>Second in minute
83 * <td><code>00-59</code>
85 * <tr bgcolor="#eeeeff">
87 * <td>The seconds since the epoch
88 * <td><code>00-...</code>
89 * <td><code>1332170682</code>
93 * <h4>Sub-Seconds Pattern</h4>
95 * <table border=0 cellspacing=3 cellpadding=0 >
96 * <tr bgcolor="#ccccff">
97 * <th align=left>Format
98 * <th align=left>Description
99 * <th align=left>Value Range
100 * <th align=left>Example
102 * <td><code>SSS</code>
103 * <td>Millisecond in second
104 * <td><code>000-999</code>
105 * <td><code>123</code>
106 * <tr bgcolor="#eeeeff">
107 * <td><code>CCC</code>
108 * <td>Microseconds in ms
109 * <td><code>000-999</code>
110 * <td><code>456</code>
112 * <td><code>NNN</code>
113 * <td>Nanosecond in µs
114 * <td><code>000-999</code>
115 * <td><code>789</code>
119 * <strong>Note: </strong>If "T" is used, no other Date or Time pattern
120 * can be used. Also, "T" should be used for time intervals.
122 * <strong>Note: </strong>Each sub-field can be separated by a single,
123 * optional character delimiter. However, the between Date/Time and the
124 * Sub-seconds pattern is mandatory (if there is a fractional part) and
125 * has to be separated from Date/time by "." (period).
127 * The recognized delimiters are:
129 * <li>Space ("<code> </code>")
130 * <li>Period (<code>".</code>")
131 * <li>Comma ("<code>,</code>")
132 * <li>Dash ("<code>-</code>")
133 * <li>Underline ("<code>_</code>")
134 * <li>Colon ("<code>:</code>")
135 * <li>Semicolon ("<code>;</code>")
136 * <li>Slash ("<code>/</code>")
137 * <li>Double-quote ("<code>"</code>")
141 * The following examples show how timestamp patterns are interpreted in
142 * the U.S. locale. The given timestamp is 1332170682539677389L, the number
143 * of nanoseconds since 1970/01/01.
146 * <table border=0 cellspacing=3 cellpadding=0>
147 * <tr bgcolor="#ccccff">
148 * <th align=left>Date and Time Pattern
149 * <th align=left>Result
151 * <td><code>"yyyy-MM-dd HH:mm:ss.SSS.CCC.NNN"</code>
152 * <td><code>2012-03-19 11:24:42.539.677.389</code>
153 * <tr bgcolor="#eeeeff">
154 * <td><code>"yyyy-MM-dd HH:mm:ss.SSS.CCC"</code>
155 * <td><code>2012-03-19 11:24:42.539.677</code>
157 * <td><code>"yyyy-D HH:mm:ss.SSS.CCC"</code>
158 * <td><code>2012-79 11:24:42.539.677</code>
159 * <tr bgcolor="#eeeeff">
160 * <td><code>"ss.SSSCCCNNN"</code>
161 * <td><code>42.539677389</code>
163 * <td><code>"T.SSS CCC NNN"</code>
164 * <td><code>1332170682.539 677 389</code>
165 * <tr bgcolor="#eeeeff">
166 * <td><code>"T"</code>
167 * <td><code>1332170682</code>
173 * @author Francois Chouinard
175 public class TmfTimestampFormat
extends SimpleDateFormat
{
177 // ------------------------------------------------------------------------
179 // ------------------------------------------------------------------------
182 * This class' serialization ID
184 private static final long serialVersionUID
= 2835829763122454020L;
187 * The default timestamp pattern
189 public static final String DEFAULT_TIME_PATTERN
= "HH:mm:ss.SSS CCC NNN"; //$NON-NLS-1$
192 * The LTTng 0.x legacy timestamp format
194 public static final String DEFAULT_INTERVAL_PATTERN
= "TTT.SSS CCC NNN"; //$NON-NLS-1$
196 // Fractions of seconds supported patterns
197 private static final String DOT_RE
= "\\."; //$NON-NLS-1$
198 private static final String SEP_RE
= "[ \\.,-_:;/\\\"]?"; //$NON-NLS-1$
199 private static final String DGTS_3_RE
= "(\\d{3})"; //$NON-NLS-1$
200 private static final String DGTS_13_RE
= "(\\d{1,3})"; //$NON-NLS-1$
202 private static final String MILLISEC_RE
= DOT_RE
+ DGTS_13_RE
;
203 private static final String MICROSEC_RE
= DOT_RE
+ DGTS_3_RE
+ SEP_RE
+ DGTS_13_RE
;
204 private static final String NANOSEC_RE
= DOT_RE
+ DGTS_3_RE
+ SEP_RE
+ DGTS_3_RE
+ SEP_RE
+ DGTS_13_RE
;
206 private static final Pattern MILLISEC_PAT
= Pattern
.compile(MILLISEC_RE
);
207 private static final Pattern MICROSEC_PAT
= Pattern
.compile(MICROSEC_RE
);
208 private static final Pattern NANOSEC_PAT
= Pattern
.compile(NANOSEC_RE
);
210 // ------------------------------------------------------------------------
212 // ------------------------------------------------------------------------
214 // The default timestamp pattern
215 private static TmfTimestampFormat fDefaultTimeFormat
= null;
217 // The default time interval format
218 private static TmfTimestampFormat fDefaultIntervalFormat
= null;
220 // The timestamp pattern
221 private String fPattern
;
223 // The timestamp pattern
224 private List
<String
> fSupplPatterns
= new ArrayList
<String
>();
227 * The supplementary pattern letters. Can be redefined by sub-classes
228 * to either override existing letters or augment the letter set.
229 * If so, the format() method must provide the (re-)implementation of the
232 protected String fSupplPatternLetters
= "TSCN"; //$NON-NLS-1$
235 * The bracketing symbols used to mitigate the risk of a format string
236 * that contains escaped sequences that would conflict with our format
239 /** The open bracket symbol */
240 protected String fOpenBracket
= "[&"; //$NON-NLS-1$
242 /** The closing bracket symbol */
243 protected String fCloseBracket
= "&]"; //$NON-NLS-1$
245 // ------------------------------------------------------------------------
247 // ------------------------------------------------------------------------
250 * The default constructor (uses the default pattern)
252 public TmfTimestampFormat() {
253 this(TmfTimePreferences
.getInstance().getTimePattern());
257 * The normal constructor
259 * @param pattern the format pattern
261 public TmfTimestampFormat(String pattern
) {
262 applyPattern(pattern
);
266 * The full constructor
268 * @param pattern the format pattern
269 * @param timeZone the time zone
272 public TmfTimestampFormat(String pattern
, TimeZone timeZone
) {
273 setTimeZone(timeZone
);
274 applyPattern(pattern
);
278 * The copy constructor
280 * @param other the other format pattern
282 public TmfTimestampFormat(TmfTimestampFormat other
) {
283 this(other
.fPattern
);
286 // ------------------------------------------------------------------------
288 // ------------------------------------------------------------------------
293 public static void updateDefaultFormats() {
294 fDefaultTimeFormat
= new TmfTimestampFormat(TmfTimePreferences
.getInstance().getTimePattern(), TmfTimePreferences
.getInstance().getTimeZone());
295 fDefaultIntervalFormat
= new TmfTimestampFormat(TmfTimePreferences
.getInstance().getIntervalPattern());
299 * @param pattern the new default time pattern
300 * @deprecated The default time pattern depends on the preferences, see
301 * {@link TmfTimePreferences}. To change the default time
302 * pattern, modify the preferences and call {@link #updateDefaultFormats()}
305 public static void setDefaultTimeFormat(final String pattern
) {
309 * @return the default time format pattern
311 public static TmfTimestampFormat
getDefaulTimeFormat() {
312 if (fDefaultTimeFormat
== null) {
313 fDefaultTimeFormat
= new TmfTimestampFormat(TmfTimePreferences
.getInstance().getTimePattern(), TmfTimePreferences
.getInstance().getTimeZone());
315 return fDefaultTimeFormat
;
319 * @param pattern the new default interval pattern
320 * @deprecated The default interval format pattern depends on the
321 * preferences, see {@link TmfTimePreferences}. To change the
322 * default time pattern, modify the preferences and call
323 * {@link #updateDefaultFormats()}
326 public static void setDefaultIntervalFormat(final String pattern
) {
330 * @return the default interval format pattern
332 public static TmfTimestampFormat
getDefaulIntervalFormat() {
333 if (fDefaultIntervalFormat
== null) {
334 fDefaultIntervalFormat
= new TmfTimestampFormat(TmfTimePreferences
.getInstance().getIntervalPattern());
336 return fDefaultIntervalFormat
;
340 public void applyPattern(String pattern
) {
342 String quotedPattern
= quoteSpecificTags(pattern
);
343 super.applyPattern(quotedPattern
);
347 public String
toPattern() {
351 // ------------------------------------------------------------------------
353 // ------------------------------------------------------------------------
356 * Format the timestamp according to its pattern.
358 * @param value the timestamp value to format (in ns)
359 * @return the formatted timestamp
361 public synchronized String
format(long value
) {
363 // Split the timestamp value into its sub-components
364 long sec
= value
/ 1000000000; // seconds
365 long ms
= value
% 1000000000 / 1000000; // milliseconds
366 long cs
= value
% 1000000 / 1000; // microseconds
367 long ns
= value
% 1000; // nanoseconds
369 // Let the base class fill the stuff it knows about
370 StringBuffer result
= new StringBuffer(super.format(sec
* 1000 + ms
));
372 // In the case where there is no separation between 2 supplementary
373 // fields, the pattern will have the form "..'[pat-1]''[pat-2]'.." and
374 // the base class format() will interpret the 2 adjacent quotes as a
375 // wanted character in the result string as ("..[pat-1]'[pat-2]..").
376 // Remove these extra quotes before filling the supplementary fields.
377 int loc
= result
.indexOf(fCloseBracket
+ "'" + fOpenBracket
); //$NON-NLS-1$
379 result
.deleteCharAt(loc
+ fCloseBracket
.length());
380 loc
= result
.indexOf(fCloseBracket
+ "'" + fOpenBracket
); //$NON-NLS-1$
383 // Fill in our extensions
384 for (String pattern
: fSupplPatterns
) {
385 int length
= pattern
.length();
387 // Prepare the format buffer
388 StringBuffer fmt
= new StringBuffer(length
);
389 for (int i
= 0; i
< length
; i
++) {
390 fmt
.append("0"); //$NON-NLS-1$
392 DecimalFormat dfmt
= new DecimalFormat(fmt
.toString());
393 String fmtVal
= ""; //$NON-NLS-1$;
395 // Format the proper value as per the pattern
396 switch (pattern
.charAt(0)) {
398 fmtVal
= dfmt
.format(sec
);
401 fmtVal
= dfmt
.format(ms
);
404 fmtVal
= dfmt
.format(cs
);
407 fmtVal
= dfmt
.format(ns
);
413 // Substitute the placeholder with the formatted value
414 String ph
= new StringBuffer(fOpenBracket
+ pattern
+ fCloseBracket
).toString();
415 loc
= result
.indexOf(ph
);
416 result
.replace(loc
, loc
+ length
+ fOpenBracket
.length() + fCloseBracket
.length(), fmtVal
);
419 return result
.toString();
423 * Parse a string according to the format pattern
425 * @param string the source string
426 * @param ref the reference (base) time
427 * @return the parsed value
428 * @throws ParseException if the string has an invalid format
430 public synchronized long parseValue(final String string
, final long ref
) throws ParseException
{
433 if (string
== null || string
.length() == 0) {
437 // The timestamp sub-components
443 // Since we are processing the fractional part, substitute it with
444 // its pattern so the base parser doesn't complain
445 StringBuilder sb
= new StringBuilder(string
);
446 int dot
= string
.indexOf('.');
449 dot
= string
.length();
451 sb
= new StringBuilder(string
.substring(0, dot
));
452 String basePattern
= super.toPattern();
453 int dot2
= basePattern
.indexOf('.');
455 sb
.append(basePattern
.substring(dot2
));
458 // Fill in our extensions
459 for (String pattern
: fSupplPatterns
) {
460 String pat
= fOpenBracket
+ pattern
+ fCloseBracket
;
463 // Extract the substring corresponding to the extra pattern letters
464 // and replace with the pattern so the base parser can do its job.
465 switch (pattern
.charAt(0)) {
467 // Remove everything up to the first "." and compute the
468 // number of seconds since the epoch. If there is no period,
469 // assume an integer value and return immediately
471 return new DecimalFormat("0").parse(string
).longValue() * 1000000000; //$NON-NLS-1$
473 seconds
= new DecimalFormat("0").parse(string
.substring(0, dot
)).longValue(); //$NON-NLS-1$
478 matcher
= MILLISEC_PAT
.matcher(string
.substring(dot
));
479 if (matcher
.find()) {
480 millisec
= new Long(matcher
.group(1));
481 for (int l
= matcher
.group(1).length(); l
< 3; l
++) {
485 stripQuotes(sb
, pattern
);
488 matcher
= MICROSEC_PAT
.matcher(string
.substring(dot
));
489 if (matcher
.find()) {
490 microsec
= new Long(matcher
.group(2));
491 for (int l
= matcher
.group(2).length(); l
< 3; l
++) {
495 stripQuotes(sb
, pattern
);
498 matcher
= NANOSEC_PAT
.matcher(string
.substring(dot
));
499 if (matcher
.find()) {
500 nanosec
= new Long(matcher
.group(3));
501 for (int l
= matcher
.group(3).length(); l
< 3; l
++) {
505 stripQuotes(sb
, pattern
);
512 // If there was no "T" (thus not an interval), parse as a date
514 Date baseDate
= super.parse(sb
.toString());
516 Calendar refTime
= Calendar
.getInstance(getTimeZone());
517 refTime
.setTimeInMillis(ref
/ 1000000);
518 Calendar newTime
= Calendar
.getInstance(getTimeZone());
519 newTime
.setTimeInMillis(baseDate
.getTime());
521 int[] fields
= new int[] { Calendar
.YEAR
, Calendar
.MONTH
, Calendar
.DATE
, Calendar
.HOUR_OF_DAY
, Calendar
.MINUTE
, Calendar
.SECOND
};
522 for (int field
: fields
) {
523 int value
= newTime
.get(field
);
524 // Do some adjustments...
525 if (field
== Calendar
.YEAR
) {
527 } else if (field
== Calendar
.DATE
) {
530 // ... and fill-in the empty fields
532 newTime
.set(field
, refTime
.get(field
));
534 break; // Get out as soon as we have a significant value
537 seconds
= newTime
.getTimeInMillis() / 1000;
540 // Compute the value in ns
541 return seconds
* 1000000000 + millisec
* 1000000 + microsec
* 1000 + nanosec
;
545 * Parse a string according to the format pattern
547 * @param string the source string
548 * @return the parsed value
549 * @throws ParseException if the string has an invalid format
551 public long parseValue(final String string
) throws ParseException
{
552 long result
= parseValue(string
, 0);
557 // ------------------------------------------------------------------------
559 // ------------------------------------------------------------------------
562 * Copy the pattern but quote (bracket with "[&" and "&]") the
563 * TmfTimestampFormat specific tags so these fields are treated as
564 * comments by the base class.
566 * It also keeps track of the corresponding quoted fields so they can be
567 * properly populated later on (by format()).
569 * @param pattern the 'extended' pattern
570 * @return the quoted and bracketed pattern
572 private String
quoteSpecificTags(final String pattern
) {
574 StringBuffer result
= new StringBuffer();
576 int length
= pattern
.length();
577 boolean inQuote
= false;
579 for (int i
= 0; i
< length
; i
++) {
580 char c
= pattern
.charAt(i
);
583 // '' is treated as a single quote regardless of being
584 // in a quoted section.
585 if ((i
+ 1) < length
) {
586 c
= pattern
.charAt(i
+ 1);
597 if (fSupplPatternLetters
.indexOf(c
) != -1) {
598 StringBuilder pat
= new StringBuilder();
600 result
.insert(result
.length() - 1, "'" + fOpenBracket
); //$NON-NLS-1$
601 while ((i
+ 1) < length
&& pattern
.charAt(i
+ 1) == c
) {
606 result
.append(fCloseBracket
+ "'"); //$NON-NLS-1$
607 fSupplPatterns
.add(pat
.toString());
611 return result
.toString();
615 * Remove the quotes from the pattern
620 private void stripQuotes(StringBuilder sb
, String pattern
) {
621 String pt
= "'" + fOpenBracket
+ pattern
+ fCloseBracket
+ "'"; //$NON-NLS-1$//$NON-NLS-2$
622 int l
= sb
.indexOf(pt
);
624 sb
.delete(l
+ pt
.length() - 1, l
+ pt
.length());