Index: apps/app_sayunixtime.c =================================================================== RCS file: /usr/cvsroot/asterisk/apps/app_sayunixtime.c,v retrieving revision 1.8 diff -u -r1.8 app_sayunixtime.c --- apps/app_sayunixtime.c 21 Apr 2005 06:02:43 -0000 1.8 +++ apps/app_sayunixtime.c 5 May 2005 03:55:53 -0000 @@ -22,6 +22,7 @@ #include "asterisk/pbx.h" #include "asterisk/module.h" #include "asterisk/say.h" +#include "asterisk/localtime.h" static char *tdesc = "Say time"; @@ -107,11 +108,63 @@ return res; } +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 ""; +} + +static struct ast_custom_function_obj strftime_function = { + .name = "STRFTIME", + .desc = "Returns the current date/time in a specified format.", + .syntax = "STRFTIME([][,[timezone][,format]])", + .read = acf_strftime, + .write = NULL, +}; + int unload_module(void) { int res; STANDARD_HANGUP_LOCALUSERS; res = ast_unregister_application(app_sayunixtime); + res |= ast_custom_function_unregister(&strftime_function); if (! res) return ast_unregister_application(app_datetime); else @@ -122,6 +175,7 @@ { int res; res = ast_register_application(app_sayunixtime, sayunixtime_exec, sayunixtime_synopsis, sayunixtime_descrip); + res |= ast_custom_function_register(&strftime_function); if (! res) return ast_register_application(app_datetime, sayunixtime_exec, sayunixtime_synopsis, datetime_descrip); else