Skip to content

Instantly share code, notes, and snippets.

@kishida
Created Jul 25, 2021
Embed
What would you like to do?
Shift GPS timestamp in nmea
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class FixGPS {
static String sample = """
@Sonygps/ver5.0/wgs-84/20210718015210.000/
@Sonygpsoption/0/20210718015213.000/20210718015212.544/
$GPGGA,015212.544,3326.5546,N,13031.8540,E,1,0,,,M,,M,,*7D
$GPRMC,015212.544,A,3326.5546,N,13031.8540,E,0.31,,180721,,,A*70
""";
public static void main(String args[]) throws IOException {
var org = Path.of("V:\\as100\\PRIVATE\\SONY\\GPS\\21071801.LOG");
var fix = Path.of("V:\\as100\\PRIVATE\\SONY\\GPS\\21071801fix.nmea");
try (var r = Files.newBufferedReader(org);
var w = Files.newBufferedWriter(fix)) {
var d = Duration.ofMinutes(23).plusSeconds(37);
convert(r, w, d);
}
}
static void convert(BufferedReader br, Writer out, Duration d) throws IOException {
var top = br.readLine();
var strs = top.split("/");
dateTimePlus(strs, 3, d);
out.write(String.join("/", strs) + "/\r\n");
var sec = br.readLine();
strs = sec.split("/");
dateTimePlus(strs, 2, d);
dateTimePlus(strs, 3, d);
out.write(String.join("/", strs) + "/\r\n");
for (String line; (line = br.readLine()) != null; ) {
strs = line.split(",");
timePlus(strs, 1, d);
out.write(String.join(",", strs) + "\r\n");
}
}
static void dateTimePlus(String[] strs, int index, Duration d) {
var pat = DateTimeFormatter.ofPattern("yyyyMMddHHmmss.SSS");
var dt = LocalDateTime.parse(strs[index], pat);
strs[index] = pat.format(dt.plus(d));
}
static void timePlus(String[] strs, int index, Duration d) {
var pat = DateTimeFormatter.ofPattern("HHmmss.SSS");
var t = LocalTime.parse(strs[index], pat);
strs[index] = pat.format(t.plus(d));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment