Black Rock Reporting Azure Function
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 minutesPercent = (Convert.ToInt32(components[1]) * 100 / 60).ToString();
  39. if (minutesPercent.Length == 1)
  40. {
  41. minutesPercent = string.Format("0{0}", minutesPercent);
  42. }
  43. return string.Format("{0}:{1}", hours, minutesPercent);
  44. }
  45. public static string getDecimalHours(TimeOnly recordTime)
  46. {
  47. return getDecimalHours(string.Format("{0}:{1}:{2}", recordTime.Hour, recordTime.Minute, recordTime.Second));
  48. }
  49. public static string getRecordTime(TimeOnly recordTime)
  50. {
  51. return string.Format("{0}:{1}:{2}", recordTime.Hour, recordTime.Minute, recordTime.Second);
  52. }
  53. public static string getTotalSum(List<string> personsSums)
  54. {
  55. var totalHours = 0;
  56. var totalMinutes = 0;
  57. var totalSeconds = 0;
  58. foreach(var personSum in personsSums)
  59. {
  60. var components = personSum.Split(':').Select(Int32.Parse).ToList();
  61. //add new person time
  62. totalSeconds += components[2] % 60;
  63. totalMinutes += components[2] / 60;
  64. //clean up new sum
  65. totalMinutes += totalSeconds / 60;
  66. totalSeconds = totalSeconds % 60;
  67. //add new person time
  68. totalMinutes += components[1] % 60;
  69. totalHours += components[1] / 60;
  70. //clean up new time
  71. totalHours += totalMinutes / 60;
  72. totalMinutes = totalMinutes % 60;
  73. //add new person time
  74. totalHours += components[0];
  75. }
  76. return string.Format("{0}:{1}:{2}",
  77. totalHours > 9 ? totalHours : string.Format("0{0}", totalHours),
  78. totalMinutes > 9 ? totalMinutes : string.Format("0{0}", totalMinutes),
  79. totalSeconds > 9 ? totalSeconds : string.Format("0{0}", totalSeconds));
  80. }
  81. }
  82. }