1 package tim.prune.data;
3 import java.text.DateFormat;
4 import java.text.ParsePosition;
5 import java.text.SimpleDateFormat;
6 import java.util.Calendar;
8 import java.util.TimeZone;
9 import java.util.regex.Matcher;
10 import java.util.regex.Pattern;
13 * Class to hold the timestamp of a track point
14 * and provide conversion functions
16 public class Timestamp
18 private boolean _valid = false;
19 private long _milliseconds = 0L;
20 private String _text = null;
22 private static final DateFormat DEFAULT_DATETIME_FORMAT = DateFormat.getDateTimeInstance();
23 private static final DateFormat DEFAULT_DATE_FORMAT = DateFormat.getDateInstance();
24 private static final DateFormat DEFAULT_TIME_FORMAT = DateFormat.getTimeInstance();
25 private static boolean MillisAddedToTimeFormat = false;
26 private static final DateFormat ISO_8601_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
27 private static final DateFormat ISO_8601_FORMAT_WITH_MILLIS = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
28 private static final DateFormat ISO_8601_FORMAT_NOZ = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
29 private static DateFormat[] ALL_DATE_FORMATS = null;
30 private static Calendar CALENDAR = null;
31 private static final Pattern ISO8601_FRACTIONAL_PATTERN
32 = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})(?:[\\.,](\\d{1,3}))?(Z|[\\+-]\\d{2}(?::?\\d{2})?)?");
33 // year month day T hour minute sec millisec Z or +/- hours : minutes
34 private static final Pattern GENERAL_TIMESTAMP_PATTERN
35 = Pattern.compile("(\\d{4})\\D(\\d{2})\\D(\\d{2})\\D(\\d{2})\\D(\\d{2})\\D(\\d{2})");
36 private static long SECS_SINCE_1970 = 0L;
37 private static long SECS_SINCE_GARTRIP = 0L;
38 private static long MSECS_SINCE_1970 = 0L;
39 private static long MSECS_SINCE_1990 = 0L;
40 private static long TWENTY_YEARS_IN_SECS = 0L;
41 private static final long GARTRIP_OFFSET = 631065600L;
43 /** Possible formats for parsing and displaying timestamps */
51 /** Identifier for the parsing strategy to use */
52 private enum ParseType
69 /** Array of parse types to loop through (first one is changed to last successful type) */
70 private static ParseType[] ALL_PARSE_TYPES = {ParseType.NONE, ParseType.ISO8601_FRACTIONAL, ParseType.LONG,
71 ParseType.FIXED_FORMAT0, ParseType.FIXED_FORMAT1, ParseType.FIXED_FORMAT2, ParseType.FIXED_FORMAT3,
72 ParseType.FIXED_FORMAT4, ParseType.FIXED_FORMAT5, ParseType.FIXED_FORMAT6, ParseType.FIXED_FORMAT7,
73 ParseType.FIXED_FORMAT8, ParseType.GENERAL_STRING};
75 // Static block to initialise offsets
78 CALENDAR = Calendar.getInstance();
79 TimeZone gmtZone = TimeZone.getTimeZone("GMT");
80 CALENDAR.setTimeZone(gmtZone);
81 MSECS_SINCE_1970 = CALENDAR.getTimeInMillis();
82 SECS_SINCE_1970 = MSECS_SINCE_1970 / 1000L;
83 SECS_SINCE_GARTRIP = SECS_SINCE_1970 - GARTRIP_OFFSET;
84 CALENDAR.add(Calendar.YEAR, -20);
85 MSECS_SINCE_1990 = CALENDAR.getTimeInMillis();
86 TWENTY_YEARS_IN_SECS = (MSECS_SINCE_1970 - MSECS_SINCE_1990) / 1000L;
87 // Set timezone for output
88 ISO_8601_FORMAT.setTimeZone(gmtZone);
89 ISO_8601_FORMAT_WITH_MILLIS.setTimeZone(gmtZone);
90 DEFAULT_DATETIME_FORMAT.setTimeZone(gmtZone);
92 ALL_DATE_FORMATS = new DateFormat[] {
93 DEFAULT_DATETIME_FORMAT,
94 new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy"),
95 new SimpleDateFormat("HH:mm:ss dd MMM yyyy"),
96 new SimpleDateFormat("dd MMM yyyy HH:mm:ss"),
97 new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"),
98 new SimpleDateFormat("yyyy MMM dd HH:mm:ss"),
99 new SimpleDateFormat("MMM dd, yyyy hh:mm:ss aa"),
100 ISO_8601_FORMAT, ISO_8601_FORMAT_NOZ
102 for (DateFormat df : ALL_DATE_FORMATS) {
103 df.setLenient(false);
110 * @param inString String containing timestamp
112 public Timestamp(String inString)
116 if (inString != null && !inString.equals(""))
118 // Try each of the parse types in turn
119 for (ParseType type : ALL_PARSE_TYPES)
121 if (parseString(inString, type))
123 ALL_PARSE_TYPES[0] = type;
133 * Try to parse the given string in the specified way
134 * @param inString String to parse
135 * @param inType parse type to use
136 * @return true if successful
138 private boolean parseString(String inString, ParseType inType)
140 if (inString == null || inString.equals("")) {
145 case NONE: return false;
147 // Try to parse into a long
150 long rawValue = Long.parseLong(inString.trim());
151 _milliseconds = getMilliseconds(rawValue);
154 catch (NumberFormatException nfe)
158 case ISO8601_FRACTIONAL:
159 final Matcher fmatcher = ISO8601_FRACTIONAL_PATTERN.matcher(inString);
160 if (fmatcher.matches())
163 _milliseconds = getMilliseconds(Integer.parseInt(fmatcher.group(1)), // year
164 Integer.parseInt(fmatcher.group(2)), // month
165 Integer.parseInt(fmatcher.group(3)), // day
166 Integer.parseInt(fmatcher.group(4)), // hour
167 Integer.parseInt(fmatcher.group(5)), // minute
168 Integer.parseInt(fmatcher.group(6)), // second
169 fmatcher.group(7), // fractional seconds
170 fmatcher.group(8)); // timezone, if any
173 catch (NumberFormatException nfe) {}
177 case FIXED_FORMAT0: return parseString(inString, ALL_DATE_FORMATS[0]);
178 case FIXED_FORMAT1: return parseString(inString, ALL_DATE_FORMATS[1]);
179 case FIXED_FORMAT2: return parseString(inString, ALL_DATE_FORMATS[2]);
180 case FIXED_FORMAT3: return parseString(inString, ALL_DATE_FORMATS[3]);
181 case FIXED_FORMAT4: return parseString(inString, ALL_DATE_FORMATS[4]);
182 case FIXED_FORMAT5: return parseString(inString, ALL_DATE_FORMATS[5]);
183 case FIXED_FORMAT6: return parseString(inString, ALL_DATE_FORMATS[6]);
184 case FIXED_FORMAT7: return parseString(inString, ALL_DATE_FORMATS[7]);
185 case FIXED_FORMAT8: return parseString(inString, ALL_DATE_FORMATS[8]);
188 if (inString.length() == 19)
190 final Matcher matcher = GENERAL_TIMESTAMP_PATTERN.matcher(inString);
191 if (matcher.matches())
194 _milliseconds = getMilliseconds(Integer.parseInt(matcher.group(1)),
195 Integer.parseInt(matcher.group(2)),
196 Integer.parseInt(matcher.group(3)),
197 Integer.parseInt(matcher.group(4)),
198 Integer.parseInt(matcher.group(5)),
199 Integer.parseInt(matcher.group(6)),
200 null, null); // no fractions of a second and no timezone
203 catch (NumberFormatException nfe2) {} // parse shouldn't fail if matcher matched
213 * Try to parse the given string with the given date format
214 * @param inString String to parse
215 * @param inDateFormat Date format to use
216 * @return true if successful
218 private boolean parseString(String inString, DateFormat inDateFormat)
220 ParsePosition pPos = new ParsePosition(0);
221 Date date = inDateFormat.parse(inString, pPos);
222 if (date != null && inString.length() == pPos.getIndex()) // require use of _all_ the string, not just the beginning
224 CALENDAR.setTime(date);
225 _milliseconds = CALENDAR.getTimeInMillis();
234 * Constructor giving each field value individually
236 * @param inMonth month, beginning with 1
237 * @param inDay day of month, beginning with 1
238 * @param inHour hour of day, 0-24
239 * @param inMinute minute
240 * @param inSecond seconds
242 public Timestamp(int inYear, int inMonth, int inDay, int inHour, int inMinute, int inSecond)
244 _milliseconds = getMilliseconds(inYear, inMonth, inDay, inHour, inMinute, inSecond, null, null);
250 * Constructor giving millis
251 * @param inMillis milliseconds since 1970
253 public Timestamp(long inMillis)
255 _milliseconds = inMillis;
261 * Convert the given timestamp parameters into a number of milliseconds
263 * @param inMonth month, beginning with 1
264 * @param inDay day of month, beginning with 1
265 * @param inHour hour of day, 0-24
266 * @param inMinute minute
267 * @param inSecond seconds
268 * @param inFraction fractions of a second
269 * @param inTimezone timezone, if any
270 * @return number of milliseconds
272 private static long getMilliseconds(int inYear, int inMonth, int inDay,
273 int inHour, int inMinute, int inSecond, String inFraction, String inTimezone)
275 Calendar cal = Calendar.getInstance();
277 if (inTimezone == null || inTimezone.equals("") || inTimezone.equals("Z")) {
278 // No timezone, use zulu
279 cal.setTimeZone(TimeZone.getTimeZone("GMT"));
282 // Timezone specified, pass to calendar
283 cal.setTimeZone(TimeZone.getTimeZone("GMT" + inTimezone));
285 cal.set(Calendar.YEAR, inYear);
286 cal.set(Calendar.MONTH, inMonth - 1);
287 cal.set(Calendar.DAY_OF_MONTH, inDay);
288 cal.set(Calendar.HOUR_OF_DAY, inHour);
289 cal.set(Calendar.MINUTE, inMinute);
290 cal.set(Calendar.SECOND, inSecond);
292 if (inFraction != null)
295 int frac = Integer.parseInt(inFraction);
296 final int fracLen = inFraction.length();
298 case 1: millis = frac * 100; break;
299 case 2: millis = frac * 10; break;
300 case 3: millis = frac; break;
303 catch (NumberFormatException nfe) {} // ignore errors, millis stay at 0
305 cal.set(Calendar.MILLISECOND, millis);
306 return cal.getTimeInMillis();
310 * Convert the given long parameters into a number of millisseconds
311 * @param inRawValue long value representing seconds / milliseconds
312 * @return number of milliseconds
314 private static long getMilliseconds(long inRawValue)
316 // check for each format possibility and pick nearest
317 long diff1 = Math.abs(SECS_SINCE_1970 - inRawValue);
318 long diff2 = Math.abs(MSECS_SINCE_1970 - inRawValue);
319 long diff3 = Math.abs(MSECS_SINCE_1990 - inRawValue);
320 long diff4 = Math.abs(SECS_SINCE_GARTRIP - inRawValue);
322 // Start off with "seconds since 1970" format
323 long smallestDiff = diff1;
324 long millis = inRawValue * 1000;
325 // Now check millis since 1970
326 if (diff2 < smallestDiff)
328 // milliseconds since 1970
330 smallestDiff = diff2;
332 // Now millis since 1990
333 if (diff3 < smallestDiff)
335 // milliseconds since 1990
336 millis = inRawValue + TWENTY_YEARS_IN_SECS * 1000L;
337 smallestDiff = diff3;
339 // Lastly, check gartrip offset
340 if (diff4 < smallestDiff)
342 // seconds since gartrip offset
343 millis = (inRawValue + GARTRIP_OFFSET) * 1000L;
349 * @return true if timestamp is valid
351 public boolean isValid()
357 * @return true if the timestamp has non-zero milliseconds
359 public boolean hasMilliseconds()
361 return isValid() && (_milliseconds % 1000L) > 0;
364 * @param inOther other Timestamp
365 * @return true if this one is at least a millisecond after the other
367 public boolean isAfter(Timestamp inOther)
369 return getMillisecondsSince(inOther) > 0L;
373 * Calculate the difference between two Timestamps in seconds
374 * @param inOther other, earlier Timestamp
375 * @return number of seconds since other timestamp
377 public long getSecondsSince(Timestamp inOther)
379 return (_milliseconds - inOther._milliseconds) / 1000L;
383 * Calculate the difference between two Timestamps in milliseconds
384 * @param inOther other, earlier Timestamp
385 * @return number of millisseconds since other timestamp
387 public long getMillisecondsSince(Timestamp inOther)
389 return _milliseconds - inOther._milliseconds;
393 * @param inOther other timestamp to compare
394 * @return true if they're equal to the nearest millisecond
396 public boolean isEqual(Timestamp inOther)
398 return inOther != null && _milliseconds == inOther._milliseconds;
402 * @param inOther other Timestamp
403 * @return true if this one is before the other
405 public boolean isBefore(Timestamp inOther)
407 return getMillisecondsSince(inOther) < 0L;
411 * Add the given number of seconds offset
412 * @param inOffset number of seconds to add/subtract
414 public void addOffset(long inOffset)
416 _milliseconds += (inOffset * 1000L);
421 * Add the given TimeDifference to this Timestamp
422 * @param inOffset TimeDifference to add
423 * @return new Timestamp object
425 public Timestamp createPlusOffset(TimeDifference inOffset)
427 return createPlusOffset(inOffset.getTotalSeconds());
431 * Add the given number of seconds to this Timestamp
432 * @param inSeconds number of seconds to add
433 * @return new Timestamp object
435 public Timestamp createPlusOffset(long inSeconds)
437 return new Timestamp(_milliseconds + (inSeconds * 1000L));
442 * Subtract the given TimeDifference from this Timestamp
443 * @param inOffset TimeDifference to subtract
444 * @return new Timestamp object
446 public Timestamp createMinusOffset(TimeDifference inOffset)
448 return new Timestamp(_milliseconds - (inOffset.getTotalSeconds() * 1000L));
453 * @return Description of timestamp in locale-specific format
455 public String getText()
457 return getText(Format.LOCALE);
461 * @param inFormat format of timestamp
462 * @return Description of timestamp in required format
464 public String getText(Format inFormat)
466 if (!_valid) {return "";}
470 if (_text != null) {return _text;}
471 // otherwise fallthrough to default
474 return format(DEFAULT_DATETIME_FORMAT);
476 return format(hasMilliseconds() ? ISO_8601_FORMAT_WITH_MILLIS : ISO_8601_FORMAT);
482 * @return date part of timestamp in locale-specific format
484 public String getDateText()
486 if (!_valid) return "";
487 return format(DEFAULT_DATE_FORMAT);
491 * @return Description of time part of timestamp in locale-specific format
493 public String getTimeText()
495 if (!_valid) return "";
496 // Maybe we should add milliseconds to this format?
497 if (hasMilliseconds() && !MillisAddedToTimeFormat)
501 SimpleDateFormat sdf = (SimpleDateFormat) DEFAULT_TIME_FORMAT;
502 String pattern = sdf.toPattern();
503 if (pattern.indexOf("ss") > 0 && pattern.indexOf("SS") < 0)
505 sdf.applyPattern(pattern.replaceFirst("s+", "$0.SSS"));
506 MillisAddedToTimeFormat = true;
509 catch (ClassCastException cce) {}
511 return format(DEFAULT_TIME_FORMAT);
515 * Utility method for formatting dates / times
516 * @param inFormat formatter object
517 * @return formatted String
519 private String format(DateFormat inFormat)
521 CALENDAR.setTimeZone(TimeZone.getTimeZone("GMT"));
522 CALENDAR.setTimeInMillis(_milliseconds);
523 return inFormat.format(CALENDAR.getTime());
527 * @return a Calendar object representing this timestamp
529 public Calendar getCalendar()
531 Calendar cal = Calendar.getInstance();
532 cal.setTimeZone(TimeZone.getTimeZone("GMT"));
533 cal.setTimeInMillis(_milliseconds);