Time conversion functions with tests.

Fixes issue with incorrect characters in time strings on UI.
Fix platform dependency


Fix return value with incorrect strings.


Just use strptime and strftime on all platforms.

Emulate strptime on msvc... because they don't have it and their get_time is buggy.
This commit is contained in:
tamasmeszaros 2019-09-24 10:48:24 +02:00
parent f29e18dad2
commit d5dcba00b1
10 changed files with 296 additions and 99 deletions

View file

@ -1,3 +1,13 @@
# TODO Add individual tests as executables in separate directories
# add_subirectory(<testcase>)
# add_subirectory(<testcase>)
find_package(GTest REQUIRED)
set(TEST_DATA_DIR ${CMAKE_CURRENT_SOURCE_DIR}/data)
file(TO_NATIVE_PATH "${TEST_DATA_DIR}" TEST_DATA_DIR)
add_library(test_common INTERFACE)
target_compile_definitions(test_common INTERFACE TEST_DATA_DIR="${TEST_DATA_DIR}")
target_link_libraries(test_common INTERFACE GTest::GTest GTest::Main)
add_subdirectory(timeutils)

View file

@ -0,0 +1,5 @@
add_executable(timeutils_tests timeutils_tests_main.cpp)
target_link_libraries(timeutils_tests test_common libslic3r ${Boost_LIBRARIES} ${TBB_LIBRARIES} ${Boost_LIBRARIES})
add_test(timeutils_tests timeutils_tests)
#gtest_discover_tests(timeutils_tests TEST_PREFIX timeutils.)

View file

@ -0,0 +1,41 @@
#include <gtest/gtest.h>
#include "libslic3r/Time.hpp"
#include <sstream>
#include <iomanip>
#include <locale>
namespace {
void test_time_fmt(Slic3r::Utils::TimeFormat fmt) {
using namespace Slic3r::Utils;
time_t t = get_current_time_utc();
std::string tstr = time2str(t, TimeZone::local, fmt);
time_t parsedtime = str2time(tstr, TimeZone::local, fmt);
ASSERT_EQ(t, parsedtime);
tstr = time2str(t, TimeZone::utc, fmt);
parsedtime = str2time(tstr, TimeZone::utc, fmt);
ASSERT_EQ(t, parsedtime);
parsedtime = str2time("not valid string", TimeZone::local, fmt);
ASSERT_EQ(parsedtime, time_t(-1));
parsedtime = str2time("not valid string", TimeZone::utc, fmt);
ASSERT_EQ(parsedtime, time_t(-1));
}
}
TEST(Timeutils, ISO8601Z) {
test_time_fmt(Slic3r::Utils::TimeFormat::iso8601Z);
}
TEST(Timeutils, Slic3r_UTC_Time_Format) {
test_time_fmt(Slic3r::Utils::TimeFormat::gcode);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}