File: Timestamp.java

package info (click to toggle)
gpsprune 10-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,220 kB
  • ctags: 3,013
  • sloc: java: 22,662; sh: 23; makefile: 16; python: 15
file content (283 lines) | stat: -rw-r--r-- 7,355 bytes parent folder | download
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
package tim.prune.data;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Class to hold the timestamp of a track point
 * and provide conversion functions
 */
public class Timestamp
{
	private boolean _valid = false;
	private long _seconds = 0L;
	private String _text = null;
	private String _timeText = null;

	private static final DateFormat DEFAULT_DATE_FORMAT = DateFormat.getDateTimeInstance();
	private static final DateFormat DEFAULT_TIME_FORMAT = DateFormat.getTimeInstance();
	private static final DateFormat ISO_8601_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
	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 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;

	/** 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
	{
		CALENDAR = Calendar.getInstance();
		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_DATE_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("yyyy MMM dd HH:mm:ss"),
			ISO_8601_FORMAT, ISO_8601_FORMAT_NOZ
		};
	}


	/**
	 * 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
			try
			{
				long rawValue = Long.parseLong(inString.trim());
				// check for each format possibility and pick nearest
				long diff1 = Math.abs(SECS_SINCE_1970 - rawValue);
				long diff2 = Math.abs(MSECS_SINCE_1970 - rawValue);
				long diff3 = Math.abs(MSECS_SINCE_1990 - rawValue);
				long diff4 = Math.abs(SECS_SINCE_GARTRIP - rawValue);

				// Start off with "seconds since 1970" format
				long smallestDiff = diff1;
				_seconds = rawValue;
				// Now check millis since 1970
				if (diff2 < smallestDiff)
				{
					// milliseconds since 1970
					_seconds = rawValue / 1000L;
					smallestDiff = diff2;
				}
				// Now millis since 1990
				if (diff3 < smallestDiff)
				{
					// milliseconds since 1990
					_seconds = rawValue / 1000L + TWENTY_YEARS_IN_SECS;
					smallestDiff = diff3;
				}
				// Lastly, check gartrip offset
				if (diff4 < smallestDiff)
				{
					// seconds since gartrip offset
					_seconds = rawValue + GARTRIP_OFFSET;
				}
				_valid = true;
			}
			catch (NumberFormatException nfe)
			{
				// String is not a long, so try a date/time string instead
				// try each of the date formatters in turn
				Date date = null;
				for (int i=0; i<ALL_DATE_FORMATS.length && !_valid; i++)
				{
					try
					{
						date = ALL_DATE_FORMATS[i].parse(inString);
						CALENDAR.setTime(date);
						_seconds = CALENDAR.getTimeInMillis() / 1000L;
						_valid = true;
					}
					catch (ParseException e) {}
				}
			}
		}
	}


	/**
	 * 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
	 */
	public boolean isValid()
	{
		return _valid;
	}

	/**
	 * @param inOther other Timestamp
	 * @return true if this one is after the other
	 */
	public boolean isAfter(Timestamp inOther)
	{
		return _seconds > inOther._seconds;
	}

	/**
	 * Calculate the difference between two Timestamps in seconds
	 * @param inOther other, earlier Timestamp
	 * @return number of seconds since other timestamp
	 */
	public long getSecondsSince(Timestamp inOther)
	{
		return _seconds - inOther._seconds;
	}

	/**
	 * Add the given number of seconds offset
	 * @param inOffset number of seconds to add/subtract
	 */
	public void addOffset(long inOffset)
	{
		_seconds += inOffset;
		_text = null;
	}

	/**
	 * Add the given TimeDifference to this Timestamp
	 * @param inOffset TimeDifference to add
	 * @return new Timestamp object
	 */
	public Timestamp createPlusOffset(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 createMinusOffset(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) {
				_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;
	}
}