DateUtils.cpp 748 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "StdInc.h"
  2. #include <vstd/DateUtils.h>
  3. VCMI_LIB_NAMESPACE_BEGIN
  4. namespace vstd
  5. {
  6. DLL_LINKAGE std::string getFormattedDateTime(std::time_t dt, std::string format)
  7. {
  8. std::tm tm = *std::localtime(&dt);
  9. std::stringstream s;
  10. if(format.empty())
  11. {
  12. try
  13. {
  14. s.imbue(std::locale(""));
  15. }
  16. catch(const std::runtime_error & e)
  17. {
  18. // locale not be available - keep default / global
  19. }
  20. s << std::put_time(&tm, "%x %X");
  21. }
  22. else
  23. s << std::put_time(&tm, format.c_str());
  24. return s.str();
  25. }
  26. DLL_LINKAGE std::string getDateTimeISO8601Basic(std::time_t dt)
  27. {
  28. std::tm tm = *std::localtime(&dt);
  29. std::stringstream s;
  30. s << std::put_time(&tm, "%Y%m%dT%H%M%S");
  31. return s.str();
  32. }
  33. }
  34. VCMI_LIB_NAMESPACE_END