public async Task <IActionResult> GetWorkingDays(string from, string to)
        {
            try
            {
                var dataSource = new DataSource();
                _configuration.GetSection("DataSource").Bind(dataSource);

                string pattern = "yyyy-MM-dd";
                var    fromDt  = DateTime.ParseExact(from, pattern, null, DateTimeStyles.None);
                var    toDt    = DateTime.ParseExact(to, pattern, null, DateTimeStyles.None);

                var dataHelper = DataHelperFactory <IHoliday> .GetDataHelper(dataSource);

                var holidays = await dataHelper.Get();

                var dayCounter = new DayCounter(holidays);

                var days = await dayCounter.GetWorkingDays(fromDt, toDt);

                return(Ok(days));
            }
            catch
            {
                return(BadRequest());
            }
        }
Exemple #2
0
        public async Task Test07_WorkingDays()
        {
            var dataHelper = DataHelperFactory <IHoliday> .GetDataHelper((int)DataSourceTypeEn.Csv, "holiday_test05.txt");

            var holidays = await dataHelper.Get();

            var dayCounter = new DayCounter(holidays);

            var frDt        = new DateTime(2019, 12, 31);
            var toDt        = new DateTime(2020, 1, 4);
            var numWeekdays = await dayCounter.GetWorkingDays(frDt, toDt);

            Assert.IsTrue(numWeekdays == 2);

            toDt        = new DateTime(2020, 2, 1);
            numWeekdays = await dayCounter.GetWorkingDays(frDt, toDt);

            Assert.IsTrue(numWeekdays == 21);

            toDt        = new DateTime(2021, 1, 1);
            numWeekdays = await dayCounter.GetWorkingDays(frDt, toDt);

            Assert.IsTrue(numWeekdays == 256);

            frDt        = new DateTime(2018, 12, 31);
            toDt        = new DateTime(2020, 1, 1);
            numWeekdays = await dayCounter.GetWorkingDays(frDt, toDt);

            Assert.IsTrue(numWeekdays == 254);

            toDt        = new DateTime(2021, 1, 1);
            numWeekdays = await dayCounter.GetWorkingDays(frDt, toDt);

            Assert.IsTrue(numWeekdays == 510);
        }
        public async Task <IActionResult> GetHolidays()
        {
            try
            {
                var dataHelper = DataHelperFactory <IHoliday> .GetDataHelper((int)DataSourceTypeEn.Csv);

                var dataSet = await dataHelper.Get();

                return(Ok(dataSet));
            }
            catch
            {
                return(BadRequest());
            }
        }
Exemple #4
0
        public async Task Test04_InvalidData()
        {
            try
            {
                var dataHelper = DataHelperFactory <IHoliday> .GetDataHelper((int)DataSourceTypeEn.Csv, "holiday_test04.txt");

                var holidays = await dataHelper.Get();

                Assert.Fail();
            }
            catch (AssertionException)
            {
                Assert.Fail();
            }
            catch (Exception)
            {
                Assert.Pass();
            }
        }
Exemple #5
0
        public async Task Test08_WorkingDays2()
        {
            var Start      = DateTime.UtcNow;
            var dataHelper = DataHelperFactory <IHoliday> .GetDataHelper((int)DataSourceTypeEn.Csv, "holiday_test05.txt");

            var holidays = await dataHelper.Get();

            var dayCounter = new DayCounter(holidays);

            var frDt        = DateTime.MinValue;
            var toDt        = DateTime.MaxValue;
            var numWeekdays = await dayCounter.GetWorkingDays(frDt, toDt);

            var end = DateTime.UtcNow;

            var diff = end - Start;

            Assert.IsTrue(diff.Seconds < 1);
        }
Exemple #6
0
        public async Task Test05()
        {
            var dataHelper = DataHelperFactory <IHoliday> .GetDataHelper((int)DataSourceTypeEn.Csv, "holiday_test05.txt");

            var holidays = await dataHelper.Get();

            Assert.IsTrue(holidays.Count() == 7);

            for (int i = 0; i < holidays.Count(); i++)
            {
                var holiday = holidays.ElementAt(i);

                if (i == 0)
                {
                    var h = holiday as HolidayAdditionalDate;
                    Assert.IsTrue(h.Id == 1);
                    Assert.IsTrue(h.Name.Equals("New Year"));
                    Assert.IsTrue(h.Type == (int)HolidayTypeEn.AdditionalDate);
                    Assert.IsTrue(h.Month == 1);
                    Assert.IsTrue(h.Day == 1);
                }
                else if (i == 1)
                {
                    var h = holiday as HolidayAdditionalDate;
                    Assert.IsTrue(h.Id == 2);
                    Assert.IsTrue(h.Name.Equals("Australian Day"));
                    Assert.IsTrue(h.Type == (int)HolidayTypeEn.AdditionalDate);
                    Assert.IsTrue(h.Month == 1);
                    Assert.IsTrue(h.Day == 26);
                }
                else if (i == 2)
                {
                    var h = holiday as Holiday;
                    Assert.IsTrue(h.Id == 3);
                    Assert.IsTrue(h.Name.Equals("Anzac Day"));
                    Assert.IsTrue(h.Type == (int)HolidayTypeEn.FixedDate);
                    Assert.IsTrue(h.Month == 4);
                    Assert.IsTrue(h.Day == 25);
                }
                else if (i == 3)
                {
                    var h = holiday as HolidayFixedWeekday;
                    Assert.IsTrue(h.Id == 4);
                    Assert.IsTrue(h.Name.Equals("Queen's Birthday"));
                    Assert.IsTrue(h.Type == (int)HolidayTypeEn.FixedWeekday);
                    Assert.IsTrue(h.Month == 6);
                    Assert.IsTrue(h.Week == 2);
                    Assert.IsTrue(h.DayOfWeek == (int)DayOfWeek.Monday);
                }
                else if (i == 4)
                {
                    var h = holiday as HolidayFixedWeekday;
                    Assert.IsTrue(h.Id == 5);
                    Assert.IsTrue(h.Name.Equals("Labour Day"));
                    Assert.IsTrue(h.Type == (int)HolidayTypeEn.FixedWeekday);
                    Assert.IsTrue(h.Month == 10);
                    Assert.IsTrue(h.Week == 1);
                    Assert.IsTrue(h.DayOfWeek == (int)DayOfWeek.Monday);
                }
                else if (i == 5)
                {
                    var h = holiday as HolidayAdditionalDate;
                    Assert.IsTrue(h.Id == 6);
                    Assert.IsTrue(h.Name.Equals("Christmas Day"));
                    Assert.IsTrue(h.Type == (int)HolidayTypeEn.AdditionalDate);
                    Assert.IsTrue(h.Month == 12);
                    Assert.IsTrue(h.Day == 25);
                }
                else if (i == 6)
                {
                    var h = holiday as HolidayAdditionalDate;
                    Assert.IsTrue(h.Id == 7);
                    Assert.IsTrue(h.Name.Equals("Boxing Day"));
                    Assert.IsTrue(h.Type == (int)HolidayTypeEn.AdditionalDate);
                    Assert.IsTrue(h.Month == 12);
                    Assert.IsTrue(h.Day == 26);
                }
            }
        }