DateTime.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #ifndef DATE_TIME_H
  2. #define DATE_TIME_H
  3. #include <set>
  4. #include <string>
  5. #include <time.h>
  6. class DateTime
  7. {
  8. public:
  9. static DateTime now()
  10. {
  11. return DateTime(time(NULL));
  12. }
  13. explicit DateTime(time_t t): theTime(t) {}
  14. DateTime(int Y, int M, int D, int h, int m, int s);
  15. friend DateTime operator+(const DateTime& t1, time_t t2)
  16. {
  17. return DateTime(t1.theTime + t2);
  18. }
  19. const DateTime& operator+=(time_t t2)
  20. {
  21. theTime += t2;
  22. return *this;
  23. }
  24. void get(int& year, int& month, int& mday,
  25. int& hour, int& min, int& sec) const;
  26. std::string getString(char typ = 'h') const;
  27. const DateTime& operator-=(time_t t2)
  28. {
  29. theTime -= t2;
  30. return *this;
  31. }
  32. friend DateTime operator-(const DateTime& t1, time_t t2)
  33. {
  34. return DateTime(t1.theTime - t2);
  35. }
  36. friend bool operator<(const DateTime& t1, const DateTime& t2)
  37. {
  38. return t1.theTime < t2.theTime;
  39. }
  40. friend bool operator<=(const DateTime& t1, const DateTime& t2)
  41. {
  42. return t1.theTime <= t2.theTime;
  43. }
  44. friend bool operator==(const DateTime& t1, const DateTime& t2)
  45. {
  46. return t1.theTime == t2.theTime;
  47. }
  48. friend bool operator!=(const DateTime& t1, const DateTime& t2)
  49. {
  50. return t1.theTime != t2.theTime;
  51. }
  52. friend bool operator>(const DateTime& t1, const DateTime& t2)
  53. {
  54. return t1.theTime > t2.theTime;
  55. }
  56. friend bool operator>=(const DateTime& t1, const DateTime& t2)
  57. {
  58. return t1.theTime >= t2.theTime;
  59. }
  60. bool match(const std::set<int>& Y, const std::set<int>& M, const std::set<int>& D,
  61. const std::set<int>& W,
  62. const std::set<int>& h, const std::set<int>& m, const std::set<int>& s);
  63. private:
  64. static bool match(const std::set<int>& v, int v2)
  65. {
  66. return (v.empty()) || (v.count(v2) > 0);
  67. }
  68. static std::string toString2(int v);
  69. time_t theTime;
  70. };
  71. #endif