private List <UpworkTimeEntry> CheckOverlap(List <UpworkTimeEntry> a_entries) { List <UpworkTimeEntry> result = new List <UpworkTimeEntry>(); if (a_entries.Count > 1) { UpworkTimeEntry currentEntry = a_entries[0]; result.Add(currentEntry); // Step 3 check if start is same as end for (int i = 1; i < a_entries.Count; ++i) { UpworkTimeEntry entry = a_entries[i]; // Check if the currentEntry ends where this new entry starts. // 15:00 == 15:00 if (currentEntry.End == entry.Start) { string current = String.Format("{0} - {1}", currentEntry.Start.ToString("HH:mm"), currentEntry.End.ToString("HH:mm")); string merged = String.Format("{0} - {1}", entry.Start.ToString("HH:mm"), entry.End.ToString("HH:mm")); currentEntry.End = entry.End; currentEntry.DurationSeconds += entry.DurationSeconds; Program.WriteMessage("Merged 2 times -- ", ConsoleColor.Yellow); Program.WriteMessage(current + " and ", ConsoleColor.Cyan); Program.WriteLineMessage(merged, ConsoleColor.Magenta); } // This should never happen unless the file has been manually edited // 15:00 > 14:50 else if (currentEntry.End > entry.Start) { Program.WriteLineMessage("END > START !! - " + currentEntry.DurationSeconds + " " + entry.DurationSeconds, ConsoleColor.Yellow); } // If End < Start this should be most cases else { currentEntry = entry; // Don't forget to add the entry to the result result.Add(entry); } } } else { result = a_entries; } return(result); }
public List <UpworkTimeEntry> FixEntries(List <UpworkTimeEntry> a_entries) { if (a_entries.Count == 0) { return(a_entries); } /* * 1: Remove all entries with less than 10 minute durations * 2: Make all times in 10 minute intervals 14:52 - 15.02 => 14:50 - 15.00 * 3: Check if Times are overlapping and if they are merge * 4: Add Rest time */ List <UpworkTimeEntry> result = new List <UpworkTimeEntry>(); // Step 1: Remove all entries with less than 10 minute duration and add it to restSeconds for (int i = 0; i < a_entries.Count; i++) { UpworkTimeEntry entry = a_entries[i]; if (entry.DurationSeconds < 600) { RestSeconds += entry.DurationSeconds; RestSeconds += entry.RestSeconds; } else { // Step 2 Move times to even blocks // Do seconds int seconds = entry.Start.Second; //restSeconds += seconds; entry.Start = entry.Start.AddSeconds(-seconds); entry.End = entry.End.AddSeconds(-seconds); //entry.RestSeconds += seconds; // Minutes int minuteOverflow = entry.Start.Minute % 10; entry.Start = entry.Start.AddMinutes(-minuteOverflow); entry.End = entry.End.AddMinutes(-minuteOverflow); result.Add(entry); RestSeconds += entry.RestSeconds; entry.RestSeconds = 0; } } // If all entries were removed if (result.Count == 0) { result.Add(a_entries[0]); } result = CheckOverlap(result); int restAdds = (int)(RestSeconds / 600); // Add 10 minutes to each entry and then repeat until there are no more entries to add to. while (restAdds > 0) { foreach (UpworkTimeEntry entry in result) { entry.End = entry.End.AddMinutes(10); entry.DurationSeconds += 600; RestSeconds -= 600; restAdds--; if (restAdds <= 0) { break; } } } result = CheckOverlap(result); return(result); }
/// <summary> /// Takes a folder path in the form of "u {folder}" /// </summary> /// <param name="a_folder"></param> public static void PrintTimes(string a_folder) { if (a_folder.Length <= 2) { Program.WriteLineMessage("expected input: 'u folder'", ConsoleColor.Red); return; } // Remove the "u " part. string folder = a_folder.Substring(2); try { IEnumerable <string> files = Directory.EnumerateFiles(folder); UpworkTimeFixer timeFixer = new UpworkTimeFixer(); double totalTime = 0; foreach (string filename in files) { string date = filename.Substring(folder.Length + 1).Split('.')[0]; Program.WriteLineMessage("Date: " + date, ConsoleColor.Yellow); using (StreamReader sr = new StreamReader(filename)) { DateTime start; TimeSpan duration; double dailyTotal = 0; double restTotal = 0; List <UpworkTimeEntry> entries = new List <UpworkTimeEntry>(); while (!sr.EndOfStream) { string line = sr.ReadLine(); if (line.StartsWith("start")) { string startStr = line.Split(' ')[1]; start = DateTime.Parse(startStr); // Read next line duration = TimeSpan.Parse(sr.ReadLine().Split(' ')[1]); UpworkTimeEntry entry = new UpworkTimeEntry(start, duration); entries.Add(entry); } } if (entries.Count > 0) { // BUG if 1 entry has less than 10 minutes work then the totalRest will be f****d entries = timeFixer.FixEntries(entries); foreach (UpworkTimeEntry entry in entries) { entry.PrintTime(); dailyTotal += entry.DurationSeconds; } restTotal = timeFixer.RestSeconds; int hours = (int)(dailyTotal / 3600); int minutes = (int)((dailyTotal - (hours * 3600)) / 60); Program.WriteLineMessage("Daily total: " + hours + "h " + minutes + "m", ConsoleColor.Cyan); hours = (int)(restTotal / 3600); minutes = (int)((restTotal - (hours * 3600)) / 60); Program.WriteLineMessage("Daily overflow:" + hours + "h " + minutes + "m", ConsoleColor.Red); totalTime += dailyTotal; Console.WriteLine(); } else { Console.WriteLine("NO ENTRIES FOUND"); } } } int totalHours = (int)(totalTime / 3600); int totalMinutes = (int)((totalTime - (totalHours * 3600)) / 60); Program.WriteLineMessage("Total time: " + totalHours + "h " + totalMinutes + "m", ConsoleColor.Magenta); totalHours = (int)(timeFixer.RestSeconds / 3600); totalMinutes = (int)((timeFixer.RestSeconds - (totalHours * 3600)) / 60); Program.WriteLineMessage("Total overflow:" + totalHours + "h " + totalMinutes + "m", ConsoleColor.DarkGreen); } catch (Exception e) { Program.WriteLineMessage("Unexpected error: " + e.Message, ConsoleColor.Red); } }