Esempio n. 1
0
        public void   排除時間_結束時間相同()
        {
            var sourcePeriod = new DurationDto(new DateTime(2020, 2, 3, 8, 0, 0),
                                               new DateTime(2020, 2, 3, 17, 0, 0));

            var exceptPeriods = new[]
            {
                new DurationDto(new DateTime(2020, 2, 3, 8, 5, 0),
                                new DateTime(2020, 2, 3, 9, 5, 0)),
                new DurationDto(new DateTime(2020, 2, 3, 10, 10, 0),
                                new DateTime(2020, 2, 3, 11, 0, 0)),
                new DurationDto(new DateTime(2020, 2, 3, 14, 0, 0),
                                new DateTime(2020, 2, 3, 17, 0, 0)),
            };

            var actual   = sourcePeriod.Except(exceptPeriods).ToArray();
            var expected = new[]
            {
                new DurationDto(new DateTime(2020, 2, 3, 8, 0, 0),
                                new DateTime(2020, 2, 3, 8, 5, 0)),
                new DurationDto(new DateTime(2020, 2, 3, 9, 5, 0),
                                new DateTime(2020, 2, 3, 10, 10, 0)),
                new DurationDto(new DateTime(2020, 2, 3, 11, 0, 0),
                                new DateTime(2020, 2, 3, 14, 0, 0)),
            };

            actual.Should().BeEquivalentTo(expected);
        }
Esempio n. 2
0
        public void  至五與二至三沒有重疊()
        {
            var a = new DurationDto(new DateTime(2020, 2, 3), new DateTime(2020, 2, 5));
            var b = new DurationDto(new DateTime(2020, 2, 2), new DateTime(2020, 2, 3));

            var actual = a.IsOverlap(b);

            Assert.False(actual);
        }
Esempio n. 3
0
        public void  至五與四至四有重疊()
        {
            var a = new DurationDto(new DateTime(2020, 2, 3), new DateTime(2020, 2, 5));
            var b = new DurationDto(new DateTime(2020, 2, 4), new DateTime(2020, 2, 4));

            var actual = a.IsOverlap(b);

            Assert.True(actual);
        }
 public static Duration FromDto(this DurationDto duration)
 {
     return(duration switch
     {
         DurationDto.Monthly => Duration.Monthly,
         DurationDto.QuarterYear => Duration.QuarterYear,
         DurationDto.HalfYear => Duration.HalfYear,
         DurationDto.Year => Duration.Year,
         _ => throw new ArgumentException($"Value {duration} is not valid.", nameof(duration))
     });
Esempio n. 5
0
        public void 一個排除時間_起始結束過晚()
        {
            var sourcePeriod = new DurationDto(new DateTime(2020, 2, 3, 9, 0, 0), new DateTime(2020, 2, 3, 10, 0, 0));

            var exceptPeriods = new[]
            {
                new DurationDto(new DateTime(2020, 2, 3, 11, 0, 0), new DateTime(2020, 2, 3, 11, 30, 0)),
            };

            var actual   = sourcePeriod.Except(exceptPeriods).ToArray();
            var expected = new[]
            {
                new DurationDto(new DateTime(2020, 2, 3, 9, 0, 0), new DateTime(2020, 2, 3, 10, 0, 0))
            };

            actual.Should().BeEquivalentTo(expected);
        }
        /// <summary>
        /// source 排除 exceptPeriods
        /// <remarks>邊檢查邊回傳</remarks>
        /// </summary>
        /// <param name="source"></param>
        /// <param name="exceptPeriods"></param>
        /// <returns></returns>
        /// <exception cref="NotSupportedException"></exception>
        public static IEnumerable <DurationDto> Except(this DurationDto source,
                                                       IEnumerable <DurationDto> exceptPeriods)
        {
            // 先依照起始日期排序
            var exceptPeriodsByOrder = exceptPeriods?.OrderBy(p => p.Begin)
                                       .ThenBy(p => p.End)
                                       .ToArray()
                                       ?? new DurationDto[0];

            var begin = source.Begin;

            foreach (var exceptPeriod in exceptPeriodsByOrder)
            {
                if (begin < exceptPeriod.Begin)
                {
                    if (source.End >= exceptPeriod.Begin)
                    {
                        yield return(new DurationDto(begin, exceptPeriod.Begin));

                        begin = exceptPeriod.End;
                    }
                }
                else if (begin == exceptPeriod.Begin)
                {
                    begin = exceptPeriod.End;
                }
                else // begin > exceptPeriod.Begin
                {
                    if (begin < exceptPeriod.End)
                    {
                        begin = exceptPeriod.End;
                    }
                }
            }

            if (begin < source.End)
            {
                yield return(new DurationDto(begin, source.End));
            }
            else if (begin > source.End)
            {
                // throw new NotSupportedException("結束時間早於於排除起始時間");
            }
        }
Esempio n. 7
0
        public static bool IsOverlap(this DurationDto a, DurationDto b)
        {
            // 列舉所有可能版本
            // return (b.Begin <= a.Begin && a.Begin < b.End)
            //     || (b.Begin < a.End    && a.End   <= b.End)
            //     || (a.Begin <= b.Begin && b.Begin < a.End)
            //     || (a.Begin < b.End    && b.End   <= a.End);

            // 列舉 - 不會重疊的情境
            if (b.End <= a.Begin || a.End <= b.Begin)
            {
                return(false);
            }

            return(true);

            // 最簡化版本
            // return (a.Begin < b.End && b.Begin < a.End);
        }
Esempio n. 8
0
        public DurationDto GetDuration(int id)
        {
            DurationDto durationDto = new DurationDto();

            try
            {
                var duration = _durationRepository.GetAll().SingleOrDefault(x => x.Id == id);
                if (duration != null)
                {
                    durationDto = new DurationDto()
                    {
                        Name        = duration.Name,
                        Id          = duration.Id,
                        Description = duration.Description
                    };
                }
            }
            catch (Exception e)
            {
                _log.Error(e);
            }

            return(durationDto);
        }