/// <summary> /// Constructor /// </summary> /// <param name="year">Year</param> /// <param name="month">Month</param> /// <param name="dayOfMonth">Day of Month</param> /// <param name="dayOfWeek">Day of Week</param> public COSEMDate(ushort year, COSEMMonth month, COSEMDayOfMonth dayOfMonth, COSEMDayOfWeek dayOfWeek) { m_Year = year; m_Month = month; m_DayOfMonth = dayOfMonth; m_DayOfWeek = dayOfWeek; }
private static COSEMDayOfWeek ConvertDateTimeDayOfWeekToCOSEMDayOfWeek(DayOfWeek dayOfWeek) { COSEMDayOfWeek ConvertedDayOfWeek = COSEMDayOfWeek.NotSpecified; switch (dayOfWeek) { case DayOfWeek.Monday: { ConvertedDayOfWeek = COSEMDayOfWeek.Monday; break; } case DayOfWeek.Tuesday: { ConvertedDayOfWeek = COSEMDayOfWeek.Tuesday; break; } case DayOfWeek.Wednesday: { ConvertedDayOfWeek = COSEMDayOfWeek.Wednseday; break; } case DayOfWeek.Thursday: { ConvertedDayOfWeek = COSEMDayOfWeek.Thursday; break; } case DayOfWeek.Friday: { ConvertedDayOfWeek = COSEMDayOfWeek.Friday; break; } case DayOfWeek.Saturday: { ConvertedDayOfWeek = COSEMDayOfWeek.Saturday; break; } case DayOfWeek.Sunday: { ConvertedDayOfWeek = COSEMDayOfWeek.Sunday; break; } } return(ConvertedDayOfWeek); }
/// <summary> /// Gets the first date after the specified date that is represented by the COSEMDate object /// </summary> /// <param name="date">The date to get from</param> /// <returns>The first valid date</returns> public DateTime?GetClosestFutureDate(DateTime date) { DateTime?FutureDate = null; if (IsSpecificDate) { // The date is a specific date so as long as it's in the future we can return the date FutureDate = new DateTime(m_Year, (int)m_Month, (int)m_DayOfMonth); if (FutureDate < date.Date) { // The date is in the past so set it back to null FutureDate = null; } } else { // Start out with the specified date and figure it out from there DateTime NextDate = date.Date; if (m_Year != YEAR_NOT_SPECIFIED) { if (m_Year < NextDate.Year) { // No valid future date return(null); } else { NextDate = NextDate.AddYears(m_Year - NextDate.Year); } } if (m_Month != COSEMMonth.NotSpecified) { if (m_Month == COSEMMonth.DaylightSavingsBegin) { // TODO: Figure out how to get the DST Month here } else if (m_Month == COSEMMonth.DaylightSavingsEnd) { // TODO: Figure out how to get the DST Month here } else { // Add one to the month until we reach the desired month so that the year will change as necessary while (NextDate.Month != (int)m_Month) { NextDate = NextDate.AddMonths(1); } } } if (m_DayOfMonth != COSEMDayOfMonth.NotSpecified) { if (m_DayOfMonth == COSEMDayOfMonth.LastDay || m_DayOfMonth == COSEMDayOfMonth.SecondToLastDay) { // Add one day until the next day causes the month to change while (NextDate.Month == NextDate.AddDays(1).Month) { NextDate = NextDate.AddDays(1); } // For second to last day just subtract a day if (m_DayOfMonth == COSEMDayOfMonth.SecondToLastDay) { NextDate = NextDate.AddDays(-1); } } else { if ((int)m_DayOfMonth >= NextDate.Day && (int)m_DayOfMonth <= DateTime.DaysInMonth(NextDate.Year, NextDate.Month)) { // We have a valid date in the current month NextDate = NextDate.AddDays((int)m_DayOfMonth - NextDate.Day); } else if (m_Month == COSEMMonth.NotSpecified) { // The month is not specified and we have already passed the day in the current month so we should move on to the next month NextDate = new DateTime(NextDate.Year, NextDate.Month, 1, 0, 0, 0, NextDate.Kind).AddMonths(1); // Since the length of a month may vary lets just keep adding one day until it matches (if 29, 30 , or 31 is picked) while (NextDate.Day != (int)m_DayOfMonth) { NextDate = NextDate.AddDays(1); } } else if (m_Year == YEAR_NOT_SPECIFIED) { // In most cases we will need to add one year so let's go ahead and do that. NextDate = new DateTime(NextDate.Year, NextDate.Month, 1, 0, 0, 0, NextDate.Kind).AddYears(1); // The month is specified but the year is not. First we should check and see if the day of month is even valid at all for the month // February is the only month that the number of days in the month can vary so lets check that first if (m_Month == COSEMMonth.February) { if (m_DayOfMonth == COSEMDayOfMonth.Twentyninth) { // Increase the year until there are 29 days in the month while (DateTime.DaysInMonth(NextDate.Year, 2) != 29) { NextDate = NextDate.AddYears(1); } // Set the day to the 29th. We should be at the first so add 28 days NextDate = NextDate.AddDays(28); } else if (m_DayOfMonth <= COSEMDayOfMonth.Twentyeighth) { // We have already adjusted forward one year so just set the date NextDate = NextDate.AddDays((int)m_DayOfMonth - 1); } else { // The date will never be valid for this month return(null); } } else if ((int)m_DayOfMonth <= DateTime.DaysInMonth(NextDate.Year, NextDate.Month)) { // We have already adjusted forward one year so just set the date NextDate = NextDate.AddDays((int)m_DayOfMonth - 1); } else { // The date will never be valid for this month return(null); } } } } if (m_DayOfWeek != COSEMDayOfWeek.NotSpecified) { if (m_DayOfMonth == COSEMDayOfMonth.NotSpecified) { // Since the date is not specified we should be able to adjust the date forward int DaysToAdjust = 0; COSEMDayOfWeek CurrentDayOfWeek = ConvertDateTimeDayOfWeekToCOSEMDayOfWeek(NextDate.DayOfWeek); if (m_DayOfWeek >= CurrentDayOfWeek) { DaysToAdjust = (int)m_DayOfWeek - (int)CurrentDayOfWeek; } else { // We have to adjust through the end of the week and then to the correct day of the week DaysToAdjust = (int)COSEMDayOfWeek.Sunday - (int)CurrentDayOfWeek + (int)m_DayOfWeek; } if (m_Year != YEAR_NOT_SPECIFIED && m_Year != NextDate.AddDays(DaysToAdjust).Year) { // If the adjustment causes the year to change but a specific year is specified then there are no future dates return(null); } else if (m_Month != COSEMMonth.NotSpecified) { } } } } return(FutureDate); }