]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/Timestamp.java
Version 1, September 2006
[GpsPrune.git] / tim / prune / data / Timestamp.java
1 package tim.prune.data;
2
3 import java.text.DateFormat;
4 import java.text.ParseException;
5 import java.text.SimpleDateFormat;
6 import java.util.Calendar;
7 import java.util.Date;
8
9 /**
10  * Class to hold the timestamp of a track point
11  * and provide conversion functions
12  */
13 public class Timestamp
14 {
15         private boolean _valid = false;
16         private long _seconds = 0L;
17         private String _text = null;
18
19         private static DateFormat DEFAULT_DATE_FORMAT = DateFormat.getDateTimeInstance();
20         private static DateFormat[] ALL_DATE_FORMATS = null;
21         private static Calendar CALENDAR = null;
22         private static long SECS_SINCE_1970 = 0L;
23         private static long SECS_SINCE_GARTRIP = 0L;
24         private static long MSECS_SINCE_1970 = 0L;
25         private static long MSECS_SINCE_1990 = 0L;
26         private static long TWENTY_YEARS_IN_SECS = 0L;
27
28         private static final long GARTRIP_OFFSET = 631065600L;
29
30         // Static block to initialise offsets
31         static
32         {
33                 CALENDAR = Calendar.getInstance();
34                 MSECS_SINCE_1970 = CALENDAR.getTimeInMillis();
35                 SECS_SINCE_1970 = MSECS_SINCE_1970 / 1000L;
36                 SECS_SINCE_GARTRIP = SECS_SINCE_1970 - GARTRIP_OFFSET;
37                 CALENDAR.add(Calendar.YEAR, -20);
38                 MSECS_SINCE_1990 = CALENDAR.getTimeInMillis();
39                 TWENTY_YEARS_IN_SECS = (MSECS_SINCE_1970 - MSECS_SINCE_1990) / 1000L;
40                 // Date formats
41                 ALL_DATE_FORMATS = new DateFormat[] {
42                         DEFAULT_DATE_FORMAT,
43                         new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy"),
44                         new SimpleDateFormat("HH:mm:ss dd MMM yyyy"),
45                         new SimpleDateFormat("dd MMM yyyy HH:mm:ss"),
46                         new SimpleDateFormat("yyyy MMM dd HH:mm:ss")
47                 };
48         }
49
50
51         /**
52          * Constructor
53          */
54         public Timestamp(String inString)
55         {
56                 if (inString != null && !inString.equals(""))
57                 {
58                         // Try to parse into a long
59                         try
60                         {
61                                 long rawValue = Long.parseLong(inString.trim());
62                                 // check for each format possibility and pick nearest
63                                 long diff1 = Math.abs(SECS_SINCE_1970 - rawValue);
64                                 long diff2 = Math.abs(MSECS_SINCE_1970 - rawValue);
65                                 long diff3 = Math.abs(MSECS_SINCE_1990 - rawValue);
66                                 long diff4 = Math.abs(SECS_SINCE_GARTRIP - rawValue);
67
68                                 // Start off with "seconds since 1970" format
69                                 long smallestDiff = diff1;
70                                 _seconds = rawValue;
71                                 // Now check millis since 1970
72                                 if (diff2 < smallestDiff)
73                                 {
74                                         // milliseconds since 1970
75                                         _seconds = rawValue / 1000L;
76                                         smallestDiff = diff2;
77                                 }
78                                 // Now millis since 1990
79                                 if (diff3 < smallestDiff)
80                                 {
81                                         // milliseconds since 1990
82                                         _seconds = rawValue / 1000L + TWENTY_YEARS_IN_SECS;
83                                         smallestDiff = diff3;
84                                 }
85                                 // Lastly, check garmin offset
86                                 if (diff4 < smallestDiff)
87                                 {
88                                         // milliseconds since garmin offset
89                                         _seconds = rawValue + GARTRIP_OFFSET;
90                                 }
91                                 _valid = true;
92                         }
93                         catch (NumberFormatException nfe)
94                         {
95                                 // String is not a long, so try a date/time string instead
96                                 // try each of the date formatters in turn
97                                 Date date = null;
98                                 for (int i=0; i<ALL_DATE_FORMATS.length && !_valid; i++)
99                                 {
100                                         try
101                                         {
102                                                 date = ALL_DATE_FORMATS[i].parse(inString);
103                                                 CALENDAR.setTime(date);
104                                                 _seconds = CALENDAR.getTimeInMillis() / 1000L;
105                                                 _valid = true;
106                                         }
107                                         catch (ParseException e) {}
108                                 }
109                         }
110                 }
111         }
112
113
114         /**
115          * @return true if timestamp is valid
116          */
117         public boolean isValid()
118         {
119                 return _valid;
120         }
121
122
123         /**
124          * Calculate the difference between two Timestamps in seconds
125          * @param inOther other, earlier Timestamp
126          * @return number of seconds since other timestamp
127          */
128         public long getSecondsSince(Timestamp inOther)
129         {
130                 return _seconds - inOther._seconds;
131         }
132
133
134         /**
135          * @return Description of timestamp in locale-specific format
136          */
137         public String getText()
138         {
139                 if (_text == null)
140                 {
141                         if (_valid)
142                         {
143                                 CALENDAR.setTimeInMillis(_seconds * 1000L);
144                                 _text = DEFAULT_DATE_FORMAT.format(CALENDAR.getTime());
145                         }
146                         else _text = "";
147                 }
148                 return _text;
149         }
150 }