There is data from the СКУД . This is the время входа and время выхода . The working hours of the organization from 8 до 17:00 (lunch does not count) There are variables - таймаут1 - 25 минут and таймаут2 - 55 минут (I will explain below why they are). It is necessary to calculate the actual time of work. Hours of organization "Horns and hoofs" from 08:00 to 17:00

Suppose a man came at 8:01 and left at 17:40 . Since he arrived at 8:01 and the number of minutes is 01 < 25(таймаут1) , the entrance time will be 08:00 . Time out 17:40 . 40 > 25(таймаут1) but less than 55(таймаут2) - therefore round up to 30 (up to half an hour). It turns out that the actual time from 08:00 to 17:30 . This is the simplest algorithm and its transfer to the code will not be a problem.

Problems arise if there is a clock in the arrival or departure time.
Suppose if a person came at 10:25 and left at 16:45 . How to calculate its actual time?

  • I correctly understood that you need to fix the time of arrival and departure with an accuracy of 30 minutes? - rdorn
  • And for such cases (difference 2:25), the calculation algorithm is not described. How to count what is unknown how to count? - Akina
  • @rdorn, The commit time is the sum of the differences between the actual start time and the actual end time. Accuracy is not quite 30 minutes. Up to 25 minutes to round off in the direction of half an hour. And if there is time, let's say 45 minutes, then since it is more than half an hour, but less than the 2nd timeout - 55 minutes. Therefore, it is also "rounded" to half an hour - Radzhab
  • one
    According to your algorithm, the fact that it came at 10:25 and left at 16:45 means that the time of arrival and departure will be marked as 10:30 and 16:30. Next in TimeSpan and at the right intervals. Or something wrong? - Zufir

1 answer 1

Something like this

 DateTime time = DateTime.Now; DateTime rounded = time.AddMilliseconds(-time.Millisecond).AddSeconds(-time.Second); int minutes = rounded.Minute < 25 ? 0 : rounded.Minute > 55 ? 60 : 30; rounded = rounded.AddMinutes(minutes - rounded.Minute); 

The clock itself will increase if we add 60.

  • Thanks for the reply, Ruslan. Your code is just rounding off, but I would like to calculate the actual time. actual time = rounded exit time - rounded entry time - Rajab
  • @Radzhab and count, TimeSpan workingTime = time2 - time1; where time1, time2 is the rounded time - RusArt