]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
move mod_timezone functions into core (part 2/3)
authorMichael Jerris <mike@jerris.com>
Mon, 29 Sep 2008 16:58:31 +0000 (16:58 +0000)
committerMichael Jerris <mike@jerris.com>
Mon, 29 Sep 2008 16:58:31 +0000 (16:58 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@9738 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/include/switch_core.h
src/mod/applications/mod_timezone/mod_timezone.c
src/switch_time.c

index cc4fa430be3dca0bde9d1cee1dd4e29cb0e20b7c..a40fec8cbdd2b74f0b2493c65866c7a3181703fe 100644 (file)
@@ -1645,7 +1645,7 @@ SWITCH_DECLARE(void) switch_core_memory_reclaim_all(void);
 SWITCH_DECLARE(void) switch_core_setrlimits(void);
 SWITCH_DECLARE(void) switch_time_sync(void);
 SWITCH_DECLARE(time_t) switch_timestamp(time_t *t);
-SWITCH_DECLARE(void) switch_tztime(const time_t * const timep, const char *tzstring, struct tm * const tmp );
+SWITCH_DECLARE(switch_status_t) switch_strftime_tz(const char *tz, const char *format, char *date, size_t len);
 SWITCH_DECLARE(void) switch_load_network_lists(switch_bool_t reload);
 SWITCH_DECLARE(switch_bool_t) switch_check_network_list_ip_token(const char *ip_str, const char *list_name, const char **token);
 #define switch_check_network_list_ip(_ip_str, _list_name) switch_check_network_list_ip_token(_ip_str, _list_name, NULL)
index b8478eb1f11d90dade99948959fe508fd4ac8c6c..dee2879b4ed2d3fc3b0e337efb6cae71ff04147c 100644 (file)
 
 #include <switch.h>
 
-/* 
-   This converts a struct tm to a switch_time_exp_t
-   We have to use UNIX structures to do our exams
-   and use switch_* functions for the output.
-*/
-
-static void tm2switchtime(struct tm * tm, switch_time_exp_t *xt ) 
-{
-
-       if (!xt || !tm) {
-           return;
-       }
-       memset( xt, 0, sizeof(xt) );
-
-       xt->tm_sec      = tm->tm_sec;
-       xt->tm_min      = tm->tm_min;
-       xt->tm_hour     = tm->tm_hour;
-       xt->tm_mday     = tm->tm_mday;
-       xt->tm_mon      = tm->tm_mon;
-       xt->tm_year     = tm->tm_year;
-       xt->tm_wday     = tm->tm_wday;
-       xt->tm_yday     = tm->tm_yday;
-       xt->tm_isdst    = tm->tm_isdst;
-#if !defined(WIN32) && !defined(__SVR4) && !defined(__sun)
-
-       xt->tm_gmtoff   = tm->tm_gmtoff;
-#endif
-
-       return;
-}
-
-/* **************************************************************************
-   LOADING OF THE XML DATA - HASH TABLE & MEMORY POOL MANAGEMENT
-   ************************************************************************** */
-
-typedef struct {
-       switch_memory_pool_t *pool;
-       switch_hash_t *hash;
-} switch_timezones_list_t;
-
-static switch_timezones_list_t TIMEZONES_LIST = { 0 };
-static switch_event_node_t *NODE = NULL;
-
-const char *switch_lookup_timezone( const char *tz_name )
-{
-       char *value = NULL;
-
-       if ( tz_name && (value = switch_core_hash_find(TIMEZONES_LIST.hash, tz_name))==NULL ) {
-           switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Timezone '%s' not found!\n", tz_name);
-       }
-       
-       return value;
-}
-
-void switch_load_timezones(switch_bool_t reload)
-{
-       switch_xml_t xml = NULL, x_lists = NULL, x_list = NULL, cfg = NULL;
-       unsigned total = 0;
-
-       if (TIMEZONES_LIST.hash) {
-               switch_core_hash_destroy(&TIMEZONES_LIST.hash);
-       }
-
-       if (TIMEZONES_LIST.pool) {
-               switch_core_destroy_memory_pool(&TIMEZONES_LIST.pool);
-       }
-
-       memset(&TIMEZONES_LIST, 0, sizeof(TIMEZONES_LIST));
-       switch_core_new_memory_pool(&TIMEZONES_LIST.pool);
-       switch_core_hash_init(&TIMEZONES_LIST.hash, TIMEZONES_LIST.pool);
-
-       if ((xml = switch_xml_open_cfg("timezones.conf", &cfg, NULL))) {
-               if ((x_lists = switch_xml_child(cfg, "timezones"))) {
-                       for (x_list = switch_xml_child(x_lists, "zone"); x_list; x_list = x_list->next) {
-                               const char *name = switch_xml_attr(x_list, "name");
-                               const char *value= switch_xml_attr(x_list, "value");
-
-                               if (switch_strlen_zero(name)) {
-                                       continue;
-                               }
-
-                               if (switch_strlen_zero(value)) {
-                                       continue;
-                               }
-
-                               switch_core_hash_insert(TIMEZONES_LIST.hash, 
-                                                                               name, 
-                                                                               switch_core_strdup(TIMEZONES_LIST.pool, value) );
-                               total++;
-                       }
-               }
-               
-               switch_xml_free(xml);
-       }
-       
-       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Timezone %sloaded %d definitions\n", reload ? "re" : "", total);
-}
 
 /* **************************************************************************
    API FUNCTIONS AND COMMANDS
@@ -131,41 +34,19 @@ void switch_load_timezones(switch_bool_t reload)
 
 SWITCH_STANDARD_API(strftime_tz_api_function)
 {
-       switch_time_t thetime;
-       time_t timep;
-
        char *format = NULL;
-       const char *tz_name;
-       const char *tzdef;
-
-       switch_size_t retsize;
+       const char *tz_name = NULL;
        char date[80] = "";
 
-       struct tm tm;
-       switch_time_exp_t stm;
-
-       thetime = switch_timestamp_now();
-
-       timep =  (thetime) / (int64_t) (1000000);
-
        if (!switch_strlen_zero(cmd)) {
                format = strchr(cmd, ' ');
                tz_name = cmd;
                if (format) {
                        *format++ = '\0';
                }
-
-               tzdef = switch_lookup_timezone( tz_name );
-       } else {
-               /* We set the default timezone to GMT. */
-               tz_name="GMT";
-               tzdef="GMT";
        }
        
-       if (tzdef) { /* The lookup of the zone may fail. */
-               switch_tztime( &timep, tzdef, &tm );
-               tm2switchtime( &tm, &stm );
-               switch_strftime(date, &retsize, sizeof(date), switch_strlen_zero(format) ? "%Y-%m-%d %T" : format, &stm);
+       if (switch_strftime_tz(tz_name, format, date, sizeof(date)) == SWITCH_STATUS_SUCCESS) { /* The lookup of the zone may fail. */
                stream->write_function(stream, "%s", date);
        } else {
                stream->write_function(stream, "-ERR Invalid Timezone\n");
@@ -174,6 +55,7 @@ SWITCH_STANDARD_API(strftime_tz_api_function)
        return SWITCH_STATUS_SUCCESS;
 }
 
+
 /* **************************************************************************
 
 ************************************************************************** */
@@ -183,20 +65,10 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_timezone_load);
 SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_timezone_shutdown);
 SWITCH_MODULE_DEFINITION(mod_timezone, mod_timezone_load, mod_timezone_shutdown, NULL);
 
-static void event_handler(switch_event_t *event)
-{
-       switch_load_timezones(1);
-}
-
 SWITCH_MODULE_LOAD_FUNCTION(mod_timezone_load)
 {
        switch_api_interface_t *api_interface;
        
-       if ((switch_event_bind_removable(modname, SWITCH_EVENT_RELOADXML, NULL, event_handler, NULL, &NODE) != SWITCH_STATUS_SUCCESS)) {
-               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't bind!\n");
-       }
-       switch_load_timezones(0);
-
        *module_interface = switch_loadable_module_create_module_interface(pool, modname);
 
        SWITCH_ADD_API(api_interface, "strftime_tz", "strftime_tz", strftime_tz_api_function, "<Timezone name>,<format string>");
@@ -207,14 +79,6 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_timezone_load)
 SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_timezone_shutdown)
 {
 
-       if (TIMEZONES_LIST.hash) {
-               switch_core_hash_destroy(&TIMEZONES_LIST.hash);
-       }
-
-       if (TIMEZONES_LIST.pool) {
-               switch_core_destroy_memory_pool(&TIMEZONES_LIST.pool);
-       }
-
        return SWITCH_STATUS_UNLOAD;
 }
 
index f41f51197e7bb1201e7acd7db8878d190af9cd39..34bb816d45bada02806cf73579da1015618567e4 100644 (file)
@@ -24,6 +24,7 @@
  * Contributor(s):
  * 
  * Anthony Minessale II <anthmct@yahoo.com>
+ * Massimo Cetra <devel@navynet.it> - Timezone functionality
  *
  *
  * softtimer.c -- Software Timer Module
@@ -426,6 +427,146 @@ SWITCH_MODULE_RUNTIME_FUNCTION(softtimer_runtime)
        return SWITCH_STATUS_TERM;
 }
 
+/* 
+   This converts a struct tm to a switch_time_exp_t
+   We have to use UNIX structures to do our exams
+   and use switch_* functions for the output.
+*/
+
+static void tm2switchtime(struct tm * tm, switch_time_exp_t *xt ) 
+{
+
+       if (!xt || !tm) {
+           return;
+       }
+       memset( xt, 0, sizeof(xt) );
+
+       xt->tm_sec      = tm->tm_sec;
+       xt->tm_min      = tm->tm_min;
+       xt->tm_hour     = tm->tm_hour;
+       xt->tm_mday     = tm->tm_mday;
+       xt->tm_mon      = tm->tm_mon;
+       xt->tm_year     = tm->tm_year;
+       xt->tm_wday     = tm->tm_wday;
+       xt->tm_yday     = tm->tm_yday;
+       xt->tm_isdst    = tm->tm_isdst;
+#if !defined(WIN32) && !defined(__SVR4) && !defined(__sun)
+
+       xt->tm_gmtoff   = tm->tm_gmtoff;
+#endif
+
+       return;
+}
+
+/* **************************************************************************
+   LOADING OF THE XML DATA - HASH TABLE & MEMORY POOL MANAGEMENT
+   ************************************************************************** */
+
+typedef struct {
+       switch_memory_pool_t *pool;
+       switch_hash_t *hash;
+} switch_timezones_list_t;
+
+static switch_timezones_list_t TIMEZONES_LIST = { 0 };
+static switch_event_node_t *NODE = NULL;
+
+const char *switch_lookup_timezone( const char *tz_name )
+{
+       char *value = NULL;
+
+       if ( tz_name && (value = switch_core_hash_find(TIMEZONES_LIST.hash, tz_name))==NULL ) {
+           switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Timezone '%s' not found!\n", tz_name);
+       }
+       
+       return value;
+}
+
+void switch_load_timezones(switch_bool_t reload)
+{
+       switch_xml_t xml = NULL, x_lists = NULL, x_list = NULL, cfg = NULL;
+       unsigned total = 0;
+
+       if (TIMEZONES_LIST.hash) {
+               switch_core_hash_destroy(&TIMEZONES_LIST.hash);
+       }
+
+       if (TIMEZONES_LIST.pool) {
+               switch_core_destroy_memory_pool(&TIMEZONES_LIST.pool);
+       }
+
+       memset(&TIMEZONES_LIST, 0, sizeof(TIMEZONES_LIST));
+       switch_core_new_memory_pool(&TIMEZONES_LIST.pool);
+       switch_core_hash_init(&TIMEZONES_LIST.hash, TIMEZONES_LIST.pool);
+
+       if ((xml = switch_xml_open_cfg("timezones.conf", &cfg, NULL))) {
+               if ((x_lists = switch_xml_child(cfg, "timezones"))) {
+                       for (x_list = switch_xml_child(x_lists, "zone"); x_list; x_list = x_list->next) {
+                               const char *name = switch_xml_attr(x_list, "name");
+                               const char *value= switch_xml_attr(x_list, "value");
+
+                               if (switch_strlen_zero(name)) {
+                                       continue;
+                               }
+
+                               if (switch_strlen_zero(value)) {
+                                       continue;
+                               }
+
+                               switch_core_hash_insert(TIMEZONES_LIST.hash, 
+                                                                               name, 
+                                                                               switch_core_strdup(TIMEZONES_LIST.pool, value) );
+                               total++;
+                       }
+               }
+               
+               switch_xml_free(xml);
+       }
+       
+       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Timezone %sloaded %d definitions\n", reload ? "re" : "", total);
+}
+
+static void event_handler(switch_event_t *event)
+{
+       switch_load_timezones(1);
+}
+
+static void tztime(const time_t * const timep, const char *tzstring, struct tm * const tmp );
+
+SWITCH_DECLARE(switch_status_t) switch_strftime_tz(const char *tz, const char *format, char *date, size_t len)
+{
+       switch_time_t thetime;
+       time_t timep;
+
+       const char *tz_name = tz;
+       const char *tzdef;
+
+       switch_size_t retsize;
+
+       struct tm tm;
+       switch_time_exp_t stm;
+
+       thetime = switch_timestamp_now();
+
+       timep =  (thetime) / (int64_t) (1000000);
+
+       if (!switch_strlen_zero(tz_name)) {
+               tzdef = switch_lookup_timezone( tz_name );
+       } else {
+               /* We set the default timezone to GMT. */
+               tz_name="GMT";
+               tzdef="GMT";
+       }
+       
+       if (tzdef) { /* The lookup of the zone may fail. */
+               tztime( &timep, tzdef, &tm );
+               tm2switchtime( &tm, &stm );
+               switch_strftime(date, &retsize, len, switch_strlen_zero(format) ? "%Y-%m-%d %T" : format, &stm);
+               return SWITCH_STATUS_SUCCESS;
+       } else {
+               return SWITCH_STATUS_FALSE;
+       }
+}
+
 SWITCH_MODULE_LOAD_FUNCTION(softtimer_load)
 {
        switch_timer_interface_t *timer_interface;
@@ -434,6 +575,11 @@ SWITCH_MODULE_LOAD_FUNCTION(softtimer_load)
        memset(&globals, 0, sizeof(globals));
        switch_mutex_init(&globals.mutex, SWITCH_MUTEX_NESTED, module_pool);
 
+       if ((switch_event_bind_removable(modname, SWITCH_EVENT_RELOADXML, NULL, event_handler, NULL, &NODE) != SWITCH_STATUS_SUCCESS)) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't bind!\n");
+       }
+       switch_load_timezones(0);
+
        /* connect my internal structure to the blank pointer passed to me */
        *module_interface = switch_loadable_module_create_module_interface(pool, modname);
        timer_interface = switch_loadable_module_create_interface(*module_interface, SWITCH_TIMER_INTERFACE);
@@ -465,6 +611,13 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(softtimer_shutdown)
        timeEndPeriod(1);
 #endif
 
+       if (TIMEZONES_LIST.hash) {
+               switch_core_hash_destroy(&TIMEZONES_LIST.hash);
+       }
+
+       if (TIMEZONES_LIST.pool) {
+               switch_core_destroy_memory_pool(&TIMEZONES_LIST.pool);
+       }
 
        return SWITCH_STATUS_NOUNLOAD;
 }
@@ -1315,7 +1468,7 @@ static void timesub(const time_t * const timep, const long offset, register cons
            
    ************************************************************************** */
 
-SWITCH_DECLARE(void) switch_tztime(const time_t * const timep, const char *tzstring, struct tm * const tmp )
+static void tztime(const time_t * const timep, const char *tzstring, struct tm * const tmp )
 {
        struct state                    *tzptr, 
                                        *sp;