StoneHaos

Untitled

Jun 24th, 2022
1,328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp5 {
  8.     class Program {
  9.         static void Main(string[] args) {
  10.             TimeSpan begin = new TimeSpan(8, 0, 0);
  11.             TimeSpan end = new TimeSpan(18, 0, 0);
  12.             TimeSpan[] startTimes = new TimeSpan[] {
  13.                 new TimeSpan(10, 0, 0),
  14.                 new TimeSpan(12, 0, 0)
  15.             };
  16.             int[] durations = new int[] {
  17.                 60, 120
  18.             };
  19.             var a = Average(startTimes, durations, begin, end, 30);
  20.             foreach (var b in a)
  21.                 Console.WriteLine(b);
  22.         }
  23.  
  24.         static string[] Average(TimeSpan[] startTimes, int[] durations, TimeSpan beginWorkingTime, TimeSpan endWorkingTime, int consultation) {
  25.             List<TimeSpan> lst = new List<TimeSpan>();
  26.             TimeSpan current = Copy(beginWorkingTime);
  27.             for (int i = 0; i < durations.Length; ++ i) {
  28.                 while (current + new TimeSpan(0, consultation, 0) <= startTimes[i]) {
  29.                     lst.Add(Copy(current));
  30.                     current += new TimeSpan(0, consultation, 0);
  31.                 }
  32.                 current = startTimes[i] + new TimeSpan(0, durations[0], 0);
  33.             }
  34.             while (current + new TimeSpan(0, consultation, 0) <= endWorkingTime) {
  35.                 lst.Add(Copy(current));
  36.                 current += new TimeSpan(0, consultation, 0);
  37.             }
  38.             List<string> ret = new List<string>();
  39.             foreach (var a in lst) {
  40.                 TimeSpan p = a + new TimeSpan(0, consultation, 0);
  41.                 DateTime x = new DateTime(1970, 1, 1, a.Hours, a.Minutes, a.Seconds);
  42.                 DateTime y = new DateTime(1970, 1, 1, p.Hours, p.Minutes, p.Seconds);
  43.                 ret.Add($"{x.ToString("HH:mm")}-{y.ToString("HH:mm")}");
  44.             }
  45.             return ret.ToArray();
  46.         }
  47.  
  48.         static TimeSpan Copy(TimeSpan time) {
  49.             return new TimeSpan(time.Hours, time.Minutes, time.Seconds);
  50.         }
  51.     }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment