tools.hpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #ifndef TRM_TOOLS_H
  2. #define TRM_TOOLS_H
  3. #include <sstream>
  4. #include <string>
  5. #include <time.h>
  6. #include <iomanip>
  7. namespace tools{
  8. struct time_tools{
  9. static std::string time_t2string(const time_t& t)
  10. {
  11. struct tm* p = nullptr;
  12. char buf[30] = {'\0'};
  13. if(t<=0){
  14. return "0";
  15. }
  16. p = localtime(&t);
  17. snprintf(buf,
  18. sizeof(buf),
  19. "%04d-%02d-%02d %02d:%02d:%02d",
  20. p->tm_year+1900,
  21. p->tm_mon+1,
  22. p->tm_mday,
  23. p->tm_hour,
  24. p->tm_min,
  25. p->tm_sec
  26. );
  27. return std::string(buf);
  28. }
  29. };
  30. struct string_tools{
  31. static const std::string format13str(const uint8_t& l,const uint32_t& r)
  32. {
  33. //std::stringstream ss;
  34. //ss<<std::setw(3)<<std::setfill('0')<<l<<std::setw(10)<<std::setfill('0')<<r;
  35. //std::string result = ss.str();
  36. //return result;
  37. char sql[15] = {0};
  38. snprintf(sql, 15,"%03d%010d", l, r);
  39. return std::string(sql);
  40. }
  41. };
  42. };
  43. #endif