Bug 6262 - New function STRPTIME
authorTilghman Lesher <tilghman@meg.abyt.es>
Sun, 22 Jan 2006 08:09:02 +0000 (08:09 +0000)
committerTilghman Lesher <tilghman@meg.abyt.es>
Sun, 22 Jan 2006 08:09:02 +0000 (08:09 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@8428 65c4cc65-6c06-0410-ace0-fbb531ad65f3

funcs/func_strings.c

index 0e315b7..dba306c 100644 (file)
@@ -296,6 +296,57 @@ struct ast_custom_function strftime_function = {
        .read = acf_strftime,
 };
 
+static char *acf_strptime(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
+{
+       AST_DECLARE_APP_ARGS(args,
+               AST_APP_ARG(timestring);
+               AST_APP_ARG(timezone);
+               AST_APP_ARG(format);
+       );
+       struct tm time;
+
+       memset(&time, 0, sizeof(struct tm));
+       
+       buf[0] = '\0';
+
+       if (!data) {
+               ast_log(LOG_ERROR, "Asterisk function STRPTIME() requires an argument.\n");
+               return buf;
+       }
+
+       AST_STANDARD_APP_ARGS(args, data);
+
+       if (ast_strlen_zero(args.format) ) {
+               ast_log(LOG_ERROR, "No format supplied to STRPTIME(<timestring>|<timezone>|<format>)");
+               return buf;
+       }
+                       
+       if (!strptime(args.timestring, args.format, &time)) {
+               ast_log(LOG_WARNING, "C function strptime() output nothing?!!\n");
+       } else {
+               snprintf(buf, len, "%d", (int)ast_mktime(&time, args.timezone));          
+       }
+       
+       return buf;
+}
+
+#ifndef BUILTIN_FUNC
+static
+#endif
+struct ast_custom_function strptime_function = {
+       .name = "STRPTIME",
+       .synopsis = "Returns the epoch of the arbitrary date/time string structured as described in the format.",
+       .syntax = "STRPTIME(<datetime>|<timezone>|<format>)",
+       .desc =
+"This is useful for converting a date into an EPOCH time, possibly to pass to\n"
+"an application like SayUnixTime or to calculate the difference between two\n"
+"date strings.\n"
+"\n"
+"Example:\n"
+"  ${STRPTIME(2006-03-01 07:30:35|America/Chicago|%Y-%m-%d %H:%M:%S)} returns 1141219835\n",
+       .read = acf_strptime,
+};
+
 static char *function_eval(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
 {
        memset(buf, 0, len);