1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-26 21:06:38 +02:00

core: add function util_get_microseconds_string

This commit is contained in:
Sébastien Helleu
2023-08-25 11:53:23 +02:00
parent 015ff084e3
commit cc2bb4b8cf
4 changed files with 95 additions and 10 deletions
+10 -10
View File
@@ -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);
}
/*
+25
View File
@@ -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).
+1
View File
@@ -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,
+59
View File
@@ -21,6 +21,8 @@
#include "CppUTest/TestHarness.h"
#include "tests/tests.h"
extern "C"
{
#include <unistd.h>
@@ -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