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

api: add support of specifier %! for timestamp in function util_strftimeval

This commit is contained in:
Sébastien Helleu
2024-01-27 11:52:54 +01:00
parent 73a1c9753a
commit e3af6a91d4
8 changed files with 44 additions and 22 deletions
+1
View File
@@ -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::
+3 -2
View File
@@ -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:
+3 -2
View File
@@ -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 :
+3 -2
View File
@@ -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:
+3 -2
View File
@@ -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:
+6 -3
View File
@@ -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)
Повратна вредност:
+17 -11
View File
@@ -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
+8
View File
@@ -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);
}
/*