日期模式比较和匹配

时间:2013-06-04 11:15:35

标签: c++ algorithm

我正在寻找一种智能方法来比较日期模式与其他日期模式,当然尤其是特定日期。我必须使用的日期模式以紧凑的形式有四个成员。

first octet year
   0 .. 254 = 1900 .. 2154
        255 = any year
second octet month
   1 ..  12 = January .. December
         13 = odd months
         14 = even months
        255 = any month
third octet day of month
   1 ..  31 = day of month
         32 = last day of month
         33 = odd days of month
         34 = even days of month
        255 = any day of month
fourth octet day of week
   1 ..   7 = Monday .. Sunday
        255 = any day of week

由于日期模式可以存储在32位整数中,我考虑使用位掩码或其他东西进行比较。代码示例显示了我目前计划使用的早期类。但没有“聪明”。对于每一个有用的提示,我将不胜感激!

#include <time.h>

class DatePattern
{
private:
   int year;
   int month;
   int dayOfMonth;
   int dayOfWeek;

public:
   DatePattern(int year, int month, int dayOfMonth, int dayOfWeek)
   {
      // validate year
      if (year < 1900 || year > 2154)
      {
         year = 255;
      }
      this->year = year;

      // validate month
      if (month < 1 || month > 14)
      {
         month = 255;
      }
      this->month = month;

      // validate day of month
      if (dayOfMonth < 1 || dayOfMonth > 34)
      {
         dayOfMonth = 255;
      }
      this->dayOfMonth = dayOfMonth;

      // validate day of week
      if (dayOfWeek < 1 || dayOfWeek > 7)
      {
         dayOfWeek = 255;
      }
      this->dayOfWeek = dayOfWeek;
   }

   bool Match(struct tm * timeinfo)
   {
      // validate time pointer
      if (timeinfo == NULL)
      {
         time_t rawtime;
         time(&rawtime);
         timeinfo = localtime(&rawtime);
      }

      // check year
      if (year != 255 && timeinfo->tm_year != year)
      {
         return false;
      }

      // check month
      int currentMonth = timeinfo->tm_mon + 1;
      bool oddMonth = (currentMonth % 2 ? true : false);
      if (month != 255 && (month == 13 && !oddMonth) && (month == 14 && oddMonth) && month != currentMonth)
      {
         return false;
      }

      // check day of month
      bool oddDay = (timeinfo->tm_mday % 2 ? true : false);
      if (dayOfMonth != 255 && (dayOfMonth == 33 && !oddDay) && (dayOfMonth == 34 && oddDay) && dayOfMonth != timeinfo->tm_mday)
      {
         return false;
      }

      // check day of week
      int currentDayOfWeek = (timeinfo->tm_wday == 0 ? 7 : timeinfo->tm_wday);
      if (dayOfWeek != 255 && dayOfWeek != currentDayOfWeek)
      {
         return false;
      }
      return true;
   }

   long Compact()
   {
      return (((year - 1900) << 24) | (month << 16) | (dayOfMonth << 8) | (dayOfWeek));
   }

   int Year()
   {
      return year;
   }

   int Month()
   {
      return month;
   }

   int DayOfMonth()
   {
      return dayOfMonth;
   }

   int DayOfWeek()
   {
      return dayOfWeek;
   }
};

int main ()
{
   DatePattern pattern(-1, 6, -1, 2); // every Tuesday in June
   bool result = pattern.Match(NULL);
   return 0;
}

所以我的问题是:有人知道将日期与日期模式进行比较的更好方法吗?是否已有以类似方式使用日期模式的库或已知实现?

1 个答案:

答案 0 :(得分:1)

这是我该怎么做的:

  • 创建课程日,接受值1..31,32,33,34,255并存储有关其性质的信息(实际日期或规则);
  • 在课程日重载==运算符,以便可以使用您的规则与另一天进行比较;
  • 以类似方式实施DayOfWeek,Month,Year课程;
  • 实施类Date,汇总DayOfWeek,Day,Month,Year,可以使用特定日期(d / m / y)或“编码”日期构建;
  • 在课程日期重载==操作员,以便通过比较他们的Day,DayOfWeek,Month和Year部分,将一个Date与另一个Date进行比较。