1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
|
package tim.prune.data;
import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Class to hold a UTC-based timestamp, for example of a track point.
* When the selected timezone changes, this timestamp will keep its
* numerical value but the date and time will change accordingly.
*/
public class TimestampUtc extends Timestamp
{
private boolean _valid = false;
private long _milliseconds = 0L;
private String _text = null;
private static final DateFormat ISO_8601_FORMAT_NOZ = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
private static DateFormat[] ALL_DATE_FORMATS = null;
private static Calendar CALENDAR = null;
private static final Pattern ISO8601_FRACTIONAL_PATTERN
= Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})(?:[\\.,](\\d{1,3}))?(Z|[\\+-]\\d{2}(?::?\\d{2})?)?");
// year month day T hour minute sec millisec Z or +/- hours : minutes
private static final Pattern GENERAL_TIMESTAMP_PATTERN
= Pattern.compile("(\\d{4})\\D(\\d{2})\\D(\\d{2})\\D(\\d{2})\\D(\\d{2})\\D(\\d{2})");
private static long SECS_SINCE_1970 = 0L;
private static long SECS_SINCE_GARTRIP = 0L;
private static long MSECS_SINCE_1970 = 0L;
private static long MSECS_SINCE_1990 = 0L;
private static long TWENTY_YEARS_IN_SECS = 0L;
private static final long GARTRIP_OFFSET = 631065600L;
/** Identifier for the parsing strategy to use */
private enum ParseType
{
NONE,
ISO8601_FRACTIONAL,
LONG,
FIXED_FORMAT0,
FIXED_FORMAT1,
FIXED_FORMAT2,
FIXED_FORMAT3,
FIXED_FORMAT4,
FIXED_FORMAT5,
FIXED_FORMAT6,
FIXED_FORMAT7,
FIXED_FORMAT8,
GENERAL_STRING
}
/** Array of parse types to loop through (first one is changed to last successful type) */
private static ParseType[] ALL_PARSE_TYPES = {ParseType.NONE, ParseType.ISO8601_FRACTIONAL, ParseType.LONG,
ParseType.FIXED_FORMAT0, ParseType.FIXED_FORMAT1, ParseType.FIXED_FORMAT2, ParseType.FIXED_FORMAT3,
ParseType.FIXED_FORMAT4, ParseType.FIXED_FORMAT5, ParseType.FIXED_FORMAT6, ParseType.FIXED_FORMAT7,
ParseType.FIXED_FORMAT8, ParseType.GENERAL_STRING};
// Static block to initialise offsets
static
{
CALENDAR = Calendar.getInstance();
TimeZone gmtZone = TimeZone.getTimeZone("GMT");
CALENDAR.setTimeZone(gmtZone);
MSECS_SINCE_1970 = CALENDAR.getTimeInMillis();
SECS_SINCE_1970 = MSECS_SINCE_1970 / 1000L;
SECS_SINCE_GARTRIP = SECS_SINCE_1970 - GARTRIP_OFFSET;
CALENDAR.add(Calendar.YEAR, -20);
MSECS_SINCE_1990 = CALENDAR.getTimeInMillis();
TWENTY_YEARS_IN_SECS = (MSECS_SINCE_1970 - MSECS_SINCE_1990) / 1000L;
// Date formats
ALL_DATE_FORMATS = new DateFormat[]
{
DEFAULT_DATETIME_FORMAT,
new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy"),
new SimpleDateFormat("HH:mm:ss dd MMM yyyy"),
new SimpleDateFormat("dd MMM yyyy HH:mm:ss"),
new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"),
new SimpleDateFormat("yyyy MMM dd HH:mm:ss"),
new SimpleDateFormat("MMM dd, yyyy hh:mm:ss aa"),
ISO_8601_FORMAT, ISO_8601_FORMAT_NOZ
};
for (DateFormat df : ALL_DATE_FORMATS)
{
df.setLenient(false);
df.setTimeZone(gmtZone);
}
}
/**
* Constructor
* @param inString String containing timestamp
*/
public TimestampUtc(String inString)
{
_valid = false;
_text = null;
if (inString != null && !inString.equals(""))
{
// Try each of the parse types in turn
for (ParseType type : ALL_PARSE_TYPES)
{
if (parseString(inString, type))
{
ALL_PARSE_TYPES[0] = type;
_valid = true;
_text = inString;
return;
}
}
}
}
/**
* Try to parse the given string in the specified way
* @param inString String to parse
* @param inType parse type to use
* @return true if successful
*/
private boolean parseString(String inString, ParseType inType)
{
if (inString == null || inString.equals("")) {
return false;
}
switch (inType)
{
case NONE: return false;
case LONG:
// Try to parse into a long
try
{
long rawValue = Long.parseLong(inString.trim());
_milliseconds = getMilliseconds(rawValue);
return true;
}
catch (NumberFormatException nfe)
{}
break;
case ISO8601_FRACTIONAL:
final Matcher fmatcher = ISO8601_FRACTIONAL_PATTERN.matcher(inString);
if (fmatcher.matches())
{
try {
_milliseconds = getMilliseconds(Integer.parseInt(fmatcher.group(1)), // year
Integer.parseInt(fmatcher.group(2)), // month
Integer.parseInt(fmatcher.group(3)), // day
Integer.parseInt(fmatcher.group(4)), // hour
Integer.parseInt(fmatcher.group(5)), // minute
Integer.parseInt(fmatcher.group(6)), // second
fmatcher.group(7), // fractional seconds
fmatcher.group(8)); // timezone, if any
return true;
}
catch (NumberFormatException nfe) {}
}
break;
case FIXED_FORMAT0: return parseString(inString, ALL_DATE_FORMATS[0]);
case FIXED_FORMAT1: return parseString(inString, ALL_DATE_FORMATS[1]);
case FIXED_FORMAT2: return parseString(inString, ALL_DATE_FORMATS[2]);
case FIXED_FORMAT3: return parseString(inString, ALL_DATE_FORMATS[3]);
case FIXED_FORMAT4: return parseString(inString, ALL_DATE_FORMATS[4]);
case FIXED_FORMAT5: return parseString(inString, ALL_DATE_FORMATS[5]);
case FIXED_FORMAT6: return parseString(inString, ALL_DATE_FORMATS[6]);
case FIXED_FORMAT7: return parseString(inString, ALL_DATE_FORMATS[7]);
case FIXED_FORMAT8: return parseString(inString, ALL_DATE_FORMATS[8]);
case GENERAL_STRING:
if (inString.length() == 19)
{
final Matcher matcher = GENERAL_TIMESTAMP_PATTERN.matcher(inString);
if (matcher.matches())
{
try {
_milliseconds = getMilliseconds(Integer.parseInt(matcher.group(1)),
Integer.parseInt(matcher.group(2)),
Integer.parseInt(matcher.group(3)),
Integer.parseInt(matcher.group(4)),
Integer.parseInt(matcher.group(5)),
Integer.parseInt(matcher.group(6)),
null, null); // no fractions of a second and no timezone
return true;
}
catch (NumberFormatException nfe2) {} // parse shouldn't fail if matcher matched
}
}
return false;
}
return false;
}
/**
* Try to parse the given string with the given date format
* @param inString String to parse
* @param inDateFormat Date format to use
* @return true if successful
*/
private boolean parseString(String inString, DateFormat inDateFormat)
{
ParsePosition pPos = new ParsePosition(0);
Date date = inDateFormat.parse(inString, pPos);
if (date != null && inString.length() == pPos.getIndex()) // require use of _all_ the string, not just the beginning
{
CALENDAR.setTime(date);
_milliseconds = CALENDAR.getTimeInMillis();
return true;
}
return false;
}
/**
* Constructor giving millis
* @param inMillis milliseconds since 1970
*/
public TimestampUtc(long inMillis)
{
_milliseconds = inMillis;
_valid = true;
}
/**
* Convert the given timestamp parameters into a number of milliseconds
* @param inYear year
* @param inMonth month, beginning with 1
* @param inDay day of month, beginning with 1
* @param inHour hour of day, 0-24
* @param inMinute minute
* @param inSecond seconds
* @param inFraction fractions of a second
* @param inTimezone timezone, if any
* @return number of milliseconds
*/
private static long getMilliseconds(int inYear, int inMonth, int inDay,
int inHour, int inMinute, int inSecond, String inFraction, String inTimezone)
{
Calendar cal = Calendar.getInstance();
// Timezone, if any
if (inTimezone == null || inTimezone.equals("") || inTimezone.equals("Z")) {
// No timezone, use zulu
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
}
else {
// Timezone specified, pass to calendar
cal.setTimeZone(TimeZone.getTimeZone("GMT" + inTimezone));
}
cal.set(Calendar.YEAR, inYear);
cal.set(Calendar.MONTH, inMonth - 1);
cal.set(Calendar.DAY_OF_MONTH, inDay);
cal.set(Calendar.HOUR_OF_DAY, inHour);
cal.set(Calendar.MINUTE, inMinute);
cal.set(Calendar.SECOND, inSecond);
int millis = 0;
if (inFraction != null)
{
try {
int frac = Integer.parseInt(inFraction);
final int fracLen = inFraction.length();
switch (fracLen) {
case 1: millis = frac * 100; break;
case 2: millis = frac * 10; break;
case 3: millis = frac; break;
}
}
catch (NumberFormatException nfe) {} // ignore errors, millis stay at 0
}
cal.set(Calendar.MILLISECOND, millis);
return cal.getTimeInMillis();
}
/**
* Convert the given long parameters into a number of milliseconds
* @param inRawValue long value representing seconds / milliseconds
* @return number of milliseconds
*/
private static long getMilliseconds(long inRawValue)
{
// check for each format possibility and pick nearest
long diff1 = Math.abs(SECS_SINCE_1970 - inRawValue);
long diff2 = Math.abs(MSECS_SINCE_1970 - inRawValue);
long diff3 = Math.abs(MSECS_SINCE_1990 - inRawValue);
long diff4 = Math.abs(SECS_SINCE_GARTRIP - inRawValue);
// Start off with "seconds since 1970" format
long smallestDiff = diff1;
long millis = inRawValue * 1000;
// Now check millis since 1970
if (diff2 < smallestDiff)
{
// milliseconds since 1970
millis = inRawValue;
smallestDiff = diff2;
}
// Now millis since 1990
if (diff3 < smallestDiff)
{
// milliseconds since 1990
millis = inRawValue + TWENTY_YEARS_IN_SECS * 1000L;
smallestDiff = diff3;
}
// Lastly, check gartrip offset
if (diff4 < smallestDiff)
{
// seconds since gartrip offset
millis = (inRawValue + GARTRIP_OFFSET) * 1000L;
}
return millis;
}
/**
* @return true if timestamp is valid
*/
public boolean isValid()
{
return _valid;
}
/**
* @return true if the timestamp has non-zero milliseconds
*/
protected boolean hasMilliseconds()
{
return isValid() && (_milliseconds % 1000L) > 0;
}
/**
* @return the milliseconds according to the given timezone
*/
public long getMilliseconds(TimeZone inZone)
{
return _milliseconds;
}
/**
* Add the given number of seconds offset
* @param inOffset number of seconds to add/subtract
*/
public void addOffsetSeconds(long inOffset)
{
_milliseconds += (inOffset * 1000L);
_text = null;
}
/**
* @param inFormat format of timestamp
* @param inTimezone timezone to use
* @return Description of timestamp in required format
*/
public String getText(Format inFormat, TimeZone inTimezone)
{
// Use the cached text if possible
if (isValid()
&& _text != null
&& inFormat == Format.ORIGINAL)
{
return _text;
}
// Nothing cached, so use the regular one
return super.getText(inFormat, inTimezone);
}
/**
* Utility method for formatting dates / times
* @param inFormat formatter object
* @param inTimezone timezone to use
* @return formatted String
*/
protected String format(DateFormat inFormat, TimeZone inTimezone)
{
CALENDAR.setTimeZone(TimeZone.getTimeZone("GMT"));
inFormat.setTimeZone(inTimezone == null ? TimeZone.getTimeZone("GMT") : inTimezone);
CALENDAR.setTimeInMillis(_milliseconds);
return inFormat.format(CALENDAR.getTime());
}
/**
* @return a Calendar object representing this timestamp
*/
public Calendar getCalendar(TimeZone inZone)
{
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
cal.setTimeInMillis(_milliseconds);
return cal;
}
}
|