X-Git-Url: https://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=tim%2Fprune%2Fdata%2FTimestamp.java;h=334bf499e94729ca32d18fdccaaf41c429a5708f;hp=965853ad036e5b61a44fe1dc58af87154bfc7df0;hb=da0b1f449260a0b4a94318006382a9039726ef3e;hpb=312fec956e43f5d0a38617da5d0add9c62563e2c diff --git a/tim/prune/data/Timestamp.java b/tim/prune/data/Timestamp.java index 965853a..334bf49 100644 --- a/tim/prune/data/Timestamp.java +++ b/tim/prune/data/Timestamp.java @@ -15,8 +15,11 @@ public class Timestamp private boolean _valid = false; private long _seconds = 0L; private String _text = null; + private String _timeText = null; private static DateFormat DEFAULT_DATE_FORMAT = DateFormat.getDateTimeInstance(); + private static DateFormat DEFAULT_TIME_FORMAT = DateFormat.getTimeInstance(); + private static DateFormat ISO_8601_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); private static DateFormat[] ALL_DATE_FORMATS = null; private static Calendar CALENDAR = null; private static long SECS_SINCE_1970 = 0L; @@ -24,9 +27,15 @@ public class Timestamp 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; + /** Specifies original timestamp format */ + public static final int FORMAT_ORIGINAL = 0; + /** Specifies locale-dependent timestamp format */ + public static final int FORMAT_LOCALE = 1; + /** Specifies ISO 8601 timestamp format */ + public static final int FORMAT_ISO_8601 = 2; + // Static block to initialise offsets static { @@ -43,16 +52,19 @@ public class Timestamp 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("yyyy MMM dd HH:mm:ss") + new SimpleDateFormat("yyyy MMM dd HH:mm:ss"), + ISO_8601_FORMAT }; } /** * Constructor + * @param inString String containing timestamp */ public Timestamp(String inString) { + // TODO: Does it really help to store timestamps in seconds rather than ms? if (inString != null && !inString.equals("")) { // Try to parse into a long @@ -82,10 +94,10 @@ public class Timestamp _seconds = rawValue / 1000L + TWENTY_YEARS_IN_SECS; smallestDiff = diff3; } - // Lastly, check garmin offset + // Lastly, check gartrip offset if (diff4 < smallestDiff) { - // milliseconds since garmin offset + // seconds since gartrip offset _seconds = rawValue + GARTRIP_OFFSET; } _valid = true; @@ -111,6 +123,41 @@ public class Timestamp } + /** + * Constructor giving each field value individually + * @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 + */ + public Timestamp(int inYear, int inMonth, int inDay, int inHour, int inMinute, int inSecond) + { + Calendar cal = Calendar.getInstance(); + 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); + cal.set(Calendar.MILLISECOND, 0); + _seconds = cal.getTimeInMillis() / 1000; + _valid = true; + } + + + /** + * Constructor giving millis + * @param inMillis milliseconds since 1970 + */ + public Timestamp(long inMillis) + { + _seconds = inMillis / 1000; + _valid = true; + } + + /** * @return true if timestamp is valid */ @@ -131,20 +178,88 @@ public class Timestamp } + /** + * Add the given TimeDifference to this Timestamp + * @param inOffset TimeDifference to add + * @return new Timestamp object + */ + public Timestamp addOffset(TimeDifference inOffset) + { + return new Timestamp((_seconds + inOffset.getTotalSeconds()) * 1000L); + } + + + /** + * Subtract the given TimeDifference from this Timestamp + * @param inOffset TimeDifference to subtract + * @return new Timestamp object + */ + public Timestamp subtractOffset(TimeDifference inOffset) + { + return new Timestamp((_seconds - inOffset.getTotalSeconds()) * 1000L); + } + + /** * @return Description of timestamp in locale-specific format */ public String getText() { + return getText(FORMAT_LOCALE); + } + + /** + * @param inFormat format of timestamp + * @return Description of timestamp in required format + */ + public String getText(int inFormat) + { + if (inFormat == FORMAT_ISO_8601) { + return format(ISO_8601_FORMAT); + } if (_text == null) { - if (_valid) - { - CALENDAR.setTimeInMillis(_seconds * 1000L); - _text = DEFAULT_DATE_FORMAT.format(CALENDAR.getTime()); + if (_valid) { + _text = format(DEFAULT_DATE_FORMAT); } else _text = ""; } return _text; } + + /** + * @return Description of time part of timestamp in locale-specific format + */ + public String getTimeText() + { + if (_timeText == null) + { + if (_valid) { + _timeText = format(DEFAULT_TIME_FORMAT); + } + else _timeText = ""; + } + return _timeText; + } + + /** + * Utility method for formatting dates / times + * @param inFormat formatter object + * @return formatted String + */ + private String format(DateFormat inFormat) + { + CALENDAR.setTimeInMillis(_seconds * 1000L); + return inFormat.format(CALENDAR.getTime()); + } + + /** + * @return a Calendar object representing this timestamp + */ + public Calendar getCalendar() + { + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(_seconds * 1000L); + return cal; + } }