time.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #ifndef _BASE_TIME_H_
  2. #define _BASE_TIME_H_
  3. #include <time.h>
  4. #include <stdint.h>
  5. #include <string>
  6. #include "utils/TimeHelper.h"
  7. #include "utils/Log.h"
  8. namespace base {
  9. const int Hour = 60 * 60;
  10. const int Day = Hour * 24;
  11. class DateTime {
  12. public:
  13. DateTime();
  14. DateTime(time_t timestamp);
  15. DateTime(const std::string& datetimestr);
  16. std::string ToString();
  17. int64_t Unix() const;
  18. bool after(const DateTime& t) const {
  19. return true;
  20. }
  21. bool before(const DateTime& t) const {
  22. return true;
  23. }
  24. DateTime Add(int64_t seconds);
  25. public:
  26. int year = 0;
  27. int month = 0;
  28. int day = 0;
  29. int hour = 0;
  30. int min = 0;
  31. int second = 0;
  32. int week = 0; /* 星期–取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
  33. };
  34. /**
  35. * 开机累计毫秒数
  36. * @return
  37. */
  38. int64_t uptime();
  39. /**
  40. * 获得当前日期时间
  41. * @return
  42. */
  43. class DateTime GetDateTimeNow();
  44. /**
  45. * 设置系统日期时间
  46. * @param timestamp
  47. */
  48. void SetSystemDateTime(time_t timestamp);
  49. /**
  50. * 设置系统日期时间
  51. * @param dt
  52. */
  53. void SetSystemDateTime(const DateTime& dt);
  54. //设置时区 - 东八区
  55. inline void SetTimeZone() {
  56. setenv("TZ", "CST-8", 1);
  57. }
  58. } /* namespace base */
  59. #endif /* _FY_TIME_H_ */