Util.cpp 956 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "Util.h"
  2. #include <array>
  3. #include <ctime>
  4. #include <filesystem/path.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. namespace CameraRecorder {
  8. namespace Util {
  9. std::string GetDateTimeString(bool omitSeparator)
  10. {
  11. std::array<char, 17> buffer;
  12. buffer.fill(0);
  13. std::time_t t = std::time(nullptr);
  14. std::tm* tm = std::localtime(&t);
  15. if (omitSeparator)
  16. strftime(buffer.data(), sizeof(buffer), "%Y%m%d%H%M%S", tm);
  17. else
  18. strftime(buffer.data(), sizeof(buffer), "%Y%m%d-%H%M%S", tm);
  19. return buffer.data();
  20. }
  21. bool FolderExists(const std::string& path)
  22. {
  23. struct stat info;
  24. if (stat(path.c_str(), &info) != 0)
  25. return false;
  26. else if (info.st_mode & S_IFDIR)
  27. return true;
  28. else
  29. return false;
  30. }
  31. void CreateFolder(const std::string& path)
  32. {
  33. int status = mkdir(path.c_str(), 0777);
  34. if ((status < 0) && (errno != EEXIST))
  35. throw std::runtime_error("Can't create folder.");
  36. }
  37. } // namespace Util
  38. } // namespace CameraRecorder