From 7b546bea2e374a20c52ffd71de51f91eba07256d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Fri, 29 Aug 2014 18:17:27 +0200 Subject: [PATCH] api: use microseconds instead of milliseconds in functions util_timeval_diff and util_timeval_add --- ChangeLog.asciidoc | 2 ++ ReleaseNotes.asciidoc | 9 +++++ doc/en/weechat_plugin_api.en.asciidoc | 26 +++++++++----- doc/fr/weechat_plugin_api.fr.asciidoc | 26 +++++++++----- doc/it/weechat_plugin_api.it.asciidoc | 34 ++++++++++++++----- doc/ja/weechat_plugin_api.ja.asciidoc | 32 ++++++++++++----- src/core/wee-hook.c | 8 +++-- src/core/wee-upgrade.c | 4 +-- src/core/wee-util.c | 19 ++++++----- src/core/wee-util.h | 4 +-- src/plugins/irc/irc-protocol.c | 4 +-- src/plugins/irc/irc-server.c | 4 +-- src/plugins/relay/weechat/relay-weechat-msg.c | 6 ++-- src/plugins/weechat-plugin.h | 6 ++-- tests/unit/core/test-util.cpp | 11 +++--- 15 files changed, 133 insertions(+), 62 deletions(-) diff --git a/ChangeLog.asciidoc b/ChangeLog.asciidoc index e3521cb81..71c7ed291 100644 --- a/ChangeLog.asciidoc +++ b/ChangeLog.asciidoc @@ -19,6 +19,8 @@ http://weechat.org/files/releasenotes/ReleaseNotes-devel.html[release notes] * core: allow incomplete commands if unambiguous, new option weechat.look.command_incomplete (task #5419) +* api: use microseconds instead of milliseconds in functions util_timeval_diff + and util_timeval_add * trigger: add option "restore" in command /trigger === Bugs fixed diff --git a/ReleaseNotes.asciidoc b/ReleaseNotes.asciidoc index 27b2dd11b..00e4e61dd 100644 --- a/ReleaseNotes.asciidoc +++ b/ReleaseNotes.asciidoc @@ -17,6 +17,15 @@ http://weechat.org/files/changelog/ChangeLog-devel.html[ChangeLog] == Version 1.1 (under dev) +=== Microseconds in API timeval functions + +The API functions using timeval are now using or returning microseconds, +instead of milliseconds: + +* function 'util_timeval_diff': returns microseconds +* function 'util_timeval_add': the argument 'interval' is now expressed in + microseconds. + === Default triggers for hiding passwords Triggers used to hide passwords have been fixed for *BSD operating systems. diff --git a/doc/en/weechat_plugin_api.en.asciidoc b/doc/en/weechat_plugin_api.en.asciidoc index 9bc9584d3..99017b3e6 100644 --- a/doc/en/weechat_plugin_api.en.asciidoc +++ b/doc/en/weechat_plugin_api.en.asciidoc @@ -2668,13 +2668,15 @@ This function is not available in scripting API. ==== weechat_util_timeval_diff -Return difference (in milliseconds) between two "timeval" structures. +_Updated in 1.1._ + +Return difference (in microseconds) between two "timeval" structures. Prototype: [source,C] ---- -long weechat_util_timeval_diff (struct timeval *tv1, struct timeval *tv2); +long long weechat_util_timeval_diff (struct timeval *tv1, struct timeval *tv2); ---- Arguments: @@ -2684,13 +2686,16 @@ Arguments: Return value: -* difference in milliseconds +* difference in microseconds + +[NOTE] +With WeeChat ≤ 1.0, the returned value was in milliseconds. C example: [source,C] ---- -long diff = weechat_util_timeval_diff (&tv1, &tv2); +long long diff = weechat_util_timeval_diff (&tv1, &tv2); ---- [NOTE] @@ -2698,25 +2703,30 @@ This function is not available in scripting API. ==== weechat_util_timeval_add -Add interval (in milliseconds) to a timeval structure. +_Updated in 1.1._ + +Add interval (in microseconds) to a timeval structure. Prototype: [source,C] ---- -void weechat_util_timeval_add (struct timeval *tv, long interval); +void weechat_util_timeval_add (struct timeval *tv, long long interval); ---- Arguments: * 'tv': timeval structure -* 'interval': interval (in milliseconds) +* 'interval': interval (in microseconds) + +[NOTE] +With WeeChat ≤ 1.0, the interval was expressed in milliseconds. C example: [source,C] ---- -weechat_util_timeval_add (&tv, 2000); /* add 2 seconds */ +weechat_util_timeval_add (&tv, 2000000); /* add 2 seconds */ ---- [NOTE] diff --git a/doc/fr/weechat_plugin_api.fr.asciidoc b/doc/fr/weechat_plugin_api.fr.asciidoc index 1a6052238..17c8c7799 100644 --- a/doc/fr/weechat_plugin_api.fr.asciidoc +++ b/doc/fr/weechat_plugin_api.fr.asciidoc @@ -2713,13 +2713,15 @@ Cette fonction n'est pas disponible dans l'API script. ==== weechat_util_timeval_diff -Retourner la différence (en millisecondes) entre deux structures "timeval". +_Mis à jour dans la 1.1._ + +Retourner la différence (en microsecondes) entre deux structures "timeval". Prototype : [source,C] ---- -long weechat_util_timeval_diff (struct timeval *tv1, struct timeval *tv2); +long long weechat_util_timeval_diff (struct timeval *tv1, struct timeval *tv2); ---- Paramètres : @@ -2729,13 +2731,16 @@ Paramètres : Valeur de retour : -* différence en millisecondes +* différence en microsecondes + +[NOTE] +Avec WeeChat ≤ 1.0, la valeur retournée était en millisecondes. Exemple en C : [source,C] ---- -long diff = weechat_util_timeval_diff (&tv1, &tv2); +long long diff = weechat_util_timeval_diff (&tv1, &tv2); ---- [NOTE] @@ -2743,25 +2748,30 @@ Cette fonction n'est pas disponible dans l'API script. ==== weechat_util_timeval_add -Ajouter un intervalle (en millisecondes) à une structure "timeval". +_Mis à jour dans la 1.1._ + +Ajouter un intervalle (en microsecondes) à une structure "timeval". Prototype : [source,C] ---- -void weechat_util_timeval_add (struct timeval *tv, long interval); +void weechat_util_timeval_add (struct timeval *tv, long long interval); ---- Paramètres : * 'tv' : structure "timeval" -* 'interval' : intervalle (en millisecondes) +* 'interval' : intervalle (en microsecondes) + +[NOTE] +Avec WeeChat ≤ 1.0, l'intervalle était exprimé en millisecondes. Exemple en C : [source,C] ---- -weechat_util_timeval_add (&tv, 2000); /* ajoute 2 secondes */ +weechat_util_timeval_add (&tv, 2000000); /* ajouter 2 secondes */ ---- [NOTE] diff --git a/doc/it/weechat_plugin_api.it.asciidoc b/doc/it/weechat_plugin_api.it.asciidoc index 1d05a074e..b643527a3 100644 --- a/doc/it/weechat_plugin_api.it.asciidoc +++ b/doc/it/weechat_plugin_api.it.asciidoc @@ -2736,13 +2736,17 @@ Questa funzione non è disponibile nelle API per lo scripting. ==== weechat_util_timeval_diff -Restituisce la differenza (in millisecondi) tra due strutture "timeval". +// TRANSLATION MISSING +_Updated in 1.1._ + +// TRANSLATION MISSING +Return difference (in microseconds) between two "timeval" structures. Prototipo: [source,C] ---- -long weechat_util_timeval_diff (struct timeval *tv1, struct timeval *tv2); +long long weechat_util_timeval_diff (struct timeval *tv1, struct timeval *tv2); ---- Argomenti: @@ -2752,13 +2756,18 @@ Argomenti: Valore restituito: -* differenza in millisecondi +// TRANSLATION MISSING +* difference in microseconds + +// TRANSLATION MISSING +[NOTE] +With WeeChat ≤ 1.0, the returned value was in milliseconds. Esempio in C: [source,C] ---- -long diff = weechat_util_timeval_diff (&tv1, &tv2); +long long diff = weechat_util_timeval_diff (&tv1, &tv2); ---- [NOTE] @@ -2766,25 +2775,34 @@ Questa funzione non è disponibile nelle API per lo scripting. ==== weechat_util_timeval_add -Aggiungi intervallo (in millisecondi) ad una struttura timeval. +// TRANSLATION MISSING +_Updated in 1.1._ + +// TRANSLATION MISSING +Add interval (in microseconds) to a timeval structure. Prototipo: [source,C] ---- -void weechat_util_timeval_add (struct timeval *tv, long interval); +void weechat_util_timeval_add (struct timeval *tv, long long interval); ---- Argomenti: * 'tv': struttura timeval -* 'interval': intervallo (in millisecondi) +// TRANSLATION MISSING +* 'interval': interval (in microseconds) + +// TRANSLATION MISSING +[NOTE] +With WeeChat ≤ 1.0, the interval was expressed in milliseconds. Esempio in C: [source,C] ---- -weechat_util_timeval_add (&tv, 2000); /* aggiunge 2 secondi */ +weechat_util_timeval_add (&tv, 2000000); /* aggiunge 2 secondi */ ---- [NOTE] diff --git a/doc/ja/weechat_plugin_api.ja.asciidoc b/doc/ja/weechat_plugin_api.ja.asciidoc index 0b14d93d6..149f493b2 100644 --- a/doc/ja/weechat_plugin_api.ja.asciidoc +++ b/doc/ja/weechat_plugin_api.ja.asciidoc @@ -2664,13 +2664,16 @@ if (weechat_util_timeval_cmp (&tv1, &tv2) > 0) ==== weechat_util_timeval_diff -2 つの "timeval" 構造体の差を (ミリ秒単位で) 返す。 +_バージョン 1.1 で更新。_ + +// TRANSLATION MISSING +Return difference (in microseconds) between two "timeval" structures. プロトタイプ: [source,C] ---- -long weechat_util_timeval_diff (struct timeval *tv1, struct timeval *tv2); +long long weechat_util_timeval_diff (struct timeval *tv1, struct timeval *tv2); ---- 引数: @@ -2680,13 +2683,18 @@ long weechat_util_timeval_diff (struct timeval *tv1, struct timeval *tv2); 戻り値: -* ミリ秒単位の差 +// TRANSLATION MISSING +* difference in microseconds + +// TRANSLATION MISSING +[NOTE] +With WeeChat ≤ 1.0, the returned value was in milliseconds. C 言語での使用例: [source,C] ---- -long diff = weechat_util_timeval_diff (&tv1, &tv2); +long long diff = weechat_util_timeval_diff (&tv1, &tv2); ---- [NOTE] @@ -2694,25 +2702,33 @@ long diff = weechat_util_timeval_diff (&tv1, &tv2); ==== weechat_util_timeval_add -timeval 構造体に時間間隔 (ミリ秒単位) を追加 +_バージョン 1.1 で更新。_ + +// TRANSLATION MISSING +Add interval (in microseconds) to a timeval structure. プロトタイプ: [source,C] ---- -void weechat_util_timeval_add (struct timeval *tv, long interval); +void weechat_util_timeval_add (struct timeval *tv, long long interval); ---- 引数: * 'tv': timeval 構造体 -* 'interval': 時間間隔 (ミリ秒単位) +// TRANSLATION MISSING +* 'interval': interval (in microseconds) + +// TRANSLATION MISSING +[NOTE] +With WeeChat ≤ 1.0, the interval was expressed in milliseconds. C 言語での使用例: [source,C] ---- -weechat_util_timeval_add (&tv, 2000); /* add 2 seconds */ +weechat_util_timeval_add (&tv, 2000000); /* add 2 seconds */ ---- [NOTE] diff --git a/src/core/wee-hook.c b/src/core/wee-hook.c index 015056ef8..5e0f0e77a 100644 --- a/src/core/wee-hook.c +++ b/src/core/wee-hook.c @@ -984,7 +984,8 @@ hook_timer_init (struct t_hook *hook) HOOK_TIMER(hook, next_exec).tv_usec = HOOK_TIMER(hook, last_exec).tv_usec; /* add interval to next call date */ - util_timeval_add (&HOOK_TIMER(hook, next_exec), HOOK_TIMER(hook, interval)); + util_timeval_add (&HOOK_TIMER(hook, next_exec), + ((long long)HOOK_TIMER(hook, interval)) * 1000); } /* @@ -1179,8 +1180,9 @@ hook_timer_exec () HOOK_TIMER(ptr_hook, last_exec).tv_sec = tv_time.tv_sec; HOOK_TIMER(ptr_hook, last_exec).tv_usec = tv_time.tv_usec; - util_timeval_add (&HOOK_TIMER(ptr_hook, next_exec), - HOOK_TIMER(ptr_hook, interval)); + util_timeval_add ( + &HOOK_TIMER(ptr_hook, next_exec), + ((long long)HOOK_TIMER(ptr_hook, interval)) * 1000); if (HOOK_TIMER(ptr_hook, remaining_calls) > 0) { diff --git a/src/core/wee-upgrade.c b/src/core/wee-upgrade.c index 5826399a5..6e07b6c65 100644 --- a/src/core/wee-upgrade.c +++ b/src/core/wee-upgrade.c @@ -898,7 +898,7 @@ void upgrade_weechat_end () { struct timeval tv_now; - long time_diff; + long long time_diff; /* remove .upgrade files */ util_exec_on_files (weechat_home, @@ -912,7 +912,7 @@ upgrade_weechat_end () gui_chat_printf (NULL, /* TRANSLATORS: %.02fs is a float number + "s" ("seconds") */ _("Upgrade done (%.02fs)"), - ((float)time_diff) / 1000); + ((float)time_diff) / 1000000); /* upgrading ended */ weechat_upgrading = 0; diff --git a/src/core/wee-util.c b/src/core/wee-util.c index 1223b0488..c709d4b25 100644 --- a/src/core/wee-util.c +++ b/src/core/wee-util.c @@ -254,13 +254,13 @@ util_timeval_cmp (struct timeval *tv1, struct timeval *tv2) /* * Calculates difference between two timeval structures. * - * Returns difference in milliseconds. + * Returns difference in microseconds. */ -long +long long util_timeval_diff (struct timeval *tv1, struct timeval *tv2) { - long diff_sec, diff_usec; + long long diff_sec, diff_usec; if (!tv1 || !tv2) return 0; @@ -273,23 +273,24 @@ util_timeval_diff (struct timeval *tv1, struct timeval *tv2) diff_usec += 1000000; diff_sec--; } - return ((diff_usec / 1000) + (diff_sec * 1000)); + + return (diff_sec * 1000000) + diff_usec; } /* - * Adds interval (in milliseconds) to a timeval structure. + * Adds interval (in microseconds) to a timeval structure. */ void -util_timeval_add (struct timeval *tv, long interval) +util_timeval_add (struct timeval *tv, long long interval) { - long usec; + long long usec; if (!tv) return; - tv->tv_sec += (interval / 1000); - usec = tv->tv_usec + ((interval % 1000) * 1000); + tv->tv_sec += (interval / 1000000); + usec = tv->tv_usec + (interval % 1000000); if (usec > 1000000) { tv->tv_usec = usec % 1000000; diff --git a/src/core/wee-util.h b/src/core/wee-util.h index 2471868a6..e0fb631eb 100644 --- a/src/core/wee-util.h +++ b/src/core/wee-util.h @@ -36,8 +36,8 @@ struct t_util_signal extern void util_setrlimit (); extern int util_timeval_cmp (struct timeval *tv1, struct timeval *tv2); -extern long util_timeval_diff (struct timeval *tv1, struct timeval *tv2); -extern void util_timeval_add (struct timeval *tv, long interval); +extern long long util_timeval_diff (struct timeval *tv1, struct timeval *tv2); +extern void util_timeval_add (struct timeval *tv, long long interval); extern char *util_get_time_string (const time_t *date); extern int util_signal_search (const char *name); extern void util_catch_signal (int signum, void (*handler)(int)); diff --git a/src/plugins/irc/irc-protocol.c b/src/plugins/irc/irc-protocol.c index fcf25d05b..3c15d614d 100644 --- a/src/plugins/irc/irc-protocol.c +++ b/src/plugins/irc/irc-protocol.c @@ -1633,8 +1633,8 @@ IRC_PROTOCOL_CALLBACK(pong) /* calculate lag (time diff with lag check) */ old_lag = server->lag; gettimeofday (&tv, NULL); - server->lag = (int) weechat_util_timeval_diff (&(server->lag_check_time), - &tv); + server->lag = (int)(weechat_util_timeval_diff (&(server->lag_check_time), + &tv) / 1000); if (old_lag != server->lag) weechat_bar_item_update ("lag"); diff --git a/src/plugins/irc/irc-server.c b/src/plugins/irc/irc-server.c index 5f8418b61..d954b79e3 100644 --- a/src/plugins/irc/irc-server.c +++ b/src/plugins/irc/irc-server.c @@ -2898,8 +2898,8 @@ irc_server_timer_cb (void *data, int remaining_calls) if (ptr_server->lag_check_time.tv_sec != 0) { gettimeofday (&tv, NULL); - ptr_server->lag = (int) weechat_util_timeval_diff (&(ptr_server->lag_check_time), - &tv); + ptr_server->lag = (int)(weechat_util_timeval_diff (&(ptr_server->lag_check_time), + &tv) / 1000); /* refresh lag item if needed */ if (((ptr_server->lag_last_refresh == 0) || (current_time >= ptr_server->lag_last_refresh + weechat_config_integer (irc_config_network_lag_refresh_interval))) diff --git a/src/plugins/relay/weechat/relay-weechat-msg.c b/src/plugins/relay/weechat/relay-weechat-msg.c index a7be45f2c..4fc601440 100644 --- a/src/plugins/relay/weechat/relay-weechat-msg.c +++ b/src/plugins/relay/weechat/relay-weechat-msg.c @@ -1023,7 +1023,7 @@ relay_weechat_msg_send (struct t_relay_client *client, Bytef *dest; uLongf dest_size; struct timeval tv1, tv2; - long time_diff; + long long time_diff; if (weechat_config_integer (relay_config_network_compression_level) > 0) { @@ -1049,11 +1049,11 @@ relay_weechat_msg_send (struct t_relay_client *client, /* display message in raw buffer */ snprintf (raw_message, sizeof (raw_message), - "obj: %d/%d bytes (%d%%, %ldms), id: %s", + "obj: %d/%d bytes (%d%%, %.2fms), id: %s", (int)dest_size + 5, msg->data_size, 100 - ((((int)dest_size + 5) * 100) / msg->data_size), - time_diff, + ((float)time_diff) / 1000, msg->id); /* send compressed data */ diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h index ad619e9c2..f53283365 100644 --- a/src/plugins/weechat-plugin.h +++ b/src/plugins/weechat-plugin.h @@ -57,7 +57,7 @@ struct timeval; * please change the date with current one; for a second change at same * date, increment the 01, otherwise please keep 01. */ -#define WEECHAT_PLUGIN_API_VERSION "20140802-01" +#define WEECHAT_PLUGIN_API_VERSION "20140829-01" /* macros for defining plugin infos */ #define WEECHAT_PLUGIN_NAME(__name) \ @@ -304,8 +304,8 @@ struct t_weechat_plugin /* util */ int (*util_timeval_cmp) (struct timeval *tv1, struct timeval *tv2); - long (*util_timeval_diff) (struct timeval *tv1, struct timeval *tv2); - void (*util_timeval_add) (struct timeval *tv, long interval); + long long (*util_timeval_diff) (struct timeval *tv1, struct timeval *tv2); + void (*util_timeval_add) (struct timeval *tv, long long interval); char *(*util_get_time_string) (const time_t *date); int (*util_version_number) (const char *version); diff --git a/tests/unit/core/test-util.cpp b/tests/unit/core/test-util.cpp index 84f5a2df2..eb2174e1e 100644 --- a/tests/unit/core/test-util.cpp +++ b/tests/unit/core/test-util.cpp @@ -43,9 +43,11 @@ TEST_GROUP(Util) TEST(Util, Timeval) { + struct timeval tv_zero = { 0, 0 }; struct timeval tv1 = { 123456, 12000 }; struct timeval tv2 = { 123456, 15000 }; struct timeval tv3 = { 123457, 15000 }; + struct timeval tv4 = { 1409288400, 0 }; /* 2014-08-29 05:00:00 GMT */ struct timeval tv; /* comparison */ @@ -60,17 +62,18 @@ TEST(Util, Timeval) LONGS_EQUAL(0, util_timeval_diff (NULL, NULL)); LONGS_EQUAL(0, util_timeval_diff (NULL, &tv1)); LONGS_EQUAL(0, util_timeval_diff (&tv1, NULL)); - LONGS_EQUAL(3, util_timeval_diff (&tv1, &tv2)); - LONGS_EQUAL(1003, util_timeval_diff (&tv1, &tv3)); + LONGS_EQUAL(3000, util_timeval_diff (&tv1, &tv2)); + LONGS_EQUAL(1003000, util_timeval_diff (&tv1, &tv3)); + CHECK(1409288400 * 1000000LL == util_timeval_diff (&tv_zero, &tv4)); /* add interval */ util_timeval_add (NULL, 0); tv.tv_sec = 123456; tv.tv_usec = 12000; - util_timeval_add (&tv, 10); + util_timeval_add (&tv, 10000); LONGS_EQUAL(123456, tv.tv_sec); LONGS_EQUAL(22000, tv.tv_usec); - util_timeval_add (&tv, 4000); + util_timeval_add (&tv, 4000000); LONGS_EQUAL(123460, tv.tv_sec); LONGS_EQUAL(22000, tv.tv_usec); }