private void LoadFromFile() { using (TextReader reader = new StreamReader(FileName)) { for (; ;) { ClockEvent clockEvent = ClockEvent.Read(reader); if (clockEvent == null) { break; } if (clockEvent.Status == EventStatus.Deleted) { // Older versions of this software changed the old ClockEvent in the file // when it was deleted, but new versions simply append a delete record to // the file and let this routine match the delete record with the original // record. If Delete() returns true then it found an original record matching // the date and time and marked it as deleted, which means it was a new style // delete, otherwise the delete record is an old style one and we add it to // the list. if (!Delete(clockEvent)) { _Events.AddLast(clockEvent); } } else { _Events.AddLast(clockEvent); } } } }
public void SaveToFile(ClockEvent newEvent) { using (TextWriter writer = new StreamWriter(FileName, true)) { newEvent.Write(writer); } }
public ClockEvent ClockExtra(DateTime when) { ClockEvent newEvent = new ClockEvent(ClockEvent.Round(when), DateTime.Now, EventStatus.Extra); _Events.AddLast(newEvent); return(newEvent); }
public ClockEvent ClockInOut() { ClockEvent newEvent = new ClockEvent(ClockEvent.Round(DateTime.Now), DateTime.Now, EventStatus.Normal); _Events.AddLast(newEvent); return(newEvent); }
public ClockEvent ClockInOut(DateTime when) { ClockEvent newEvent = new ClockEvent(ClockEvent.Round(when), DateTime.Now, EventStatus.Overridden); _Events.AddLast(newEvent); return(newEvent); }
private void btnClockNow_Click(object sender, EventArgs e) { ClockEvent newEvent = _Times.ClockInOut(); _Times.SaveToFile(newEvent); ShowTimeCard(); txtSpecificDateTime.Text = newEvent.InOutDateTime.ToString("MM/dd/yyyy hh:mmtt"); }
private bool Delete(ClockEvent newEvent) { foreach (ClockEvent clockEvent in _Events) { if (clockEvent.InOutDateTime == newEvent.InOutDateTime && clockEvent.Status != EventStatus.Deleted) { clockEvent.Status = EventStatus.Deleted; return(true); } } return(false); }
private void btnAddExtra_Click(object sender, EventArgs e) { DateTime when; if (!TryParseDateTime(out when)) { return; } ClockEvent newEvent = _Times.ClockExtra(when); _Times.SaveToFile(newEvent); ShowTimeCard(); }
public TimePair(ClockEvent startEvent, ClockEvent endEvent) { StartEvent = startEvent; EndEvent = endEvent; if (EndEvent != null) { Length = EndEvent.InOutDateTime.Subtract(StartEvent.InOutDateTime); IsOpen = false; } else { Length = new TimeSpan(0); IsOpen = true; } }
public void Get(PayrollPeriod period, out List <TimePair> timePairs, out double overtimeHours, out List <TimePair> absentPairs) { timePairs = new List <TimePair>(); absentPairs = new List <TimePair>(); ClockEvent startEvent = null; List <ClockEvent> eventsInPeriod = new List <ClockEvent>(); foreach (ClockEvent clockEvent in _Events) { if (clockEvent.InOutDateTime.Date >= period.StartDate && clockEvent.InOutDateTime.Date <= period.EndDate && clockEvent.Status != EventStatus.Deleted) { eventsInPeriod.Add(clockEvent); } } eventsInPeriod.Sort(delegate(ClockEvent event1, ClockEvent event2) { return(event1.InOutDateTime.CompareTo(event2.InOutDateTime)); }); foreach (ClockEvent clockEvent in eventsInPeriod) { if (startEvent == null) { startEvent = clockEvent; } else { if (startEvent.InOutDateTime.Date == clockEvent.InOutDateTime.Date) { AddPair(timePairs, absentPairs, startEvent, clockEvent); startEvent = null; } else { // pair cross a date boundary, so leave an open pair in the old date and start a new pair. AddPair(timePairs, absentPairs, startEvent, null); startEvent = clockEvent; } } } if (startEvent != null) { AddPair(timePairs, absentPairs, startEvent, null); } overtimeHours = ComputeOvertime(period, timePairs); }
private string FormatTimeOfDay(ClockEvent clockEvent) { string result = clockEvent.InOutDateTime.ToString("hh:mmtt"); if (clockEvent.Status == EventStatus.Overridden) { result = "<" + result + ">"; } else if (clockEvent.Status == EventStatus.Absent) { result = "{" + result + "}"; } else if (clockEvent.Status == EventStatus.Extra) { result = "[" + result + "]"; } return(result); }
public static ClockEvent Read(TextReader reader) { string line = reader.ReadLine(); if (line == null) { return(null); } string[] parts = line.Split('|'); DateTime inOutDateTime; DateTime actionDateTime; EventStatus status; if (!DateTime.TryParse(parts[0], out inOutDateTime)) { throw new InvalidDataException("Invalid in/out datetime in " + line); } if (!DateTime.TryParse(parts[1], out actionDateTime)) { throw new InvalidDataException("Invalid action datetime in " + line); } switch (parts[2]) { case "N": status = EventStatus.Normal; break; case "O": status = EventStatus.Overridden; break; case "D": status = EventStatus.Deleted; break; case "A": status = EventStatus.Absent; break; case "X": status = EventStatus.Extra; break; default: throw new InvalidDataException("Invalid status in " + line); } ClockEvent clockEvent = new ClockEvent(inOutDateTime, actionDateTime, status); return(clockEvent); }
public bool Delete(DateTime when, out ClockEvent newEvent) { newEvent = new ClockEvent(ClockEvent.Round(when), DateTime.Now, EventStatus.Deleted); return(Delete(newEvent)); }
private static void AddPair(List <TimePair> timePairs, List <TimePair> absentPairs, ClockEvent startEvent, ClockEvent endEvent) { TimePair pair = new TimePair(startEvent, endEvent); if (pair.IsAbsent) { absentPairs.Add(pair); return; } timePairs.Add(pair); }