summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2024-01-27 12:15:40 +0100
committerSébastien Helleu <flashcode@flashtux.org>2024-01-27 16:53:53 +0100
commit2469dc6df95713d15eb993438594db14416141b2 (patch)
treebe0bc7e858630a9b4d4aeb0e14347d5f29f3b5ca
parente3af6a91d4e7b2de38c0b60b5c1bb0344dc00e02 (diff)
downloadweechat-2469dc6df95713d15eb993438594db14416141b2.zip
core: use function util_strftimeval in evaluation of expression `date:xxx`
-rw-r--r--ChangeLog.adoc1
-rw-r--r--doc/en/weechat_plugin_api.en.adoc9
-rw-r--r--doc/fr/weechat_plugin_api.fr.adoc11
-rw-r--r--doc/it/weechat_plugin_api.it.adoc9
-rw-r--r--doc/ja/weechat_plugin_api.ja.adoc10
-rw-r--r--doc/sr/weechat_plugin_api.sr.adoc10
-rw-r--r--src/core/wee-eval.c18
-rw-r--r--tests/unit/core/test-core-eval.cpp12
8 files changed, 52 insertions, 28 deletions
diff --git a/ChangeLog.adoc b/ChangeLog.adoc
index ab9ad98f1..4cb0a866b 100644
--- a/ChangeLog.adoc
+++ b/ChangeLog.adoc
@@ -16,6 +16,7 @@ For a list of important changes that require manual actions, please look at rele
New features::
* core: allow case insensitive search of partial buffer name with `(?i)name` in command `/buffer`
+ * core: use function util_strftimeval in evaluation of expression `date:xxx`
* api: add support of specifier `%!` for timestamp in function util_strftimeval
Bug fixes::
diff --git a/doc/en/weechat_plugin_api.en.adoc b/doc/en/weechat_plugin_api.en.adoc
index 4567c9bc4..d5fc83563 100644
--- a/doc/en/weechat_plugin_api.en.adoc
+++ b/doc/en/weechat_plugin_api.en.adoc
@@ -2567,7 +2567,7 @@ str3 = weechat.string_input_for_buffer("//test") # "/test"
==== string_eval_expression
_WeeChat ≥ 0.4.0, updated in 0.4.2, 0.4.3, 1.0, 1.1, 1.2, 1.3, 1.6, 1.8, 2.0,
-2.2, 2.3, 2.7, 2.9, 3.1, 3.2, 3.3, 3.4, 3.6, 3.8, 4.0.0, 4.2.0._
+2.2, 2.3, 2.7, 2.9, 3.1, 3.2, 3.3, 3.4, 3.6, 3.8, 4.0.0, 4.2.0, 4.3.0._
Evaluate an expression and return result as a string.
Special variables with format `+${variable}+` are expanded (see table below).
@@ -3208,13 +3208,16 @@ expanded to last):
| `+${date}+` +
`+${date:xxx}+` | 1.3
-| Current date/time, with custom format (see `man strftime`),
+| Current date/time, with custom format (see function <<_util_strftimeval,util_strftimeval>>),
default format is `%F %T`.
| >> `+${date}+` +
== `+2015-06-30 19:02:45+` +
+
>> `+${date:%H:%M:%S}+` +
- == `+19:02:45+`
+ == `+19:02:45+` +
+ +
+ >> `+${date:%!.%.3}+` +
+ == `+1435683765.482+`
| `+${env:NAME}+` | 1.2
| Value of the environment variable `NAME`.
diff --git a/doc/fr/weechat_plugin_api.fr.adoc b/doc/fr/weechat_plugin_api.fr.adoc
index 5a8c81b2e..74d5a184e 100644
--- a/doc/fr/weechat_plugin_api.fr.adoc
+++ b/doc/fr/weechat_plugin_api.fr.adoc
@@ -2613,7 +2613,7 @@ str3 = weechat.string_input_for_buffer("//test") # "/test"
==== string_eval_expression
_WeeChat ≥ 0.4.0, mis à jour dans la 0.4.2, 0.4.3, 1.0, 1.1, 1.2, 1.3, 1.6, 1.8,
-2.0, 2.2, 2.3, 2.7, 2.9, 3.1, 3.2, 3.3, 3.4, 3.6, 3.8, 4.0.0, 4.2.0._
+2.0, 2.2, 2.3, 2.7, 2.9, 3.1, 3.2, 3.3, 3.4, 3.6, 3.8, 4.0.0, 4.2.0, 4.3.0._
Évaluer l'expression et retourner le résultat sous forme de chaîne.
Les variables spéciales avec le format `+${variable}+` sont étendues (voir le
@@ -3263,13 +3263,16 @@ première étendue à la dernière) :
| `+${date}+` +
`+${date:xxx}+` | 1.3
-| La date/heure courante, avec un format personnalisé (voir `man strftime`),
- le format par défaut est `%F %T`.
+| La date/heure courante, avec un format personnalisé (voir la fonction
+ <<_util_strftimeval,util_strftimeval>>), le format par défaut est `%F %T`.
| >> `+${date}+` +
== `+2015-06-30 19:02:45+` +
+
>> `+${date:%H:%M:%S}+` +
- == `+19:02:45+`
+ == `+19:02:45+` +
+ +
+ >> `+${date:%!.%.3}+` +
+ == `+1435683765.482+`
| `+${env:NOM}+` | 1.2
| Valeur de la variable d'environnement `NOM`.
diff --git a/doc/it/weechat_plugin_api.it.adoc b/doc/it/weechat_plugin_api.it.adoc
index 2f47ef3ce..9a9da605f 100644
--- a/doc/it/weechat_plugin_api.it.adoc
+++ b/doc/it/weechat_plugin_api.it.adoc
@@ -2717,7 +2717,7 @@ str3 = weechat.string_input_for_buffer("//test") # "/test"
==== string_eval_expression
_WeeChat ≥ 0.4.0, updated in 0.4.2, 0.4.3, 1.0, 1.1, 1.2, 1.3, 1.6, 1.8, 2.0,
-2.2, 2.3, 2.7, 2.9, 3.1, 3.2, 3.3, 3.4, 3.6, 3.8, 4.0.0, 4.2.0._
+2.2, 2.3, 2.7, 2.9, 3.1, 3.2, 3.3, 3.4, 3.6, 3.8, 4.0.0, 4.2.0, 4.3.0._
Evaluate an expression and return result as a string.
Special variables with format `+${variable}+` are expanded (see table below).
@@ -3357,13 +3357,16 @@ expanded to last):
| `+${date}+` +
`+${date:xxx}+` | 1.3
-| Current date/time, with custom format (see `man strftime`),
+| Current date/time, with custom format (see function <<_util_strftimeval,util_strftimeval>>),
default format is `%F %T`.
| >> `+${date}+` +
== `+2015-06-30 19:02:45+` +
+
>> `+${date:%H:%M:%S}+` +
- == `+19:02:45+`
+ == `+19:02:45+` +
+ +
+ >> `+${date:%!.%.3}+` +
+ == `+1435683765.482+`
| `+${env:NAME}+` | 1.2
| Value of the environment variable `NAME`.
diff --git a/doc/ja/weechat_plugin_api.ja.adoc b/doc/ja/weechat_plugin_api.ja.adoc
index eacaa9e23..1f44d1d14 100644
--- a/doc/ja/weechat_plugin_api.ja.adoc
+++ b/doc/ja/weechat_plugin_api.ja.adoc
@@ -2637,7 +2637,7 @@ str3 = weechat.string_input_for_buffer("//test") # "/test"
// TRANSLATION MISSING
_WeeChat ≥ 0.4.0, updated in 0.4.2, 0.4.3, 1.0, 1.1, 1.2, 1.3, 1.6, 1.8, 2.0,
-2.2, 2.3, 2.7, 2.9, 3.1, 3.2, 3.3, 3.4, 3.6, 3.8, 4.0.0, 4.2.0._
+2.2, 2.3, 2.7, 2.9, 3.1, 3.2, 3.3, 3.4, 3.6, 3.8, 4.0.0, 4.2.0, 4.3.0._
式を評価して文字列として返す。`+${variable}+`
という書式で書かれた特殊変数は展開されます (以下の表を参照)。
@@ -3304,15 +3304,19 @@ str5 = weechat.string_eval_expression("password=abc password=def", {}, {}, optio
>> `+${base_decode:64,dGVzdCBzdHJpbmc=}+` +
== `+test string+`
+// TRANSLATION MISSING
| `+${date}+` +
`+${date:xxx}+` | 1.3
-| 現在の日付/時刻、カスタム書式を使うことも可能です (`man strftime` を参照)、
+| 現在の日付/時刻、カスタム書式を使うことも可能です (see function <<_util_strftimeval,util_strftimeval>>)、
デフォルト書式は `%F %T`
| >> `+${date}+` +
== `+2015-06-30 19:02:45+` +
+
>> `+${date:%H:%M:%S}+` +
- == `+19:02:45+`
+ == `+19:02:45+` +
+ +
+ >> `+${date:%!.%.3}+` +
+ == `+1435683765.482+`
| `+${env:NAME}+` | 1.2
| 環境変数 `NAME` の値
diff --git a/doc/sr/weechat_plugin_api.sr.adoc b/doc/sr/weechat_plugin_api.sr.adoc
index da48a15c0..2ffb9e99e 100644
--- a/doc/sr/weechat_plugin_api.sr.adoc
+++ b/doc/sr/weechat_plugin_api.sr.adoc
@@ -2479,7 +2479,7 @@ str3 = weechat.string_input_for_buffer("//test") # "/test"
==== string_eval_expression
-_WeeChat ≥ 0.4.0, ажурирано у верзијама 0.4.2, 0.4.3, 1.0, 1.1, 1.2, 1.3, 1.6, 1.8, 2.0, 2.2, 2.3, 2.7, 2.9, 3.1, 3.2, 3.3, 3.4, 3.6 и 3.8, 4.0.0, 4.2.0._
+_WeeChat ≥ 0.4.0, ажурирано у верзијама 0.4.2, 0.4.3, 1.0, 1.1, 1.2, 1.3, 1.6, 1.8, 2.0, 2.2, 2.3, 2.7, 2.9, 3.1, 3.2, 3.3, 3.4, 3.6 и 3.8, 4.0.0, 4.2.0, 4.3.0._
Израчунава израз и враћа вредност као стринг. Специјалне променљиве у формату `+${променљива}+` се развијају (погледајте табелу испод).
@@ -3094,15 +3094,19 @@ str5 = weechat.string_eval_expression("password=abc password=def", {}, {}, optio
>> `+${base_decode:64,dGVzdCBzdHJpbmc=}+` +
== `+test string+`
+// TRANSLATION MISSING
| `+${date}+` +
`+${date:xxx}+` | 1.3
-| Текући датум/време, у прилагођеном формату (погледајте `man strftime`),
+| Текући датум/време, у прилагођеном формату (see function <<_util_strftimeval,util_strftimeval>>),
подразумевани формат је `%F %T`.
| >> `+${date}+` +
== `+2015-06-30 19:02:45+` +
+
>> `+${date:%H:%M:%S}+` +
- == `+19:02:45+`
+ == `+19:02:45+` +
+ +
+ >> `+${date:%!.%.3}+` +
+ == `+1435683765.482+`
| `+${env:ИМЕ}+` | 1.2
| Име променљиве окружења `ИМЕ`.
diff --git a/src/core/wee-eval.c b/src/core/wee-eval.c
index a1fc10538..4f0f928ef 100644
--- a/src/core/wee-eval.c
+++ b/src/core/wee-eval.c
@@ -28,6 +28,7 @@
#include <stdarg.h>
#include <regex.h>
#include <time.h>
+#include <sys/time.h>
#include "weechat.h"
#include "wee-eval.h"
@@ -39,6 +40,7 @@
#include "wee-secure.h"
#include "wee-string.h"
#include "wee-utf8.h"
+#include "wee-util.h"
#include "../gui/gui-buffer.h"
#include "../gui/gui-chat.h"
#include "../gui/gui-color.h"
@@ -988,19 +990,13 @@ char *
eval_string_date (const char *text)
{
char str_value[512];
- time_t date;
- struct tm *date_tmp;
+ struct timeval tv_now;
int rc;
- date = time (NULL);
- date_tmp = localtime (&date);
- if (!date_tmp)
- return strdup ("");
-
- rc = (int) strftime (str_value, sizeof (str_value),
- (text[0] == ':') ? text + 1 : "%F %T",
- date_tmp);
-
+ gettimeofday (&tv_now, NULL);
+ rc = util_strftimeval (str_value, sizeof (str_value),
+ (text[0] == ':') ? text + 1 : "%F %T",
+ &tv_now);
return strdup ((rc > 0) ? str_value : "");
}
diff --git a/tests/unit/core/test-core-eval.cpp b/tests/unit/core/test-core-eval.cpp
index b06a338d3..8dea0020b 100644
--- a/tests/unit/core/test-core-eval.cpp
+++ b/tests/unit/core/test-core-eval.cpp
@@ -26,6 +26,7 @@ extern "C"
#include <stdio.h>
#include <string.h>
#include <regex.h>
+#include <time.h>
#include "src/core/wee-eval.h"
#include "src/core/wee-config.h"
#include "src/core/wee-config-file.h"
@@ -453,8 +454,10 @@ TEST(CoreEval, EvalExpression)
{
struct t_hashtable *pointers, *extra_vars, *options;
struct t_config_option *ptr_option;
- char *value, str_value[256], str_expr[256];
+ char *value, str_value[256], str_expr[256], *error;
const char *ptr_debug_output;
+ long number;
+ time_t time_now;
pointers = hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
@@ -898,6 +901,13 @@ TEST(CoreEval, EvalExpression)
pointers, extra_vars, options);
LONGS_EQUAL(8, strlen (value));
free (value);
+ value = eval_expression ("${date:%!}", pointers, extra_vars, options);
+ CHECK(value);
+ error = NULL;
+ number = strtol (value, &error, 10);
+ CHECK(error && !error[0]);
+ time_now = time (NULL);
+ CHECK((number >= time_now - 10) && (number <= time_now + 10));
/* test ternary operator */
WEE_CHECK_EVAL("1", "${if:5>2}");