From 52fa4d5ae447495ec50036436d1d46cae6c986dc Mon Sep 17 00:00:00 2001 From: Janus Varmarken Date: Mon, 23 Jul 2018 17:41:27 -0700 Subject: [PATCH] Added script for prepending dates to the timestamp files --- Code/Projects/DateWriter/DateWriter.java | 54 ++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Code/Projects/DateWriter/DateWriter.java diff --git a/Code/Projects/DateWriter/DateWriter.java b/Code/Projects/DateWriter/DateWriter.java new file mode 100644 index 0000000..adde143 --- /dev/null +++ b/Code/Projects/DateWriter/DateWriter.java @@ -0,0 +1,54 @@ +import java.io.File; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.PrintWriter; +import java.time.LocalDate; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; +import java.io.IOException; + +public class DateWriter { + + public static void main(String[] args) throws IOException { + if (args.length < 3) { + System.out.println("Usage: java /path/to/file/with/timestamps /path/to/new/timestamp/file/with/dates initial_date_in_uuuu-MM-dd_format"); + System.exit(1); + } + String pathOriginal = args[0]; + String pathModified = args[1]; + String initialDateStr = args[2]; + LocalDate date = LocalDate.parse(initialDateStr, DateTimeFormatter.ofPattern("uuuu-MM-dd")); + File originalFile = new File(pathOriginal); + // Create output file + File modifiedFile = new File(pathModified); + modifiedFile.createNewFile(); + BufferedReader reader = new BufferedReader(new FileReader(originalFile)); + PrintWriter writer = new PrintWriter(modifiedFile); + String line = null; + String prevLine = null; + while ((line = reader.readLine()) != null) { + if (isNewDay(line, prevLine)) { + // Advance date + date = date.plusDays(1); + } + writer.println(String.format("%s %s", date.toString(), line)); + prevLine = line; + } + writer.flush(); + writer.close(); + reader.close(); + } + + private static boolean isNewDay(String line, String prevLine) { + if (prevLine == null) { + return false; + } + // First part handles case where we pass midnight and the following timestamp is an AM timestamp + // Second case handles case where we pass midnight, but the following timestamp is a PM timestamp + return line.endsWith("AM") && prevLine.endsWith("PM") || toLocalTime(line).isBefore(toLocalTime(prevLine)); + } + + private static LocalTime toLocalTime(String timeString) { + return LocalTime.parse(timeString, DateTimeFormatter.ofPattern("h:mm:ss a")); + } +} \ No newline at end of file -- 2.34.1