Skip to content

Commit afae67a

Browse files
authored
Merge pull request #1052 from WildMeOrg/861_time_math_is_hard
time math bug fix (for indexing)
2 parents d597216 + a41c99e commit afae67a

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/main/java/org/ecocean/Util.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ public static List<OptionDesc> findSamplingProtocols(String langCode, String con
164164
List<String> values = CommonConfiguration.getIndexedPropertyValues("samplingProtocol",
165165
context);
166166
List<OptionDesc> list = new ArrayList<OptionDesc>();
167-
168167
int valuesSize = values.size();
169168

170169
for (int i = 0; i < valuesSize; i++) {
@@ -597,7 +596,7 @@ public static Double getDecimalCoordFromString(String latOrLong) {
597596
return null;
598597
}
599598

600-
//GPS Longitude: "-69.0° 22.0' 45.62999999998169"",
599+
// GPS Longitude: "-69.0° 22.0' 45.62999999998169"",
601600
public static Double latlonDMStoDD(String dms) {
602601
String[] d = dms.split(" +");
603602

@@ -1120,7 +1119,7 @@ public static JSONObject copy(JSONObject original) {
11201119
public static long getVersionFromModified(final String modified) {
11211120
if (!stringExists(modified)) return 0;
11221121
try {
1123-
String iso8601 = getISO8601Date(modified);
1122+
String iso8601 = roundISO8601toMillis(getISO8601Date(modified));
11241123
if (iso8601 == null) return 0;
11251124
// switching from DateTimeFormatter to DateTime here because the math seems to line up with how psql does it -jon
11261125
return new DateTime(iso8601).getMillis();
@@ -1130,6 +1129,15 @@ public static long getVersionFromModified(final String modified) {
11301129
}
11311130
}
11321131

1132+
public static String roundISO8601toMillis(final String input) {
1133+
if ((input == null) || (input.length() < 24)) return input;
1134+
try {
1135+
float seconds = Float.parseFloat(input.substring(17)) + 0.0005f;
1136+
return input.substring(0, 17) + String.valueOf(seconds).substring(0, 6);
1137+
} catch (Exception ex) {}
1138+
return input;
1139+
}
1140+
11331141
// note: this respect user.receiveEmails - you have been warned
11341142
public static Set<String> getUserEmailAddresses(List<User> users) {
11351143
Set<String> addrs = new HashSet<String>();
@@ -1145,7 +1153,6 @@ public static Set<String> getUserEmailAddresses(List<User> users) {
11451153
public static String getISO8601Date(final String date) {
11461154
if (date == null) return null;
11471155
String iso8601 = date.replace(" ", "T");
1148-
11491156
if (iso8601.length() == 10) iso8601 += "T00:00:00";
11501157
// TODO: better testing of date's string format (or transition to date format support?)
11511158
if (iso8601.length() < 16) return null;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.ecocean;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.Assert.*;
5+
6+
class UtilTest {
7+
@Test void testRoundISO8601toMillis() {
8+
// should just passthru unchanged
9+
String testVal = "2024-10-28T16:36:56.656";
10+
11+
assertEquals(testVal, Util.roundISO8601toMillis(testVal));
12+
testVal = "2024-10-28T16:36:56";
13+
assertEquals(testVal, Util.roundISO8601toMillis(testVal));
14+
assertNull(Util.roundISO8601toMillis(null));
15+
16+
// this should round up
17+
testVal = "2024-10-28T16:36:56.656839";
18+
assertEquals("2024-10-28T16:36:56.657", Util.roundISO8601toMillis(testVal));
19+
// round down
20+
testVal = "2024-10-28T16:36:56.656039";
21+
assertEquals("2024-10-28T16:36:56.656", Util.roundISO8601toMillis(testVal));
22+
23+
// should fall thru due to exception in parsing float
24+
testVal = "2024-10-28T16:36:56.1ABC";
25+
assertEquals(testVal, Util.roundISO8601toMillis(testVal));
26+
}
27+
}

0 commit comments

Comments
 (0)