worktime_rate.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #ifndef __WORKTIME_RATE_H
  2. #define __WORKTIME_RATE_H
  3. #define LENGTH_SQL 2000
  4. #include "three_rates.h"
  5. #include <map>
  6. #include <string>
  7. #include <memory>
  8. #include<chrono>
  9. class BanShift
  10. {
  11. public:
  12. int shift_id;
  13. int shift_type_id;
  14. uint32_t start_time;
  15. uint32_t end_time;
  16. BanShift()
  17. {}
  18. BanShift(int sid,int tid, uint32_t stime, uint32_t etime)
  19. {
  20. shift_id = sid;
  21. shift_type_id = tid;
  22. start_time = stime%(24*3600);
  23. end_time = etime%(24*3600);
  24. }
  25. };
  26. typedef std::map<int,std::shared_ptr<BanShift>> BanShiftMap;
  27. class worktime
  28. {
  29. public:
  30. worktime();
  31. ~worktime();
  32. /*
  33. *@brief
  34. Load shift From dat_shift
  35. */
  36. void init_shift();
  37. /*
  38. *@brief
  39. when the card enter work_area record the date in DB
  40. *@param [in] const card_pos &pos \n
  41. *@param [in] const area_data &Area \n
  42. */
  43. void enter_area(std::shared_ptr<card_pos> &card_ptr, std::shared_ptr<area_data> &area_ptr);
  44. /*
  45. *@brief
  46. when the card leave work_area record the date in DB
  47. *@param [in] const card_pos &pos \n
  48. *@param [in] const area_data &Area \n
  49. */
  50. void leave_area(std::shared_ptr<card_pos> &card_ptr, std::shared_ptr<area_data> &area_ptr);
  51. private:
  52. /*
  53. *@brief
  54. Get current time for shitf
  55. *return shift_id
  56. *param [in] struct tm* current_time \n
  57. *param [in] int shift_type = 1 \n
  58. */
  59. int _get_shift_id( time_t time);
  60. bool _get_start_worktime(int staff_id, time_t& out_time);
  61. BanShiftMap _shitf_map;
  62. //"%Y-%m-%d %H:%M:%S"
  63. static time_t to_time(std::string str)
  64. {
  65. time_t t_;
  66. tm tm_={0};
  67. strptime(str.c_str(), "%Y-%m-%d %H:%M:%S", &tm_); //将字符串转换为tm时间
  68. t_ = mktime(&tm_); //将tm时间转换为秒时间
  69. return t_;
  70. }
  71. //%H:%M:%S"
  72. static uint32_t to_time_short(std::string str)
  73. {
  74. tm tm_;
  75. strptime(str.c_str(), "%H:%M:%S", &tm_); //将字符串转换为tm时间
  76. //t_ = mktime(&tm_); //将tm时间转换为秒时间
  77. return tm_.tm_hour*3600 + tm_.tm_min*60 + tm_.tm_sec;
  78. }
  79. static uint32_t to_time_short(std::time_t time)
  80. {
  81. std::string str = to_str(time);
  82. tm tm_;
  83. strptime(str.c_str(), "%Y-%m-%d %H:%M:%S", &tm_); //将字符串转换为tm时间
  84. //t_ = mktime(&tm_); //将tm时间转换为秒时间
  85. return tm_.tm_hour*3600 + tm_.tm_min*60 + tm_.tm_sec;
  86. }
  87. //"%Y-%m-%d %H:%M:%S"
  88. static std::string to_str(const std::time_t &time)
  89. {
  90. char _time[25] = {0};
  91. struct tm local_time;
  92. localtime_r(&time, &local_time);
  93. strftime(_time, 22, "%Y-%m-%d %H:%M:%S", &local_time);
  94. return std::string(_time);
  95. }
  96. uint32_t now_to_seconds()
  97. {
  98. return static_cast<uint32_t>( std::chrono::duration_cast<std::chrono::seconds>
  99. (std::chrono::system_clock::now().time_since_epoch()).count() );
  100. }
  101. };
  102. #endif//__WORKTIME_RATE_H