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;
14 * Class to hold a UTC-based timestamp, for example of a track point.
15 * When the selected timezone changes, this timestamp will keep its
16 * numerical value but the date and time will change accordingly.
18 public class TimestampUtc extends Timestamp
20 private boolean _valid = false;
21 private long _milliseconds = 0L;
22 private String _text = null;
24 private static final DateFormat ISO_8601_FORMAT_NOZ = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
25 private static DateFormat[] ALL_DATE_FORMATS = null;
26 private static Calendar CALENDAR = null;
27 private static final Pattern ISO8601_FRACTIONAL_PATTERN
28 = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})(?:[\\.,](\\d{1,3}))?(Z|[\\+-]\\d{2}(?::?\\d{2})?)?");
29 // year month day T hour minute sec millisec Z or +/- hours : minutes
30 private static final Pattern GENERAL_TIMESTAMP_PATTERN
31 = Pattern.compile("(\\d{4})\\D(\\d{2})\\D(\\d{2})\\D(\\d{2})\\D(\\d{2})\\D(\\d{2})");
32 private static long SECS_SINCE_1970 = 0L;
33 private static long SECS_SINCE_GARTRIP = 0L;
34 private static long MSECS_SINCE_1970 = 0L;
35 private static long MSECS_SINCE_1990 = 0L;
36 private static long TWENTY_YEARS_IN_SECS = 0L;
37 private static final long GARTRIP_OFFSET = 631065600L;
39 /** Identifier for the parsing strategy to use */
40 private enum ParseType
57 /** Array of parse types to loop through (first one is changed to last successful type) */
58 private static ParseType[] ALL_PARSE_TYPES = {ParseType.NONE, ParseType.ISO8601_FRACTIONAL, ParseType.LONG,
59 ParseType.FIXED_FORMAT0, ParseType.FIXED_FORMAT1, ParseType.FIXED_FORMAT2, ParseType.FIXED_FORMAT3,
60 ParseType.FIXED_FORMAT4, ParseType.FIXED_FORMAT5, ParseType.FIXED_FORMAT6, ParseType.FIXED_FORMAT7,
61 ParseType.FIXED_FORMAT8, ParseType.GENERAL_STRING};
63 // Static block to initialise offsets
66 CALENDAR = Calendar.getInstance();
67 TimeZone gmtZone = TimeZone.getTimeZone("GMT");
68 CALENDAR.setTimeZone(gmtZone);
69 MSECS_SINCE_1970 = CALENDAR.getTimeInMillis();
70 SECS_SINCE_1970 = MSECS_SINCE_1970 / 1000L;
71 SECS_SINCE_GARTRIP = SECS_SINCE_1970 - GARTRIP_OFFSET;
72 CALENDAR.add(Calendar.YEAR, -20);
73 MSECS_SINCE_1990 = CALENDAR.getTimeInMillis();
74 TWENTY_YEARS_IN_SECS = (MSECS_SINCE_1970 - MSECS_SINCE_1990) / 1000L;
76 ALL_DATE_FORMATS = new DateFormat[]
78 DEFAULT_DATETIME_FORMAT,
79 new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy"),
80 new SimpleDateFormat("HH:mm:ss dd MMM yyyy"),
81 new SimpleDateFormat("dd MMM yyyy HH:mm:ss"),
82 new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"),
83 new SimpleDateFormat("yyyy MMM dd HH:mm:ss"),
84 new SimpleDateFormat("MMM dd, yyyy hh:mm:ss aa"),
85 ISO_8601_FORMAT, ISO_8601_FORMAT_NOZ
87 for (DateFormat df : ALL_DATE_FORMATS)
90 df.setTimeZone(gmtZone);
97 * @param inString String containing timestamp
99 public TimestampUtc(String inString)
103 if (inString != null && !inString.equals(""))
105 // Try each of the parse types in turn
106 for (ParseType type : ALL_PARSE_TYPES)
108 if (parseString(inString, type))
110 ALL_PARSE_TYPES[0] = type;
120 * Try to parse the given string in the specified way
121 * @param inString String to parse
122 * @param inType parse type to use
123 * @return true if successful
125 private boolean parseString(String inString, ParseType inType)
127 if (inString == null || inString.equals("")) {
132 case NONE: return false;
134 // Try to parse into a long
137 long rawValue = Long.parseLong(inString.trim());
138 _milliseconds = getMilliseconds(rawValue);
141 catch (NumberFormatException nfe)
145 case ISO8601_FRACTIONAL:
146 final Matcher fmatcher = ISO8601_FRACTIONAL_PATTERN.matcher(inString);
147 if (fmatcher.matches())
150 _milliseconds = getMilliseconds(Integer.parseInt(fmatcher.group(1)), // year
151 Integer.parseInt(fmatcher.group(2)), // month
152 Integer.parseInt(fmatcher.group(3)), // day
153 Integer.parseInt(fmatcher.group(4)), // hour
154 Integer.parseInt(fmatcher.group(5)), // minute
155 Integer.parseInt(fmatcher.group(6)), // second
156 fmatcher.group(7), // fractional seconds
157 fmatcher.group(8)); // timezone, if any
160 catch (NumberFormatException nfe) {}
164 case FIXED_FORMAT0: return parseString(inString, ALL_DATE_FORMATS[0]);
165 case FIXED_FORMAT1: return parseString(inString, ALL_DATE_FORMATS[1]);
166 case FIXED_FORMAT2: return parseString(inString, ALL_DATE_FORMATS[2]);
167 case FIXED_FORMAT3: return parseString(inString, ALL_DATE_FORMATS[3]);
168 case FIXED_FORMAT4: return parseString(inString, ALL_DATE_FORMATS[4]);
169 case FIXED_FORMAT5: return parseString(inString, ALL_DATE_FORMATS[5]);
170 case FIXED_FORMAT6: return parseString(inString, ALL_DATE_FORMATS[6]);
171 case FIXED_FORMAT7: return parseString(inString, ALL_DATE_FORMATS[7]);
172 case FIXED_FORMAT8: return parseString(inString, ALL_DATE_FORMATS[8]);
175 if (inString.length() == 19)
177 final Matcher matcher = GENERAL_TIMESTAMP_PATTERN.matcher(inString);
178 if (matcher.matches())
181 _milliseconds = getMilliseconds(Integer.parseInt(matcher.group(1)),
182 Integer.parseInt(matcher.group(2)),
183 Integer.parseInt(matcher.group(3)),
184 Integer.parseInt(matcher.group(4)),
185 Integer.parseInt(matcher.group(5)),
186 Integer.parseInt(matcher.group(6)),
187 null, null); // no fractions of a second and no timezone
190 catch (NumberFormatException nfe2) {} // parse shouldn't fail if matcher matched
200 * Try to parse the given string with the given date format
201 * @param inString String to parse
202 * @param inDateFormat Date format to use
203 * @return true if successful
205 private boolean parseString(String inString, DateFormat inDateFormat)
207 ParsePosition pPos = new ParsePosition(0);
208 Date date = inDateFormat.parse(inString, pPos);
209 if (date != null && inString.length() == pPos.getIndex()) // require use of _all_ the string, not just the beginning
211 CALENDAR.setTime(date);
212 _milliseconds = CALENDAR.getTimeInMillis();
221 * Constructor giving millis
222 * @param inMillis milliseconds since 1970
224 public TimestampUtc(long inMillis)
226 _milliseconds = inMillis;
232 * Convert the given timestamp parameters into a number of milliseconds
234 * @param inMonth month, beginning with 1
235 * @param inDay day of month, beginning with 1
236 * @param inHour hour of day, 0-24
237 * @param inMinute minute
238 * @param inSecond seconds
239 * @param inFraction fractions of a second
240 * @param inTimezone timezone, if any
241 * @return number of milliseconds
243 private static long getMilliseconds(int inYear, int inMonth, int inDay,
244 int inHour, int inMinute, int inSecond, String inFraction, String inTimezone)
246 Calendar cal = Calendar.getInstance();
248 if (inTimezone == null || inTimezone.equals("") || inTimezone.equals("Z")) {
249 // No timezone, use zulu
250 cal.setTimeZone(TimeZone.getTimeZone("GMT"));
253 // Timezone specified, pass to calendar
254 cal.setTimeZone(TimeZone.getTimeZone("GMT" + inTimezone));
256 cal.set(Calendar.YEAR, inYear);
257 cal.set(Calendar.MONTH, inMonth - 1);
258 cal.set(Calendar.DAY_OF_MONTH, inDay);
259 cal.set(Calendar.HOUR_OF_DAY, inHour);
260 cal.set(Calendar.MINUTE, inMinute);
261 cal.set(Calendar.SECOND, inSecond);
263 if (inFraction != null)
266 int frac = Integer.parseInt(inFraction);
267 final int fracLen = inFraction.length();
269 case 1: millis = frac * 100; break;
270 case 2: millis = frac * 10; break;
271 case 3: millis = frac; break;
274 catch (NumberFormatException nfe) {} // ignore errors, millis stay at 0
276 cal.set(Calendar.MILLISECOND, millis);
277 return cal.getTimeInMillis();
281 * Convert the given long parameters into a number of milliseconds
282 * @param inRawValue long value representing seconds / milliseconds
283 * @return number of milliseconds
285 private static long getMilliseconds(long inRawValue)
287 // check for each format possibility and pick nearest
288 long diff1 = Math.abs(SECS_SINCE_1970 - inRawValue);
289 long diff2 = Math.abs(MSECS_SINCE_1970 - inRawValue);
290 long diff3 = Math.abs(MSECS_SINCE_1990 - inRawValue);
291 long diff4 = Math.abs(SECS_SINCE_GARTRIP - inRawValue);
293 // Start off with "seconds since 1970" format
294 long smallestDiff = diff1;
295 long millis = inRawValue * 1000;
296 // Now check millis since 1970
297 if (diff2 < smallestDiff)
299 // milliseconds since 1970
301 smallestDiff = diff2;
303 // Now millis since 1990
304 if (diff3 < smallestDiff)
306 // milliseconds since 1990
307 millis = inRawValue + TWENTY_YEARS_IN_SECS * 1000L;
308 smallestDiff = diff3;
310 // Lastly, check gartrip offset
311 if (diff4 < smallestDiff)
313 // seconds since gartrip offset
314 millis = (inRawValue + GARTRIP_OFFSET) * 1000L;
320 * @return true if timestamp is valid
322 public boolean isValid()
328 * @return true if the timestamp has non-zero milliseconds
330 protected boolean hasMilliseconds()
332 return isValid() && (_milliseconds % 1000L) > 0;
336 * @return the milliseconds according to the given timezone
338 public long getMilliseconds(TimeZone inZone)
340 return _milliseconds;
345 * Add the given number of seconds offset
346 * @param inOffset number of seconds to add/subtract
348 public void addOffsetSeconds(long inOffset)
350 _milliseconds += (inOffset * 1000L);
356 * @param inFormat format of timestamp
357 * @param inTimezone timezone to use
358 * @return Description of timestamp in required format
360 public String getText(Format inFormat, TimeZone inTimezone)
362 // Use the cached text if possible
365 && inFormat == Format.ORIGINAL)
370 // Nothing cached, so use the regular one
371 return super.getText(inFormat, inTimezone);
375 * Utility method for formatting dates / times
376 * @param inFormat formatter object
377 * @param inTimezone timezone to use
378 * @return formatted String
380 protected String format(DateFormat inFormat, TimeZone inTimezone)
382 CALENDAR.setTimeZone(TimeZone.getTimeZone("GMT"));
383 inFormat.setTimeZone(inTimezone == null ? TimeZone.getTimeZone("GMT") : inTimezone);
385 CALENDAR.setTimeInMillis(_milliseconds);
386 return inFormat.format(CALENDAR.getTime());
390 * @return a Calendar object representing this timestamp
392 public Calendar getCalendar(TimeZone inZone)
394 Calendar cal = Calendar.getInstance();
395 cal.setTimeZone(TimeZone.getTimeZone("GMT"));
396 cal.setTimeInMillis(_milliseconds);