summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2024-01-27 11:52:54 +0100
committerSébastien Helleu <flashcode@flashtux.org>2024-01-27 16:53:53 +0100
commite3af6a91d4e7b2de38c0b60b5c1bb0344dc00e02 (patch)
tree9815d4e2c4cc888b565c1bc24d6e1da4364b2d58
parent73a1c9753a9e5f996046abde2b797ab0af5b349b (diff)
downloadweechat-e3af6a91d4e7b2de38c0b60b5c1bb0344dc00e02.zip
api: add support of specifier `%!` for timestamp in function util_strftimeval
-rw-r--r--ChangeLog.adoc1
-rw-r--r--doc/en/weechat_plugin_api.en.adoc5
-rw-r--r--doc/fr/weechat_plugin_api.fr.adoc5
-rw-r--r--doc/it/weechat_plugin_api.it.adoc5
-rw-r--r--doc/ja/weechat_plugin_api.ja.adoc5
-rw-r--r--doc/sr/weechat_plugin_api.sr.adoc9
-rw-r--r--src/core/wee-util.c28
-rw-r--r--tests/unit/core/test-core-util.cpp8
8 files changed, 44 insertions, 22 deletions
diff --git a/ChangeLog.adoc b/ChangeLog.adoc
index 789f8d2da..ab9ad98f1 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`
+ * 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 a2ebe85ce..4567c9bc4 100644
--- a/doc/en/weechat_plugin_api.en.adoc
+++ b/doc/en/weechat_plugin_api.en.adoc
@@ -4698,10 +4698,10 @@ This function is not available in scripting API.
==== util_strftimeval
-_WeeChat ≥ 4.2.0._
+_WeeChat ≥ 4.2.0, updated in 4.3.0._
Format date and time like function `strftime` in C library, using `struct timeval`
-as input, and supporting extra specifiers for microseconds.
+as input, and supporting extra specifiers.
Prototype:
@@ -4718,6 +4718,7 @@ Arguments:
** `%.N` where `N` is between 1 and 6: zero-padded microseconds on N digits
(for example `%.3` for milliseconds)
** `%f`: alias of `%.6`
+** `%!`: timestamp as integer, in seconds (value of tv->tv_sec)
Return value:
diff --git a/doc/fr/weechat_plugin_api.fr.adoc b/doc/fr/weechat_plugin_api.fr.adoc
index 7292b3a81..5a8c81b2e 100644
--- a/doc/fr/weechat_plugin_api.fr.adoc
+++ b/doc/fr/weechat_plugin_api.fr.adoc
@@ -4777,11 +4777,11 @@ Cette fonction n'est pas disponible dans l'API script.
==== util_strftimeval
-_WeeChat ≥ 4.2.0._
+_WeeChat ≥ 4.2.0, mis à jour dans la 4.3.0._
Formatter la date et l'heure comme la fonction `strftime` de la bibliothèque C,
en utilisant un `struct timeval` en entrée et en supportant des caractères de
-conversion pour les microsecondes.
+conversion supplémentaires.
Prototype :
@@ -4799,6 +4799,7 @@ Paramètres :
** `%.N` où `N` est entre 1 and 6: microsecondes remplies avec des zéros sur
N chiffres (par exemple `%.3` pour les millisecondes)
** `%f` : alias de `%.6`
+** `%!` : horodatage sous forme d'entier, en secondes (valeur de tv->tv_sec)
Valeur de retour :
diff --git a/doc/it/weechat_plugin_api.it.adoc b/doc/it/weechat_plugin_api.it.adoc
index ff1e98de2..2f47ef3ce 100644
--- a/doc/it/weechat_plugin_api.it.adoc
+++ b/doc/it/weechat_plugin_api.it.adoc
@@ -4916,10 +4916,10 @@ Questa funzione non è disponibile nelle API per lo scripting.
// TRANSLATION MISSING
==== util_strftimeval
-_WeeChat ≥ 4.2.0._
+_WeeChat ≥ 4.2.0, updated in 4.3.0._
Format date and time like function `strftime` in C library, using `struct timeval`
-as input, and supporting extra specifiers for microseconds.
+as input, and supporting extra specifiers.
Prototype:
@@ -4936,6 +4936,7 @@ Arguments:
** `%.N` where `N` is between 1 and 6: zero-padded microseconds on N digits
(for example `%.3` for milliseconds)
** `%f`: alias of `%.6`
+** `%!`: timestamp as integer, in seconds (value of tv->tv_sec)
Return value:
diff --git a/doc/ja/weechat_plugin_api.ja.adoc b/doc/ja/weechat_plugin_api.ja.adoc
index d1c58c26c..eacaa9e23 100644
--- a/doc/ja/weechat_plugin_api.ja.adoc
+++ b/doc/ja/weechat_plugin_api.ja.adoc
@@ -4823,10 +4823,10 @@ weechat_printf (NULL, "date: %s",
// TRANSLATION MISSING
==== util_strftimeval
-_WeeChat ≥ 4.2.0._
+_WeeChat ≥ 4.2.0, updated in 4.3.0._
Format date and time like function `strftime` in C library, using `struct timeval`
-as input, and supporting extra specifiers for microseconds.
+as input, and supporting extra specifiers.
Prototype:
@@ -4843,6 +4843,7 @@ Arguments:
** `%.N` where `N` is between 1 and 6: zero-padded microseconds on N digits
(for example `%.3` for milliseconds)
** `%f`: alias of `%.6`
+** `%!`: timestamp as integer, in seconds (value of tv->tv_sec)
Return value:
diff --git a/doc/sr/weechat_plugin_api.sr.adoc b/doc/sr/weechat_plugin_api.sr.adoc
index adfe88946..da48a15c0 100644
--- a/doc/sr/weechat_plugin_api.sr.adoc
+++ b/doc/sr/weechat_plugin_api.sr.adoc
@@ -4559,10 +4559,11 @@ weechat_printf (NULL, "date: %s",
==== util_strftimeval
-_WeeChat ≥ 4.2.0._
+_WeeChat ≥ 4.2.0, ажурирано у 4.3.0._
-Форматира датум и време као функција `strftime` у C библиотеци, користећи `struct timeval`
-као улаз и уз подршку додатних спецификатора за микросекунде.
+// TRANSLATION MISSING
+Format date and time like function `strftime` in C library, using `struct timeval`
+as input, and supporting extra specifiers.
Прототип:
@@ -4579,6 +4580,8 @@ int weechat_util_strftimeval (char *string, int max, const char *format, struct
** `%.N` где је `N` између 1 и 6: микросекунде допуњене нулама на N цифара
(на пример `%.3` за милисекунде)
** `%f`: алијас за `%.6`
+// TRANSLATION MISSING
+** `%!`: timestamp as integer, in seconds (value of tv->tv_sec)
Повратна вредност:
diff --git a/src/core/wee-util.c b/src/core/wee-util.c
index abd149ae1..be4a868bd 100644
--- a/src/core/wee-util.c
+++ b/src/core/wee-util.c
@@ -164,16 +164,17 @@ util_get_time_string (const time_t *date)
}
/*
- * Formats date and time like strftime and adds additional formats for support
- * of microseconds: "%.N" where N is an integer between 1 and 6: first N
- * digits of microseconds (for example "%.3" is milliseconds, on 3 digits).
- * Format "%f" is an alias of "%.6" (microseconds, zero-padded to 6 digits).
+ * Formats date and time like strftime (but with timeval structure as input)
+ * and adds extra specifiers:
+ * - "%.1" to "%.6": first N digits of microseconds, zero-padded
+ * - "%f": alias of "%.6" (microseconds, zero-padded to 6 digits)
+ * - "%!": timestamp as integer, in seconds (value of tv->tv_sec)
*/
int
util_strftimeval (char *string, int max, const char *format, struct timeval *tv)
{
- char **format2, str_usec[32];
+ char **format2, str_temp[32];
const char *ptr_format;
struct tm *local_time;
int length, bytes;
@@ -201,18 +202,23 @@ util_strftimeval (char *string, int max, const char *format, struct timeval *tv)
else if ((ptr_format[0] == '%') && (ptr_format[1] == '.')
&& (ptr_format[2] >= '1') && (ptr_format[2] <= '6'))
{
- snprintf (str_usec, sizeof (str_usec),
+ snprintf (str_temp, sizeof (str_temp),
"%06ld", (long)(tv->tv_usec));
length = ptr_format[2] - '1' + 1;
- str_usec[length] = '\0';
- string_dyn_concat (format2, str_usec, -1);
+ str_temp[length] = '\0';
+ string_dyn_concat (format2, str_temp, -1);
ptr_format += 3;
}
else if ((ptr_format[0] == '%') && (ptr_format[1] == 'f'))
{
- snprintf (str_usec, sizeof (str_usec),
- "%06ld", (long)(tv->tv_usec));
- string_dyn_concat (format2, str_usec, -1);
+ snprintf (str_temp, sizeof (str_temp), "%06ld", (long)(tv->tv_usec));
+ string_dyn_concat (format2, str_temp, -1);
+ ptr_format += 2;
+ }
+ else if ((ptr_format[0] == '%') && (ptr_format[1] == '!'))
+ {
+ snprintf (str_temp, sizeof (str_temp), "%ld", (long)(tv->tv_sec));
+ string_dyn_concat (format2, str_temp, -1);
ptr_format += 2;
}
else
diff --git a/tests/unit/core/test-core-util.cpp b/tests/unit/core/test-core-util.cpp
index a7fc00e9d..5f5c0847b 100644
--- a/tests/unit/core/test-core-util.cpp
+++ b/tests/unit/core/test-core-util.cpp
@@ -236,6 +236,14 @@ TEST(CoreUtil, Strftimeval)
LONGS_EQUAL(23, util_strftimeval (str_time, sizeof (str_time),
"%Y-%m-%d %H:%M:%S.%.7", &tv));
STRCMP_EQUAL("2023-12-25 10:29:09.%.7", str_time);
+
+ /* timestamp */
+ strcpy (str_time, "test");
+ LONGS_EQUAL(10, util_strftimeval (str_time, sizeof (str_time), "%!", &tv));
+ STRCMP_EQUAL("1703500149", str_time);
+ strcpy (str_time, "test");
+ LONGS_EQUAL(17, util_strftimeval (str_time, sizeof (str_time), "%!.%f", &tv));
+ STRCMP_EQUAL("1703500149.456789", str_time);
}
/*