DateTime.h 1.9 KB

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