Asterisk data retrieval API.
[asterisk/asterisk.git] / apps / app_sayunixtime.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (c) 2003, 2006 Tilghman Lesher.  All rights reserved.
5  * Copyright (c) 2006 Digium, Inc.
6  *
7  * Tilghman Lesher <app_sayunixtime__200309@the-tilghman.com>
8  *
9  * This code is released by the author with no restrictions on usage.
10  *
11  * See http://www.asterisk.org for more information about
12  * the Asterisk project. Please do not directly contact
13  * any of the maintainers of this project for assistance;
14  * the project provides a web site, mailing lists and IRC
15  * channels for your use.
16  *
17  */
18
19 /*! \file
20  *
21  * \brief SayUnixTime application
22  *
23  * \author Tilghman Lesher <app_sayunixtime__200309@the-tilghman.com>
24  * 
25  * \ingroup applications
26  */
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/file.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/module.h"
36 #include "asterisk/say.h"
37 #include "asterisk/app.h"
38
39 /*** DOCUMENTATION
40         <application name="SayUnixTime" language="en_US">
41                 <synopsis>
42                         Says a specified time in a custom format.
43                 </synopsis>
44                 <syntax>
45                         <parameter name="unixtime">
46                                 <para>time, in seconds since Jan 1, 1970.  May be negative. Defaults to now.</para>
47                         </parameter>
48                         <parameter name="timezone">
49                                 <para>timezone, see <directory>/usr/share/zoneinfo</directory> for a list. Defaults to machine default.</para>
50                         </parameter>
51                         <parameter name="format">
52                                 <para>a format the time is to be said in.  See <filename>voicemail.conf</filename>.
53                                 Defaults to <literal>ABdY "digits/at" IMp</literal></para>
54                         </parameter>
55                 </syntax>
56                 <description>
57                         <para>Uses some of the sound files stored in <directory>/var/lib/asterisk/sounds</directory> to construct a phrase 
58                         saying the specified date and/or time in the specified format. </para>
59                 </description>
60                 <see-also>
61                         <ref type="function">STRFTIME</ref>
62                         <ref type="function">STRPTIME</ref>
63                         <ref type="function">IFTIME</ref>
64                 </see-also>
65         </application>
66         <application name="DateTime" language="en_US">
67                 <synopsis>
68                         Says a specified time in a custom format.
69                 </synopsis>
70                 <syntax>
71                         <parameter name="unixtime">
72                                 <para>time, in seconds since Jan 1, 1970.  May be negative. Defaults to now.</para>
73                         </parameter>
74                         <parameter name="timezone">
75                                 <para>timezone, see <filename>/usr/share/zoneinfo</filename> for a list. Defaults to machine default.</para>
76                         </parameter>
77                         <parameter name="format">
78                                 <para>a format the time is to be said in.  See <filename>voicemail.conf</filename>.
79                                 Defaults to <literal>ABdY "digits/at" IMp</literal></para>
80                         </parameter>
81                 </syntax>
82                 <description>
83                         <para>Say the date and time in a specified format.</para>
84                 </description>
85         </application>
86
87  ***/
88
89 static char *app_sayunixtime = "SayUnixTime";
90 static char *app_datetime = "DateTime";
91
92 static int sayunixtime_exec(struct ast_channel *chan, const char *data)
93 {
94         AST_DECLARE_APP_ARGS(args,
95                 AST_APP_ARG(timeval);
96                 AST_APP_ARG(timezone);
97                 AST_APP_ARG(format);
98         );
99         char *parse;
100         int res = 0;
101         time_t unixtime;
102         
103         if (!data)
104                 return 0;
105
106         parse = ast_strdupa(data);
107
108         AST_STANDARD_APP_ARGS(args, parse);
109
110         ast_get_time_t(args.timeval, &unixtime, time(NULL), NULL);
111
112         if (chan->_state != AST_STATE_UP)
113                 res = ast_answer(chan);
114
115         if (!res)
116                 res = ast_say_date_with_format(chan, unixtime, AST_DIGIT_ANY,
117                                                chan->language, args.format, args.timezone);
118
119         return res;
120 }
121
122 static int unload_module(void)
123 {
124         int res;
125         
126         res = ast_unregister_application(app_sayunixtime);
127         res |= ast_unregister_application(app_datetime);
128         
129         return res;
130 }
131
132 static int load_module(void)
133 {
134         int res;
135         
136         res = ast_register_application_xml(app_sayunixtime, sayunixtime_exec);
137         res |= ast_register_application_xml(app_datetime, sayunixtime_exec);
138         
139         return res;
140 }
141
142 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Say time");