...
 
Commits (3)
......@@ -37,7 +37,7 @@ Bug fixes::
Tests::
* unit: add tests on IRC configuration functions
* unit: add tests on IRC configuration and protocol functions
Build::
......
......@@ -6080,50 +6080,49 @@ irc_protocol_get_message_tags (const char *tags)
}
/*
* Gets value of time in tags.
* Parses date/time received in a "time" tag.
*
* Returns value of tag "time", 0 if not found.
* Returns value of time (timestamp), 0 if error.
*/
time_t
irc_protocol_get_message_tag_time (struct t_hashtable *tags)
irc_protocol_parse_time (const char *time)
{
const char *tag_time;
time_t time_value, time_msg, time_gm, time_local;
struct tm tm_date, tm_date_gm, tm_date_local;
if (!tags)
if (!time || !time[0])
return 0;
time_value = 0;
tag_time = weechat_hashtable_get (tags, "time");
if (!tag_time)
return time_value;
/* initialize structure, because strptime does not do it */
memset (&tm_date, 0, sizeof (struct tm));
if (strchr (tag_time, '-'))
if (strchr (time, '-'))
{
/* date is with ISO 8601 format: "2012-11-24T07:41:02.018Z" */
strptime (tag_time, "%Y-%m-%dT%H:%M:%S", &tm_date);
if (tm_date.tm_year > 0)
if (strptime (time, "%Y-%m-%dT%H:%M:%S", &tm_date))
{
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);
time_value = mktime (&tm_date_local) + (time_local - time_gm);
if (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);
time_value = mktime (&tm_date_local) + (time_local - time_gm);
}
}
}
else
{
/* date is with timestamp format: "1353403519.478" */
strptime (tag_time, "%s", &tm_date);
if (tm_date.tm_year > 0)
time_value = mktime (&tm_date);
if (strptime (time, "%s", &tm_date))
{
if (tm_date.tm_year > 0)
time_value = mktime (&tm_date);
}
}
return time_value;
......@@ -6329,7 +6328,10 @@ irc_protocol_recv_command (struct t_irc_server *server,
{
hash_tags = irc_protocol_get_message_tags (tags);
if (hash_tags)
date = irc_protocol_get_message_tag_time (hash_tags);
{
date = irc_protocol_parse_time (
weechat_hashtable_get (hash_tags, "time"));
}
free (tags);
}
ptr_msg_after_tags = pos_space;
......
......@@ -85,6 +85,7 @@ struct t_irc_protocol_msg
extern const char *irc_protocol_tags (const char *command, const char *tags,
const char *nick, const char *address);
extern time_t irc_protocol_parse_time (const char *time);
extern void irc_protocol_recv_command (struct t_irc_server *server,
const char *irc_message,
const char *msg_command,
......
......@@ -45,6 +45,7 @@ add_library(weechat_unit_tests_core STATIC ${LIB_WEECHAT_UNIT_TESTS_CORE_SRC})
# unit tests (plugins)
set(LIB_WEECHAT_UNIT_TESTS_PLUGINS_SRC
unit/plugins/irc/test-irc-config.cpp
unit/plugins/irc/test-irc-protocol.cpp
)
add_library(weechat_unit_tests_plugins MODULE ${LIB_WEECHAT_UNIT_TESTS_PLUGINS_SRC})
......
......@@ -61,7 +61,8 @@ tests_SOURCES = tests.cpp \
lib_LTLIBRARIES = lib_weechat_unit_tests_plugins.la
lib_weechat_unit_tests_plugins_la_SOURCES = unit/plugins/irc/test-irc-config.cpp
lib_weechat_unit_tests_plugins_la_SOURCES = unit/plugins/irc/test-irc-config.cpp \
unit/plugins/irc/test-irc-protocol.cpp
lib_weechat_unit_tests_plugins_la_LDFLAGS = -module -no-undefined
......
/*
* test-irc-protocol.cpp - test IRC protocol functions
*
* Copyright (C) 2019 Sébastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* WeeChat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CppUTest/TestHarness.h"
extern "C"
{
#include "src/plugins/irc/irc-protocol.h"
}
TEST_GROUP(IrcProtocol)
{
};
/*
* Tests functions:
* irc_protocol_parse_time
*/
TEST(IrcProtocol, ParseTime)
{
/* invalid time formats */
LONGS_EQUAL(0, irc_protocol_parse_time (NULL));
LONGS_EQUAL(0, irc_protocol_parse_time (""));
LONGS_EQUAL(0, irc_protocol_parse_time ("invalid"));
/* incomplete time formats */
LONGS_EQUAL(0, irc_protocol_parse_time ("2019-01"));
LONGS_EQUAL(0, irc_protocol_parse_time ("2019-01-13"));
LONGS_EQUAL(0, irc_protocol_parse_time ("2019-01-13T14"));
LONGS_EQUAL(0, irc_protocol_parse_time ("2019-01-13T14:37"));
/* valid time with ISO 8601 format*/
LONGS_EQUAL(1547386699, irc_protocol_parse_time ("2019-01-13T13:38:19.123Z"));
LONGS_EQUAL(1547386699, irc_protocol_parse_time ("2019-01-13T13:38:19.123"));
LONGS_EQUAL(1547386699, irc_protocol_parse_time ("2019-01-13T13:38:19"));
/* valid time as timestamp */
LONGS_EQUAL(1547386699, irc_protocol_parse_time ("1547386699.123"));
LONGS_EQUAL(1547386699, irc_protocol_parse_time ("1547386699"));
}