Example #1
0
        public IEnumerable<KlimaTemperaturPeriod> GroupTemperaturByPeriods(
            IEnumerable<KlimaTemperatur> klimaTemperaturs, 
            DateTime startDate, 
            IntervalType intervalType = IntervalType.M24)
        {
            var endDate = startDate.GetPastDate((int)intervalType);

            var datumVon1 = startDate.GetPastDate(12);
            var datumBis1 = startDate.GetLastDayOfMonth();
            var datumVon2 = endDate;
            var datumBis2 = startDate.GetPastDate(13).GetLastDayOfMonth();

            //For dayCounter important only month and day (used in chart X-Axis)
            var dayCounter = datumVon1; 
            var result = klimaTemperaturs
                .Where(p => !(p.Datum.Month == 2 && p.Datum.Day == 29 )) 
                .OrderBy( p=> p.Datum)
                .GroupBy(p => new { p.Datum.Day, p.Datum.Month })
                .Select((gr, index) => new KlimaTemperaturPeriod
                {
                    Datum = dayCounter.AddDays(index),
                    Period1 = gr.Where(p => p.Datum.IsBetween(datumVon1, datumBis1)).Select(p => p.Temperatur).FirstOrDefault(),
                    Period2= gr.Where(p => p.Datum.IsBetween(datumVon2, datumBis2)).Select(p => p.Temperatur).FirstOrDefault(),
                    Heizgrenztemperatur = 15
                });
            return  result;
        }
        public override DateTime CalculateNextPeriodEndDateFrom(DateTime fromDate)
        {
            if (PeriodStartDate == null)
            {
                return fromDate;
            }

            if (PaidToLastDayOfMonth)
            {
                return new DateTime(fromDate.Year, fromDate.Month, fromDate.GetLastDayOfMonth());
            }

            int day = PeriodStartDate.Value.Day <= fromDate.GetLastDayOfMonth() ?
                                                                                    PeriodStartDate.Value.Day :
                                                                                                                  fromDate.GetLastDayOfMonth();

            var nextPeriodEndDate = new DateTime(fromDate.Year, fromDate.Month, day);
            bool inSameMonth = PeriodStartDate.Value.Day > fromDate.Day;
            if (!inSameMonth)
            {
                nextPeriodEndDate = nextPeriodEndDate.AddMonths(1);
            }
            return nextPeriodEndDate;
        }
    public void TestGetLastDayOfMonth(int givenYear, int givenMonth, int givenDay,
                                      int expectedYear, int expectedMonth, int expectedDay)
    {
      // Arrange
      DateTime
        given = new DateTime(givenYear, givenMonth, givenDay),
        expected = new DateTime(expectedYear, expectedMonth, expectedDay);

      // Act and assert together
      Assert.AreEqual(expected,
                      given.GetLastDayOfMonth(),
                      "Date: {0}, expected: {1}",
                      given.ToShortDateString(),
                      expected.ToShortDateString());
    }
        public void TestCurrentDate()
        {
            var date = new DateTime(2015, 1, 30, 10, 30, 5);
            var date1 = new DateTime(2015, 1, 30);
            var date2 = new DateTime(2015, 1, 30, 23, 59, 59);
            var date3 = new DateTime(2015, 1, 31);
            var date4 = new DateTime(2015, 1, 29);
            var date5 = new DateTime(2015, 1, 1);
            var date6 = new DateTime(2015, 1, 31);
            var date7 = new DateTime(2015, 1, 26);
            var date8 = new DateTime(2015, 2, 1);
            var date9 = new DateTime(2015, 1, 29);

            Assert.AreEqual(date1, date.StartOfDay());
            Assert.AreEqual(date2, date.EndOfDay());
            Assert.AreEqual(date3, date.NextDay());
            Assert.AreEqual(date4, date.Yesterday());
            Assert.AreEqual(date5, date.GetFirstDayOfMonth());
            Assert.AreEqual(date6, date.GetLastDayOfMonth());
            Assert.AreEqual(date7, date.GetFirstDayOfWeek());
            Assert.AreEqual(date8, date.GetLastDayOfWeek());
            Assert.AreEqual(date9, date.GetWeekday(DayOfWeek.Thursday));
        }
 private void TestLastDayOfMonth(DateTime testingDate, int expectedLastDay)
 {
     int lastDayOfMonth = testingDate.GetLastDayOfMonth();
     Assert.AreEqual(expectedLastDay, lastDayOfMonth);
 }
 private static int GetMaximumWeekDayIndex(DateTime date, DayOfWeek weekDay)
 {
     var weekDayIndex = 0;
       for (var i = date.GetFirstDayOfMonth(); i <= date.GetLastDayOfMonth(); i = i.AddDays(1))
       {
     if (i.DayOfWeek == weekDay)
     {
       weekDayIndex++;
     }
       }
       return weekDayIndex;
 }