@@ -45,11 +45,9 @@ public class DateTimeStamp implements Comparable<DateTimeStamp> {
45
45
private static final DateTimeFormatter formatter = DateTimeFormatter .ofPattern ("yyyy-MM-dd'T'HH:mm:ss.SSSZ" );
46
46
47
47
private static ZonedDateTime dateFromString (String iso8601DateTime ) {
48
- if (iso8601DateTime != null ) {
49
- TemporalAccessor temporalAccessor = formatter .parse (iso8601DateTime );
50
- return ZonedDateTime .from (temporalAccessor );
51
- }
52
- return null ;
48
+ if ( iso8601DateTime == null )
49
+ return null ;
50
+ return ZonedDateTime .from (formatter .parse (iso8601DateTime ));
53
51
}
54
52
55
53
private static double ageFromString (String doubleFormat ) {
@@ -151,6 +149,8 @@ public DateTimeStamp(ZonedDateTime dateTime, double timeStamp) {
151
149
* @return The time stamp value, in decimal seconds.
152
150
*/
153
151
public double getTimeStamp () {
152
+ if (! hasTimeStamp ())
153
+ return toEpochInMillis ();
154
154
return timeStamp ;
155
155
}
156
156
@@ -179,14 +179,11 @@ public boolean hasTimeStamp() {
179
179
public boolean equals (Object obj ) {
180
180
if (obj instanceof DateTimeStamp ) {
181
181
DateTimeStamp other = (DateTimeStamp ) obj ;
182
- boolean hasTimeStamp = hasTimeStamp () && other .hasTimeStamp ();
183
- boolean hasDateStamp = hasDateStamp () && other .hasDateStamp ();
184
- if (hasDateStamp && hasTimeStamp )
185
- return equals (other .getTimeStamp ()) && getDateTime ().equals (other .getDateTime ());
186
- if (hasTimeStamp )
187
- return equals (other .getTimeStamp ());
188
- if (hasDateStamp )
189
- return getDateTime ().equals (other .getDateTime ());
182
+ if (this .hasDateStamp ())
183
+ return this .getDateTime ().equals (other .getDateTime ()) &&
184
+ (this .getTimeStamp () == other .getTimeStamp ());
185
+ else
186
+ return getTimeStamp () == other .getTimeStamp ();
190
187
}
191
188
return false ;
192
189
}
@@ -202,47 +199,17 @@ public String toString() {
202
199
if (hasDateStamp ())
203
200
buffer .append (getDateTime ().toString ());
204
201
if (hasTimeStamp ())
205
- buffer .append ("@" ).append (String .format (Locale .US ,"%.3f" ,timeStamp ));
202
+ buffer .append ("@" ).append (String .format (Locale .US ,"%.3f" ,getTimeStamp () ));
206
203
return buffer .toString ();
207
204
}
208
205
209
- /**
210
- * Return {@code true} if this time stamp is less than the other.
211
- * @param other The other time stamp, in decimal seconds.
212
- * @return {@code true} if this time stamp is less than the other.
213
- */
214
- public boolean before (double other ) {
215
- if ( other < 0.000d || Double .isNaN (other )) return false ;
216
- if ( Double .isNaN (getTimeStamp ())) return false ;
217
- return getTimeStamp () < other ;
218
- }
219
-
220
- /**
221
- * Return {@code true} if this time stamp is greater than the other.
222
- * @param other The other time stamp, in decimal seconds.
223
- * @return {@code true} if this time stamp is greater than the other.
224
- */
225
- public boolean after (double other ) {
226
- if ( other < 0.000d || Double .isNaN (other )) return false ;
227
- if ( Double .isNaN (getTimeStamp ())) return false ;
228
- return getTimeStamp () > other ;
229
- }
230
-
231
- public boolean equals (double other ) {
232
- if ( other < 0.000d || Double .isNaN (other )) return false ;
233
- if ( Double .isNaN (getTimeStamp ())) return false ;
234
- return getTimeStamp () == other ;
235
- }
236
-
237
206
/**
238
207
* Return {@code true} if this DateTimeStamp comes before the other.
239
208
* @param other The other DateTimeStamp.
240
209
* @return {@code true} if this time stamp is less than the other.
241
210
*/
242
211
public boolean before (DateTimeStamp other ) {
243
- return ( hasTimeStamp () && other .hasTimeStamp ())
244
- ? before (other .getTimeStamp ())
245
- : compare (other .getDateTime ()) < 0 ;
212
+ return getTimeStamp () < other .getTimeStamp ();
246
213
}
247
214
248
215
/**
@@ -251,9 +218,7 @@ public boolean before(DateTimeStamp other) {
251
218
* @return {@code true} if this time stamp is less than the other.
252
219
*/
253
220
public boolean after (DateTimeStamp other ) {
254
- return ( hasTimeStamp () && other .hasTimeStamp ())
255
- ? after (other .getTimeStamp ())
256
- : compare (other .getDateTime ()) > 0 ;
221
+ return getTimeStamp () > other .getTimeStamp ();
257
222
}
258
223
259
224
/**
@@ -284,26 +249,20 @@ public DateTimeStamp add(double offsetInDecimalSeconds) {
284
249
if (Double .isNaN (offsetInDecimalSeconds ))
285
250
throw new IllegalArgumentException ("Cannot add " + Double .NaN );
286
251
287
- DateTimeStamp now ;
288
252
double adjustedTimeStamp = Double .NaN ;
289
- if ( ! Double .isNaN (getTimeStamp ())) {
253
+ ZonedDateTime adjustedDateStamp = null ;
254
+ if ( hasTimeStamp ()) {
290
255
adjustedTimeStamp = getTimeStamp () + offsetInDecimalSeconds ;
291
256
}
292
257
293
- // Need to scale the values from ZonedDateTime
294
- if (getDateTime () != null ) {
258
+ if (hasDateStamp ()) {
295
259
double offset = (Double .isNaN (offsetInDecimalSeconds )) ? 0.000d : offsetInDecimalSeconds ;
296
260
int seconds = (int ) offset ;
297
261
long nanos = ((long ) ((offset % 1 ) * 1_000L )) * 1_000_000L ;
298
- ZonedDateTime adjustedDateStamp = dateTime .plusSeconds (seconds ).plusNanos (nanos );
299
- if ( Double .isNaN (getTimeStamp ()))
300
- now = new DateTimeStamp (adjustedDateStamp );
301
- else
302
- now = new DateTimeStamp (adjustedDateStamp , adjustedTimeStamp );
303
- } else
304
- now = new DateTimeStamp (adjustedTimeStamp );
262
+ adjustedDateStamp = dateTime .plusSeconds (seconds ).plusNanos (nanos );
263
+ }
305
264
306
- return now ;
265
+ return new DateTimeStamp ( adjustedDateStamp , adjustedTimeStamp ) ;
307
266
}
308
267
309
268
/**
0 commit comments