Skip to content

Commit

Permalink
Merge pull request #1 from btison/parse-date
Browse files Browse the repository at this point in the history
Milliseconds of dates stored in postgresql are trimmed: '2019-05-03T2…
  • Loading branch information
btison authored May 3, 2019
2 parents 1bacd8e + 1aeb7e2 commit d17e9f3
Showing 1 changed file with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -21,6 +23,10 @@ public class RestApiVerticle extends AbstractVerticle {

FreeMarkerTemplateEngine engine;

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.S'Z'");

@Override
public Completable rxStart() {
return initializeHttpServer(config());
Expand Down Expand Up @@ -94,7 +100,6 @@ private void processInstanceData(RoutingContext rc) {

private JsonObject transformProcessData(JsonObject data) {

DateTimeFormatter in = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
DateTimeFormatter out = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

String mission = getVariableValue(data.getJsonArray("variables"), "mission");
Expand All @@ -105,18 +110,15 @@ private JsonObject transformProcessData(JsonObject data) {
.put("instanceId", Long.toString(data.getLong("processinstanceid")))
.put("processId", data.getString("processid"))
.put("status", status(data.getInteger("status")))
.put("startDate", out.format(LocalDateTime.from(in.parse(data.getString("start_date")))))
.put("endDate", data.getString("end_date") == null ? "" : out.format(LocalDateTime.from(in.parse(data.getString("end_date")))))
.put("startDate", out.format(LocalDateTime.from(parse(data.getString("start_date")))))
.put("endDate", data.getString("end_date") == null ? "" : out.format(LocalDateTime.from(parse(data.getString("end_date")))))
.put("duration", data.getLong("duration") == null ? "" : data.getLong("duration") / 1000)
.put("assignments_retries", getVariableValue(data.getJsonArray("variables"), "nrAssignments"))
.put("responder_id", match(".*responderId=([0-9]*),", mission))
.put("incident_location", coordinates(".*latitude=([-+]?[0-9.]*),",".*longitude=([-+]?[0-9.]*),", incident))
.put("responder_location",coordinates(".*responderLat=([-+]?[0-9.]*),",".*responderLong=([-+]?[0-9.]*),", mission))
.put("destination_location",coordinates(".*destinationLat=([-+]?[0-9.]*),",".*destinationLong=([-+]?[0-9.]*),", mission))
.put("image", data.getString("image"));


//responderId=87
}

private String status(int status) {
Expand Down Expand Up @@ -170,6 +172,16 @@ private String coordinates(String patternLat, String patternLon, String text) {
}
}

private TemporalAccessor parse(String date) {
TemporalAccessor ta;
try {
ta = dtf.parse(date);
} catch (DateTimeParseException e) {
ta = dtf2.parse(date);
}
return ta;
}



}

0 comments on commit d17e9f3

Please sign in to comment.