Esempio n. 1
0
        public void WorkoutRepeatedDates_Day()
        {
            var startdate = new Date(2020, 1, 6);
            var enddate   = new Date(2020, 1, 10);
            RepeatDatesCalculationInput vm = new RepeatDatesCalculationInput
            {
                StartDate  = startdate,
                EndDate    = enddate,
                RepeatType = RepeatFrequency.Day
            };
            List <RepeatedDates> results = CommonUtility.WorkoutRepeatedDates(vm);

            Assert.Equal(5, results.Count);

            Assert.Equal(startdate, results[0].StartDate);
            Assert.Equal(startdate, results[0].EndDate);

            Assert.Equal(new Date(2020, 1, 7), results[1].StartDate);
            Assert.Equal(new Date(2020, 1, 7), results[1].EndDate);

            Assert.Equal(new Date(2020, 1, 8), results[2].StartDate);
            Assert.Equal(new Date(2020, 1, 8), results[2].EndDate);

            Assert.Equal(new Date(2020, 1, 9), results[3].StartDate);
            Assert.Equal(new Date(2020, 1, 9), results[3].EndDate);

            Assert.Equal(enddate, results[4].StartDate);
            Assert.Equal(enddate, results[4].EndDate);
        }
        public IActionResult GetRepeatedDates2([FromODataUri] string StartDate, string EndDate, int RepeatType)
        {
            //            if (!ModelState.IsValid)
            //            {
            //#if DEBUG
            //                foreach (var value in ModelState.Values)
            //                {
            //                    foreach (var err in value.Errors)
            //                    {
            //                        System.Diagnostics.Debug.WriteLine(err.Exception != null ? err.Exception.Message : err.ErrorMessage);
            //                    }
            //                }
            //#endif
            //                return BadRequest();
            //            }

            var input = new RepeatDatesCalculationInput
            {
                StartDate  = DateTime.Parse(StartDate),
                EndDate    = DateTime.Parse(EndDate),
                RepeatType = (RepeatFrequency)RepeatType,
            };

            return(Ok(CommonUtility.WorkoutRepeatedDates(input)));
        }
        public IActionResult GetRepeatedDates([FromBody] RepeatDatesCalculationInput input)
        {
            if (!ModelState.IsValid)
            {
                HIHAPIUtility.HandleModalStateError(ModelState);
            }

            return(Ok(CommonUtility.WorkoutRepeatedDates(input)));
        }
Esempio n. 4
0
        public void WorkoutRepeatedDates_Quarter()
        {
            var startdate = new DateTime(2020, 1, 1);
            var enddate   = startdate.AddDays(100);
            RepeatDatesCalculationInput vm = new RepeatDatesCalculationInput
            {
                StartDate  = startdate,
                EndDate    = enddate,
                RepeatType = RepeatFrequency.Quarter
            };
            List <RepeatedDates> results = CommonUtility.WorkoutRepeatedDates(vm);

            Assert.Equal(2, results.Count);

            for (int i = 0; i < results.Count; i++)
            {
                Assert.Equal(startdate.Year, results[i].StartDate.Year);
                Assert.Equal(results[i].StartDate.Year, results[i].EndDate.Year);

                TimeSpan ts = ((DateTime)results[i].EndDate).Subtract(results[i].StartDate);
                Assert.Equal(90, ts.TotalDays);
            }
        }
Esempio n. 5
0
        public void WorkoutRepeatedDates_Month()
        {
            var startyear = 2020;
            RepeatDatesCalculationInput vm = new RepeatDatesCalculationInput
            {
                StartDate  = new DateTime(startyear, 1, 1),
                EndDate    = new DateTime(startyear, 12, 31),
                RepeatType = RepeatFrequency.Month
            };
            List <RepeatedDates> results = CommonUtility.WorkoutRepeatedDates(vm);

            Assert.Equal(12, results.Count);

            for (int i = 0; i < 12; i++)
            {
                Assert.Equal(startyear, results[i].StartDate.Year);
                Assert.Equal(results[i].StartDate.Year, results[i].EndDate.Year);
                Assert.Equal(results[i].StartDate.Month, results[i].EndDate.Month);
                Assert.Equal(i + 1, results[i].StartDate.Month);
                Assert.Equal(1, results[i].StartDate.Day);

                switch (i)
                {
                case 0:
                    Assert.Equal(31, results[i].EndDate.Day);
                    break;

                case 1:
                    Assert.Equal(29, results[i].EndDate.Day);
                    break;

                case 2:
                    Assert.Equal(31, results[i].EndDate.Day);
                    break;

                case 3:
                    Assert.Equal(30, results[i].EndDate.Day);
                    break;

                case 4:
                    Assert.Equal(31, results[i].EndDate.Day);
                    break;

                case 5:
                    Assert.Equal(30, results[i].EndDate.Day);
                    break;

                case 6:
                    Assert.Equal(31, results[i].EndDate.Day);
                    break;

                case 7:
                    Assert.Equal(31, results[i].EndDate.Day);
                    break;

                case 8:
                    Assert.Equal(30, results[i].EndDate.Day);
                    break;

                case 9:
                    Assert.Equal(31, results[i].EndDate.Day);
                    break;

                case 10:
                    Assert.Equal(30, results[i].EndDate.Day);
                    break;

                case 11:
                    Assert.Equal(31, results[i].EndDate.Day);
                    break;

                default:
                    break;
                }
            }
        }
Esempio n. 6
0
        public void RepeatedDates_Test(RepeatDatesCalculationInput datInput)
        {
            var results = CommonUtility.WorkoutRepeatedDates(datInput);

            switch (datInput.RepeatType)
            {
            case RepeatFrequency.Month:
                Assert.Equal(12, results.Count);
                for (int i = 0; i < 12; i++)
                {
                    Assert.Equal(results[i].StartDate.Year, results[i].EndDate.Year);
                    Assert.Equal(results[i].StartDate.Month, results[i].EndDate.Month);
                    Assert.Equal(i + 1, results[i].StartDate.Month);
                    Assert.Equal(1, results[i].StartDate.Day);

                    switch (i)
                    {
                    case 0:
                        Assert.Equal(31, results[i].EndDate.Day);
                        break;

                    case 1:
                        Assert.Equal(29, results[i].EndDate.Day);
                        break;

                    case 2:
                        Assert.Equal(31, results[i].EndDate.Day);
                        break;

                    case 3:
                        Assert.Equal(30, results[i].EndDate.Day);
                        break;

                    case 4:
                        Assert.Equal(31, results[i].EndDate.Day);
                        break;

                    case 5:
                        Assert.Equal(30, results[i].EndDate.Day);
                        break;

                    case 6:
                        Assert.Equal(31, results[i].EndDate.Day);
                        break;

                    case 7:
                        Assert.Equal(31, results[i].EndDate.Day);
                        break;

                    case 8:
                        Assert.Equal(30, results[i].EndDate.Day);
                        break;

                    case 9:
                        Assert.Equal(31, results[i].EndDate.Day);
                        break;

                    case 10:
                        Assert.Equal(30, results[i].EndDate.Day);
                        break;

                    case 11:
                        Assert.Equal(31, results[i].EndDate.Day);
                        break;

                    default:
                        break;
                    }
                }

                break;

            case RepeatFrequency.Week:
                Assert.Equal(5, results.Count);
                break;

            default:
                break;
            }
        }
Esempio n. 7
0
        public static List <RepeatedDates> WorkoutRepeatedDates(RepeatDatesCalculationInput datInput)
        {
            List <RepeatedDates> listResults = new List <RepeatedDates>();

            // Input checks
            if (datInput == null)
            {
                throw new Exception("Input the data!");
            }
            var dtEnd   = new DateTime(datInput.EndDate.Year, datInput.EndDate.Month, datInput.EndDate.Day);
            var dtStart = new DateTime(datInput.StartDate.Year, datInput.StartDate.Month, datInput.StartDate.Day);

            if (dtEnd < dtStart)
            {
                throw new Exception("Invalid data range");
            }

            switch (datInput.RepeatType)
            {
            case RepeatFrequency.Day:
            {
                var tspans = dtEnd - dtStart;
                var tdays  = (Int32)tspans.Days;

                for (int i = 0; i <= tdays; i++)
                {
                    listResults.Add(new RepeatedDates
                        {
                            StartDate = dtStart.AddDays(i),
                        });
                }

                for (int i = 0; i < listResults.Count; i++)
                {
                    listResults[i].EndDate = listResults[i].StartDate;
                }
            }
            break;

            case RepeatFrequency.Fortnight:
            {
                var tspans = dtEnd - dtStart;
                var tdays  = (Int32)tspans.Days;

                var tfortnights = tdays / 14;

                for (int i = 0; i <= tfortnights; i++)
                {
                    listResults.Add(new RepeatedDates
                        {
                            StartDate = datInput.StartDate.AddDays(i * 14),
                        });
                }

                for (int i = 0; i < listResults.Count; i++)
                {
                    if (i == listResults.Count - 1)
                    {
                        listResults[i].EndDate = listResults[i].StartDate.AddDays(13);
                    }
                    else
                    {
                        listResults[i].EndDate = listResults[i + 1].StartDate.AddDays(-1);
                    }
                }
            }
            break;

            case RepeatFrequency.HalfYear:
            {
                var nmonths   = (datInput.EndDate.Year - datInput.StartDate.Year) * 12 + (datInput.EndDate.Month - datInput.StartDate.Month);
                var nhalfyear = nmonths / 6;

                for (int i = 0; i <= nhalfyear; i++)
                {
                    listResults.Add(new RepeatedDates
                        {
                            StartDate = datInput.StartDate.AddMonths(i * 6),
                        });
                }

                for (int i = 0; i < listResults.Count; i++)
                {
                    if (i == listResults.Count - 1)
                    {
                        listResults[i].EndDate = listResults[i].StartDate.AddMonths(6);
                    }
                    else
                    {
                        listResults[i].EndDate = listResults[i + 1].StartDate.AddDays(-1);
                    }
                }
            }
            break;

            case RepeatFrequency.Month:
            {
                var nmonths = (datInput.EndDate.Year - datInput.StartDate.Year) * 12 + (datInput.EndDate.Month - datInput.StartDate.Month);

                for (int i = 0; i <= nmonths; i++)
                {
                    listResults.Add(new RepeatedDates
                        {
                            StartDate = datInput.StartDate.AddMonths(i),
                        });
                }

                for (int i = 0; i < listResults.Count; i++)
                {
                    if (i == listResults.Count - 1)
                    {
                        listResults[i].EndDate = listResults[i].StartDate.AddMonths(1).AddDays(-1);
                    }
                    else
                    {
                        listResults[i].EndDate = listResults[i + 1].StartDate.AddDays(-1);
                    }
                }
            }
            break;

            case RepeatFrequency.Quarter:
            {
                var nmonths   = (datInput.EndDate.Year - datInput.StartDate.Year) * 12 + (datInput.EndDate.Month - datInput.StartDate.Month);
                var nquarters = nmonths / 3;

                for (int i = 0; i <= nquarters; i++)
                {
                    listResults.Add(new RepeatedDates
                        {
                            StartDate = datInput.StartDate.AddMonths(i * 3),
                        });
                }

                for (int i = 0; i < listResults.Count; i++)
                {
                    if (i == listResults.Count - 1)
                    {
                        listResults[i].EndDate = listResults[i].StartDate.AddMonths(3).AddDays(-1);
                    }
                    else
                    {
                        listResults[i].EndDate = listResults[i + 1].StartDate.AddDays(-1);
                    }
                }
            }
            break;

            case RepeatFrequency.Week:
            {
                var tspans = dtEnd - dtStart;
                var tdays  = (Int32)tspans.Days;

                var tweeks = tdays / 7;

                for (int i = 0; i <= tweeks; i++)
                {
                    listResults.Add(new RepeatedDates
                        {
                            StartDate = datInput.StartDate.AddDays(i * 7),
                        });
                }

                for (int i = 0; i < listResults.Count; i++)
                {
                    if (i == listResults.Count - 1)
                    {
                        listResults[i].EndDate = listResults[i].StartDate.AddDays(6);
                    }
                    else
                    {
                        listResults[i].EndDate = listResults[i + 1].StartDate.AddDays(-1);
                    }
                }
            }
            break;

            case RepeatFrequency.Year:
            {
                var nyears = datInput.EndDate.Year - datInput.StartDate.Year;

                for (int i = 0; i <= nyears; i++)
                {
                    listResults.Add(new RepeatedDates
                        {
                            StartDate = datInput.StartDate.AddYears(i),
                        });
                }

                for (int i = 0; i < listResults.Count; i++)
                {
                    if (i == listResults.Count - 1)
                    {
                        listResults[i].EndDate = listResults[i].StartDate.AddYears(1).AddDays(-1);
                    }
                    else
                    {
                        listResults[i].EndDate = listResults[i + 1].StartDate.AddDays(-1);
                    }
                }
            }
            break;

            case RepeatFrequency.Manual:
            {
                // It shall return only entry out
                listResults.Add(new RepeatedDates
                    {
                        StartDate = datInput.StartDate,
                        EndDate   = datInput.EndDate
                    });
            }
            break;

            default:
                System.Diagnostics.Debug.Assert(false);
                break;
            }

            return(listResults);
        }