[Asterisk-cvs] asterisk/funcs func_strings.c,1.1,1.2

kpfleming at lists.digium.com kpfleming at lists.digium.com
Thu May 5 01:43:14 CDT 2005


Update of /usr/cvsroot/asterisk/funcs
In directory mongoose.digium.com:/tmp/cvs-serv19360/funcs

Modified Files:
	func_strings.c 
Log Message:
add STRFTIME dialplan function (bug #4098, modified to use new funcs subdirectory)


Index: func_strings.c
===================================================================
RCS file: /usr/cvsroot/asterisk/funcs/func_strings.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- func_strings.c	5 May 2005 05:39:33 -0000	1.1
+++ func_strings.c	5 May 2005 05:50:11 -0000	1.2
@@ -19,6 +19,7 @@
 #include "asterisk/logger.h"
 #include "asterisk/utils.h"
 #include "asterisk/app.h"
+#include "asterisk/localtime.h"
 
 static char *function_fieldqty(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
 {
@@ -115,3 +116,56 @@
 	.syntax = "LEN(<string>)",
 	.read = builtin_function_len,
 };
+
+static char *acf_strftime(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
+{
+	char *format, *epoch, *timezone;
+	long epochi;
+	struct timeval tv;
+	struct tm time;
+
+	if (data) {
+		format = ast_strdupa(data);
+		if (format) {
+			epoch = strsep(&format, "|");
+			timezone = strsep(&format, "|");
+
+			if (epoch && !ast_strlen_zero(epoch) && sscanf(epoch, "%ld", &epochi) == 1) {
+			} else if (!gettimeofday(&tv, NULL)) {
+				epochi = tv.tv_sec;
+			} else {
+				ast_log(LOG_ERROR, "Cannot gettimeofday() ?!!\n");
+				return "";
+			}
+
+			ast_localtime(&epochi, &time, timezone);
+
+			if (!format) {
+				format = "%c";
+			}
+
+			buf[0] = '\0';
+			if (! strftime(buf, len, format, &time)) {
+				ast_log(LOG_WARNING, "C function strftime() output nothing?!!\n");
+			}
+			buf[len - 1] = '\0';
+
+			return buf;
+		} else {
+			ast_log(LOG_ERROR, "Out of memory\n");
+		}
+	} else {
+		ast_log(LOG_ERROR, "Asterisk function STRFTIME() requires an argument.\n");
+	}
+	return "";
+}
+
+#ifndef BUILTIN_FUNC
+static
+#endif
+struct ast_custom_function strftime_function = {
+	.name = "STRFTIME",
+	.synopsis = "Returns the current date/time in a specified format.",
+	.syntax = "STRFTIME([<epoch>][,[timezone][,format]])",
+	.read = acf_strftime,
+};




More information about the svn-commits mailing list