diff --git a/src/core/wee-debug.c b/src/core/wee-debug.c index 88233ccad..00655a448 100644 --- a/src/core/wee-debug.c +++ b/src/core/wee-debug.c @@ -736,29 +736,29 @@ debug_display_time_elapsed (struct timeval *time1, struct timeval *time2, const char *message, int display) { struct timeval debug_timeval_end; - long long diff, diff_hour, diff_min, diff_sec, diff_usec; + char *str_diff; + long long diff; gettimeofday (&debug_timeval_end, NULL); diff = util_timeval_diff (time1, time2); - - diff_usec = diff % 1000000; - diff_sec = (diff / 1000000) % 60; - diff_min = ((diff / 1000000) / 60) % 60; - diff_hour = (diff / 1000000) / 3600; + str_diff = util_get_microseconds_string (diff); if (display) { gui_chat_printf (NULL, - "debug: time[%s] -> %lld:%02lld:%02lld.%06lld", + "debug: time[%s] -> %s", (message) ? message : "?", - diff_hour, diff_min, diff_sec, diff_usec); + (str_diff) ? str_diff : "?"); } else { - log_printf ("debug: time[%s] -> %lld:%02lld:%02lld.%06lld", + log_printf ("debug: time[%s] -> %s", (message) ? message : "?", - diff_hour, diff_min, diff_sec, diff_usec); + (str_diff) ? str_diff : "?"); } + + if (str_diff) + free (str_diff); } /* diff --git a/src/core/wee-util.c b/src/core/wee-util.c index 0e70aee4a..e39aa01bd 100644 --- a/src/core/wee-util.c +++ b/src/core/wee-util.c @@ -285,6 +285,31 @@ util_timeval_add (struct timeval *tv, long long interval) tv->tv_usec = usec; } +/* + * Converts microseconds to a string, using format: "H:MM:SS.mmmmmm" + * where: H=hours, MM=minutes, SS=seconds, mmmmmm=microseconds + * + * Note: result must be freed after use. + */ + +char * +util_get_microseconds_string (long long microseconds) +{ + long long hour, min, sec, usec; + char result[128]; + + usec = microseconds % 1000000; + sec = (microseconds / 1000000) % 60; + min = ((microseconds / 1000000) / 60) % 60; + hour = (microseconds / 1000000) / 3600; + + snprintf (result, sizeof (result), + "%lld:%02lld:%02lld.%06lld", + hour, min, sec, usec); + + return strdup (result); +} + /* * Converts date to a string, using format of option "weechat.look.time_format" * (can be localized). diff --git a/src/core/wee-util.h b/src/core/wee-util.h index 1acb9a76d..d569e0c43 100644 --- a/src/core/wee-util.h +++ b/src/core/wee-util.h @@ -40,6 +40,7 @@ extern long long util_timeval_diff (struct timeval *tv1, struct timeval *tv2); extern void util_timeval_add (struct timeval *tv, long long interval); /* time */ +extern char *util_get_microseconds_string (long long diff); extern const char *util_get_time_string (const time_t *date); extern void util_get_time_diff (time_t time1, time_t time2, time_t *total_seconds, diff --git a/tests/unit/core/test-core-util.cpp b/tests/unit/core/test-core-util.cpp index 04aaa0554..e5021db53 100644 --- a/tests/unit/core/test-core-util.cpp +++ b/tests/unit/core/test-core-util.cpp @@ -21,6 +21,8 @@ #include "CppUTest/TestHarness.h" +#include "tests/tests.h" + extern "C" { #include @@ -35,6 +37,26 @@ TEST_GROUP(CoreUtil) { }; +/* + * Tests functions: + * util_setrlimit_resource + */ + +TEST(CoreUtil, SetrlimitResource) +{ + /* TODO: write tests */ +} + +/* + * Tests functions: + * util_setrlimit + */ + +TEST(CoreUtil, Setrlimit) +{ + /* TODO: write tests */ +} + /* * Tests functions: * util_timeval_cmp @@ -86,6 +108,43 @@ TEST(CoreUtil, Timeval) LONGS_EQUAL(21000, tv.tv_usec); } +/* + * Tests functions: + * util_get_microseconds_string + */ + +TEST(CoreUtil, GetMicrosecondsString) +{ + char *str; + + /* zero */ + WEE_TEST_STR("0:00:00.000000", + util_get_microseconds_string (0LL)); + + /* microseconds */ + WEE_TEST_STR("0:00:00.000001", util_get_microseconds_string (1LL)); + WEE_TEST_STR("0:00:00.000123", util_get_microseconds_string (123LL)); + + /* microseconds */ + WEE_TEST_STR("0:00:00.001000", util_get_microseconds_string (1LL * 1000LL)); + WEE_TEST_STR("0:00:00.123000", util_get_microseconds_string (123LL * 1000LL)); + + /* seconds */ + WEE_TEST_STR("0:00:01.000000", util_get_microseconds_string (1LL * 1000LL * 1000LL)); + WEE_TEST_STR("0:00:12.000000", util_get_microseconds_string (12LL * 1000LL * 1000LL)); + + /* minutes */ + WEE_TEST_STR("0:01:00.000000", util_get_microseconds_string (1LL * 60LL * 1000LL * 1000LL)); + WEE_TEST_STR("0:34:00.000000", util_get_microseconds_string (34LL * 60LL * 1000LL * 1000LL)); + + /* hours */ + WEE_TEST_STR("1:00:00.000000", util_get_microseconds_string (1LL * 60LL * 60LL * 1000LL * 1000LL)); + WEE_TEST_STR("34:00:00.000000", util_get_microseconds_string (34LL * 60LL * 60LL * 1000LL * 1000LL)); + + /* hours + minutes + seconds + milliseconds + microseconds */ + WEE_TEST_STR("3:25:45.678901", util_get_microseconds_string (12345678901LL)); +} + /* * Tests functions: * util_get_time_string