Advertisement
Guest User

Untitled

a guest
May 25th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. namespace WorktimeCollector
  9. {
  10. class Program
  11. {
  12. static int days = 7;
  13.  
  14. static int pause = 30;
  15.  
  16. static void Main(string[] args)
  17. {
  18. if (args.Length > 0 && !string.IsNullOrEmpty(args[0]))
  19. {
  20. try
  21. {
  22. days = Convert.ToInt32(args[0]);
  23. }
  24. catch
  25. {
  26. PrintHelpText();
  27. return;
  28. }
  29. }
  30.  
  31. Console.WriteLine("parsing Eventlogs...\n");
  32. var allDates = GetEventLogDatesFromLastNDays(days);
  33. var dates = new List<Day>();
  34.  
  35. for (int i = 0; i < days - 1; i++)
  36. {
  37. var eventsOnDay = allDates.Where(x => x > DateTime.Today.AddDays(-i) && x < DateTime.Today.AddDays(-(i - 1)));
  38. if (eventsOnDay.FirstOrDefault() != default(DateTime))
  39. {
  40. dates.Add(new Day(eventsOnDay.Last(), eventsOnDay.First()));
  41. }
  42. }
  43.  
  44. var currentWeek = new TimeSpan();
  45. var currentWeeksPause = new TimeSpan();
  46. for (int i = 0; i < dates.Count; i++)
  47. {
  48. var date = dates[i];
  49. if (date.Start.Date == DateTime.Today)
  50. {
  51. Console.WriteLine("Today Begin: " + date.StartString());
  52. Console.WriteLine("Today Time: " + TimeSpanToString(DateTime.Now.Subtract(date.Start)));
  53. }
  54. else
  55. {
  56. Console.WriteLine(date + " Begin: " + date.StartString());
  57. Console.WriteLine(date + " End: " + date.EndString());
  58. Console.WriteLine(date + " Time: " + date.TimeString());
  59. }
  60.  
  61. currentWeek = currentWeek.Add(date.Time);
  62. currentWeeksPause = currentWeeksPause.Add(new TimeSpan(0, pause, 0));
  63.  
  64. var weekNumber = WeekOfYear(date.Start);
  65. var nextWeekNumber = i + 1 < dates.Count ? WeekOfYear(dates[i + 1].Start) : weekNumber;
  66. if (weekNumber != nextWeekNumber)
  67. {
  68. Console.WriteLine();
  69. Console.WriteLine("Week " + weekNumber + " (" + GetWeekSpan(date.Start) + "): " + TimeSpanToString(currentWeek));
  70. Console.WriteLine("Week " + weekNumber + " - Pause " + TimeSpanToString(currentWeeksPause) + ": " + TimeSpanToString(currentWeek.Subtract(currentWeeksPause)));
  71. currentWeek = new TimeSpan();
  72. currentWeeksPause = new TimeSpan();
  73. }
  74.  
  75. Console.WriteLine();
  76. }
  77. }
  78.  
  79. private static string GetWeekSpan(DateTime date)
  80. {
  81. return GetFirstDayOfWeek(date).ToString("dd.MM") + " - " + GetLastDayOfWeek(date).ToString("dd.MM");
  82. }
  83.  
  84. private static DateTime GetFirstDayOfWeek(DateTime date)
  85. {
  86. var firstDayInWeek = date;
  87. while (WeekOfYear(firstDayInWeek) == WeekOfYear(date))
  88. {
  89. firstDayInWeek = firstDayInWeek.AddDays(-1);
  90. }
  91. return firstDayInWeek.AddDays(1);
  92. }
  93.  
  94. private static DateTime GetLastDayOfWeek(DateTime date)
  95. {
  96. var LastDayInWeek = date;
  97. while (WeekOfYear(LastDayInWeek) == WeekOfYear(date))
  98. {
  99. LastDayInWeek = LastDayInWeek.AddDays(+1);
  100. }
  101. return LastDayInWeek.AddDays(-1);
  102. }
  103.  
  104. private static string TimeSpanToString(TimeSpan span)
  105. {
  106. return string.Format("{0:00}:{1:00}", (int)span.TotalMinutes / 60, (int)span.TotalMinutes % 60);
  107. }
  108.  
  109. private static int WeekOfYear(DateTime date)
  110. {
  111. return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
  112. }
  113.  
  114. private static List<DateTime> GetEventLogDatesFromLastNDays(int days)
  115. {
  116. var minDay = DateTime.Today.AddDays(-days).ToString(@"yyyy-MM-ddTHH\:mm\:ss");
  117. var times = new List<DateTime>();
  118.  
  119. var proc = new Process
  120. {
  121. StartInfo = new ProcessStartInfo
  122. {
  123. FileName = "wevtutil.exe",
  124. Arguments = @"qe System /rd:true /f:text /q:""*[System[TimeCreated[@SystemTime >= '" + minDay + @"']]]""",
  125. UseShellExecute = false,
  126. RedirectStandardOutput = true,
  127. CreateNoWindow = true
  128. }
  129. };
  130. proc.Start();
  131.  
  132. string time = string.Empty;
  133. while (!proc.StandardOutput.EndOfStream)
  134. {
  135. string line = proc.StandardOutput.ReadLine();
  136. if (line.StartsWith(" Date:"))
  137. {
  138. time = line.Substring(8);
  139. times.Add(DateTime.Parse(time));
  140. }
  141. }
  142.  
  143. return times;
  144. }
  145.  
  146. private static void PrintHelpText()
  147. {
  148. Console.WriteLine();
  149. Console.WriteLine("Usage Examples:");
  150. Console.WriteLine(" WorktimeCollector.exe \t\t... Show Worktime for last " + days + " days");
  151. Console.WriteLine(" WorktimeCollector.exe 14 \t\t... Show Worktime for last 2 weeks");
  152. Console.WriteLine(" WorktimeCollector.exe 365 \t\t... Show Worktime for last year");
  153. Console.WriteLine();
  154. Console.WriteLine("How it work:");
  155. Console.WriteLine(" WorktimeCollector crunches the systems Eventlog entries and");
  156. Console.WriteLine(" looks for the first and last entry for each day. It then takes");
  157. Console.WriteLine(" the difference and prints them out.");
  158. }
  159. }
  160. }
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169. using System;
  170. using System.Collections.Generic;
  171. using System.Linq;
  172. using System.Text;
  173.  
  174. namespace WorktimeCollector
  175. {
  176. public class Day
  177. {
  178. public DateTime Start { get; set; }
  179.  
  180. public DateTime End { get; set; }
  181.  
  182. public TimeSpan Time
  183. {
  184. get { return this.End.Subtract(this.Start); }
  185.  
  186. }
  187.  
  188. public Day(DateTime start, DateTime end)
  189. {
  190. this.Start = start;
  191. this.End = end;
  192. }
  193.  
  194. public override string ToString()
  195. {
  196. return this.Start.ToString("dd.MM.yyyy");
  197. }
  198.  
  199. public string StartString()
  200. {
  201. return this.Start.ToString("HH:mm");
  202. }
  203.  
  204. public string EndString()
  205. {
  206. return this.End.ToString("HH:mm");
  207. }
  208.  
  209. public string TimeString()
  210. {
  211. return new DateTime(this.Time.Ticks).ToString("HH:mm");
  212. }
  213. }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement