summaryrefslogtreecommitdiff
path: root/src/plugins
diff options
context:
space:
mode:
authorSebastien Helleu <flashcode@flashtux.org>2012-11-24 09:43:50 +0100
committerSebastien Helleu <flashcode@flashtux.org>2012-11-24 09:43:50 +0100
commit46a3d95aabd0b2c1c3fce0eaf18c852c87891af8 (patch)
tree40c7fe2e0497b0fed8ffe8332d19b159fc865788 /src/plugins
parent53b8cdfef328017b08bc575854d5f6a5ad6ef1d3 (diff)
downloadweechat-46a3d95aabd0b2c1c3fce0eaf18c852c87891af8.zip
irc: read timestamp or ISO 8601 date format in tag "time" (capability "server-time")
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/irc/irc-protocol.c46
1 files changed, 26 insertions, 20 deletions
diff --git a/src/plugins/irc/irc-protocol.c b/src/plugins/irc/irc-protocol.c
index 859986e5d..b4739af5a 100644
--- a/src/plugins/irc/irc-protocol.c
+++ b/src/plugins/irc/irc-protocol.c
@@ -23,6 +23,11 @@
* according to RFC 1459, 2810, 2811 and 2812
*/
+/* this define is needed for strptime() (not on OpenBSD) */
+#if !defined(__OpenBSD__)
+#define _XOPEN_SOURCE 700
+#endif
+
#ifndef __USE_XOPEN
#define __USE_XOPEN
#endif
@@ -4663,9 +4668,10 @@ time_t
irc_protocol_get_message_tag_time (struct t_hashtable *tags)
{
const char *tag_time;
- char *tag_time2, *pos, *error;
- int value;
time_t time_value;
+ struct tm tm_date;
+
+ tzset();
if (!tags)
return 0;
@@ -4673,25 +4679,25 @@ irc_protocol_get_message_tag_time (struct t_hashtable *tags)
time_value = 0;
tag_time = weechat_hashtable_get (tags, "time");
- if (tag_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, '-'))
{
- tag_time2 = strdup (tag_time);
- if (tag_time2)
- {
- pos = tag_time2;
- while (isdigit ((unsigned char)pos[0]))
- {
- pos++;
- }
- pos[0] = '\0';
- if (tag_time2[0])
- {
- value = (int)strtol (tag_time2, &error, 10);
- if (error && !error[0] && (value >= 0))
- time_value = (time_t)value;
- }
- free (tag_time2);
- }
+ /* date is with ISO 8601 format: "2012-11-24T07:41:02.018Z" */
+ strptime (tag_time, "%FT%T%z", &tm_date);
+ if (tm_date.tm_year > 0)
+ time_value = mktime (&tm_date) - timezone;
+ }
+ 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);
}
return time_value;