Black Rock Reporting Azure Function
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Extensions.Logging;
  7. namespace BlackRockReportFunction.Helpers
  8. {
  9. public static class Formaters
  10. {
  11. public static string getSumOfRecordTimes(List<TimeOnly> recordTimes)
  12. {
  13. int totalHours = 0;
  14. int totalMinutes = 0;
  15. int totalSeconds = 0;
  16. // Calculate Seconds
  17. int totalRecordsSeconds = recordTimes.Sum(record => record.Second);
  18. totalSeconds = totalRecordsSeconds % 60;
  19. totalMinutes += totalRecordsSeconds / 60;
  20. // Calculate Minutes
  21. int totalRecordsMinutes = recordTimes.Sum(record => record.Minute);
  22. totalMinutes = totalMinutes + (totalRecordsMinutes % 60);
  23. totalHours += totalRecordsMinutes / 60;
  24. // Calculate Hours
  25. int totalRecordHours = recordTimes.Sum(record => record.Hour);
  26. totalHours += totalRecordHours;
  27. return string.Format("{0}:{1}:{2}",
  28. totalHours > 9 ? totalHours : string.Format("0{0}", totalHours),
  29. totalMinutes > 9 ? totalMinutes : string.Format("0{0}", totalMinutes),
  30. totalSeconds > 9 ? totalSeconds : string.Format("0{0}", totalSeconds));
  31. }
  32. public static string getDecimalHours(string recordHoursSum)
  33. {
  34. var components = recordHoursSum.Split(':').ToList();
  35. var hours = (components[0].Length == 2 && components[0].First() == '0')
  36. ? components[0].ToCharArray()[1].ToString()
  37. : components[0];
  38. var totalMinutes = Convert.ToDecimal(components[1]) + (Convert.ToDecimal(components[2]) / 60);
  39. var minutesPercent = Convert.ToInt32(Math.Floor(totalMinutes * 100 / 60)).ToString();
  40. if (minutesPercent.Length == 1)
  41. {
  42. minutesPercent = string.Format("0{0}", minutesPercent);
  43. }
  44. return string.Format("{0}:{1}", hours, minutesPercent);
  45. }
  46. public static string getDecimalHours(TimeOnly recordTime)
  47. {
  48. return getDecimalHours(string.Format("{0}:{1}:{2}", recordTime.Hour, recordTime.Minute, recordTime.Second));
  49. }
  50. public static string getRecordTime(TimeOnly recordTime)
  51. {
  52. return string.Format("{0}:{1}:{2}", recordTime.Hour, recordTime.Minute, recordTime.Second);
  53. }
  54. public static string getTotalSum(List<string> personsSums)
  55. {
  56. var totalHours = 0;
  57. var totalMinutes = 0;
  58. var totalSeconds = 0;
  59. foreach(var personSum in personsSums)
  60. {
  61. var components = personSum.Split(':').Select(Int32.Parse).ToList();
  62. //add new person time
  63. totalSeconds += components[2] % 60;
  64. totalMinutes += components[2] / 60;
  65. //clean up new sum
  66. totalMinutes += totalSeconds / 60;
  67. totalSeconds = totalSeconds % 60;
  68. //add new person time
  69. totalMinutes += components[1] % 60;
  70. totalHours += components[1] / 60;
  71. //clean up new time
  72. totalHours += totalMinutes / 60;
  73. totalMinutes = totalMinutes % 60;
  74. //add new person time
  75. totalHours += components[0];
  76. }
  77. return string.Format("{0}:{1}:{2}",
  78. totalHours > 9 ? totalHours : string.Format("0{0}", totalHours),
  79. totalMinutes > 9 ? totalMinutes : string.Format("0{0}", totalMinutes),
  80. totalSeconds > 9 ? totalSeconds : string.Format("0{0}", totalSeconds));
  81. }
  82. public static string getTotalDecimalHours(List<string> usersDecimalHours)
  83. {
  84. var minutesPercent = 0;
  85. var hours = 0;
  86. foreach(var userDecimalHours in usersDecimalHours)
  87. {
  88. var components = userDecimalHours.Split(':');
  89. minutesPercent += Convert.ToInt32(components[1]);
  90. hours += Convert.ToInt32(components[0]);
  91. }
  92. hours += minutesPercent / 100;
  93. minutesPercent = minutesPercent % 100;
  94. return string.Format("{0}:{1}", hours, minutesPercent);
  95. }
  96. }
  97. }