test_timeutils.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <catch2/catch.hpp>
  2. #include "libslic3r/Time.hpp"
  3. #include <sstream>
  4. #include <iomanip>
  5. #include <locale>
  6. using namespace Slic3r;
  7. static void test_time_fmt(Slic3r::Utils::TimeFormat fmt) {
  8. using namespace Slic3r::Utils;
  9. time_t t = get_current_time_utc();
  10. std::string tstr = time2str(t, TimeZone::local, fmt);
  11. time_t parsedtime = str2time(tstr, TimeZone::local, fmt);
  12. REQUIRE(t == parsedtime);
  13. tstr = time2str(t, TimeZone::utc, fmt);
  14. parsedtime = str2time(tstr, TimeZone::utc, fmt);
  15. REQUIRE(t == parsedtime);
  16. parsedtime = str2time("not valid string", TimeZone::local, fmt);
  17. REQUIRE(parsedtime == time_t(-1));
  18. parsedtime = str2time("not valid string", TimeZone::utc, fmt);
  19. REQUIRE(parsedtime == time_t(-1));
  20. }
  21. TEST_CASE("ISO8601Z", "[Timeutils]") {
  22. test_time_fmt(Slic3r::Utils::TimeFormat::iso8601Z);
  23. std::string mydate = "20190710T085000Z";
  24. time_t t = Slic3r::Utils::parse_iso_utc_timestamp(mydate);
  25. std::string date = Slic3r::Utils::iso_utc_timestamp(t);
  26. REQUIRE(date == mydate);
  27. }
  28. TEST_CASE("Slic3r_UTC_Time_Format", "[Timeutils]") {
  29. using namespace Slic3r::Utils;
  30. test_time_fmt(TimeFormat::gcode);
  31. std::string mydate = "2019-07-10 at 08:50:00 UTC";
  32. time_t t = Slic3r::Utils::str2time(mydate, TimeZone::utc, TimeFormat::gcode);
  33. std::string date = Slic3r::Utils::utc_timestamp(t);
  34. REQUIRE(date == mydate);
  35. }