1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-30 14:56:39 +02:00

irc: remove function irc_protocol_parse_time, use API function weechat_util_parse_time (issue #649)

This commit is contained in:
Sébastien Helleu
2024-01-05 19:28:47 +01:00
parent f6ba789c3d
commit 4a1e5e7eaa
2 changed files with 5 additions and 154 deletions
+5 -105
View File
@@ -372,108 +372,6 @@ irc_protocol_nick_address (struct t_irc_server *server,
return string;
}
/*
* Parses date/time received in a "time" tag and sets *date to the date/time
* and *date_usec to the microseconds of date (value is set to 0 for both in
* case of error).
*/
void
irc_protocol_parse_time (const char *time, time_t *date, int *date_usec)
{
time_t time_msg, time_gm, time_local;
struct tm tm_date, tm_date_gm, tm_date_local;
char *time2, *pos, *pos2, *pos_msec, *error, str_msec[16];
long value;
int length;
if (!date || !date_usec)
return;
*date = 0;
*date_usec = 0;
if (!time || !time[0])
return;
time2 = strdup (time);
if (!time2)
return;
pos_msec = NULL;
if (strchr (time2, '-'))
{
/* date is with ISO 8601 format: "2012-11-24T07:41:02.018Z" */
/* initialize structure, because strptime does not do it */
memset (&tm_date, 0, sizeof (struct tm));
pos = strptime (time2, "%Y-%m-%dT%H:%M:%S", &tm_date);
if (pos && (tm_date.tm_year > 0))
{
time_msg = mktime (&tm_date);
gmtime_r (&time_msg, &tm_date_gm);
localtime_r (&time_msg, &tm_date_local);
time_gm = mktime (&tm_date_gm);
time_local = mktime (&tm_date_local);
*date = mktime (&tm_date_local) + (time_local - time_gm);
if (pos[0] == '.')
{
pos++;
pos2 = strchr (pos, 'Z');
if (pos2)
pos2[0] = '\0';
pos_msec = pos;
}
}
}
else
{
/* date is with timestamp format: "1353403519.478" or "1353403519,478" */
pos = strchr (time2, '.');
if (pos)
{
pos[0] = '\0';
}
else
{
pos = strchr (time2, ',');
if (pos)
pos[0] = '\0';
}
error = NULL;
value = strtol (time2, &error, 10);
if (error && !error[0] && (value >= 0))
*date = (int)value;
if (pos && pos[1])
pos_msec = pos + 1;
}
if (pos_msec)
{
length = strlen (pos_msec);
if (length > 6)
length = 6;
memcpy (str_msec, pos_msec, length);
str_msec[length] = '\0';
while (strlen (str_msec) < 6)
{
strcat (str_msec, "0");
}
error = NULL;
value = strtol (str_msec, &error, 10);
if (error && !error[0])
{
if (value < 0)
value = 0;
else if (value > 999999)
value = 999999;
*date_usec = (int)value;
}
}
free (time2);
}
/*
* Builds a string with concatenation of IRC command parameters, from
* arg_start to arg_end.
@@ -8191,6 +8089,7 @@ irc_protocol_recv_command (struct t_irc_server *server,
const char *nick1, *address1, *host1;
char *address, *host, *host_no_color;
struct t_irc_protocol_ctxt ctxt;
struct timeval tv;
struct t_irc_protocol_msg irc_protocol_messages[] = {
/* format: "command", decode_color, keep_trailing_spaces, func_cb */
@@ -8409,10 +8308,11 @@ irc_protocol_recv_command (struct t_irc_server *server,
if (ctxt.tags)
{
irc_tag_parse (tags, ctxt.tags, NULL);
irc_protocol_parse_time (
weechat_util_parse_time (
weechat_hashtable_get (ctxt.tags, "time"),
&(ctxt.date),
&(ctxt.date_usec));
&tv);
ctxt.date = tv.tv_sec;
ctxt.date_usec = tv.tv_usec;
}
free (tags);
}
@@ -57,8 +57,6 @@ extern const char *irc_protocol_nick_address (struct t_irc_server *server,
struct t_irc_nick *nick,
const char *nickname,
const char *address);
extern void irc_protocol_parse_time (const char *time, time_t *date,
int *date_usec);
extern char *irc_protocol_string_params (const char **params,
int arg_start, int arg_end);
extern char *irc_protocol_cap_to_enable (const char *capabilities,
@@ -92,14 +90,6 @@ extern char *irc_protocol_cap_to_enable (const char *capabilities,
__extra_tags)); \
}
#define WEE_CHECK_PARSE_TIME(__result_date, __result_date_usec, \
__time) \
date = 1; \
date_usec = 1; \
irc_protocol_parse_time (__time, &date, &date_usec); \
LONGS_EQUAL(__result_date, date); \
LONGS_EQUAL(__result_date_usec, date_usec);
#define WEE_CHECK_CAP_TO_ENABLE(__result, __string, __sasl_requested) \
str = irc_protocol_cap_to_enable (__string, __sasl_requested); \
STRCMP_EQUAL(__result, str); \
@@ -593,45 +583,6 @@ TEST(IrcProtocolWithServer, Tags)
free (ctxt.address);
}
/*
* Tests functions:
* irc_protocol_parse_time
*/
TEST(IrcProtocol, ParseTime)
{
time_t date;
int date_usec;
/* invalid time formats */
WEE_CHECK_PARSE_TIME(0, 0, NULL);
WEE_CHECK_PARSE_TIME(0, 0, "");
WEE_CHECK_PARSE_TIME(0, 0, "invalid");
/* incomplete time formats */
WEE_CHECK_PARSE_TIME(0, 0, "2019-01");
WEE_CHECK_PARSE_TIME(0, 0, "2019-01-13");
WEE_CHECK_PARSE_TIME(0, 0, "2019-01-13T14");
WEE_CHECK_PARSE_TIME(0, 0, "2019-01-13T14:37");
/* valid time with ISO 8601 format*/
WEE_CHECK_PARSE_TIME(1547386699, 0, "2019-01-13T13:38:19");
WEE_CHECK_PARSE_TIME(1547386699, 0, "2019-01-13T13:38:19Z");
WEE_CHECK_PARSE_TIME(1547386699, 123, "2019-01-13T13:38:19.000123");
WEE_CHECK_PARSE_TIME(1547386699, 123, "2019-01-13T13:38:19.000123Z");
WEE_CHECK_PARSE_TIME(1547386699, 123000, "2019-01-13T13:38:19.123");
WEE_CHECK_PARSE_TIME(1547386699, 123000, "2019-01-13T13:38:19.123Z");
WEE_CHECK_PARSE_TIME(1547386699, 123456, "2019-01-13T13:38:19.123456");
WEE_CHECK_PARSE_TIME(1547386699, 123456, "2019-01-13T13:38:19.123456789");
/* valid time as timestamp */
WEE_CHECK_PARSE_TIME(1547386699, 0, "1547386699");
WEE_CHECK_PARSE_TIME(1547386699, 123, "1547386699.000123");
WEE_CHECK_PARSE_TIME(1547386699, 123000, "1547386699.123");
WEE_CHECK_PARSE_TIME(1547386699, 123456, "1547386699.123456");
WEE_CHECK_PARSE_TIME(1547386699, 123456, "1547386699.123456789");
}
/*
* Tests functions:
* irc_protocol_string_params