[Freeswitch-svn] [commit] r9738 - in freeswitch/trunk/src: . include mod/applications/mod_timezone

Freeswitch SVN mikej at freeswitch.org
Mon Sep 29 12:58:32 EDT 2008


Author: mikej
Date: Mon Sep 29 12:58:31 2008
New Revision: 9738

Modified:
   freeswitch/trunk/src/include/switch_core.h
   freeswitch/trunk/src/mod/applications/mod_timezone/mod_timezone.c
   freeswitch/trunk/src/switch_time.c

Log:
move mod_timezone functions into core (part 2/3)

Modified: freeswitch/trunk/src/include/switch_core.h
==============================================================================
--- freeswitch/trunk/src/include/switch_core.h	(original)
+++ freeswitch/trunk/src/include/switch_core.h	Mon Sep 29 12:58:31 2008
@@ -1645,7 +1645,7 @@
 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)

Modified: freeswitch/trunk/src/mod/applications/mod_timezone/mod_timezone.c
==============================================================================
--- freeswitch/trunk/src/mod/applications/mod_timezone/mod_timezone.c	(original)
+++ freeswitch/trunk/src/mod/applications/mod_timezone/mod_timezone.c	Mon Sep 29 12:58:31 2008
@@ -27,103 +27,6 @@
 
 #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 @@
 
 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 @@
 	return SWITCH_STATUS_SUCCESS;
 }
 
+
 /* **************************************************************************
 
 ************************************************************************** */
@@ -183,20 +65,10 @@
 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_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;
 }
 

Modified: freeswitch/trunk/src/switch_time.c
==============================================================================
--- freeswitch/trunk/src/switch_time.c	(original)
+++ freeswitch/trunk/src/switch_time.c	Mon Sep 29 12:58:31 2008
@@ -24,6 +24,7 @@
  * Contributor(s):
  * 
  * Anthony Minessale II <anthmct at yahoo.com>
+ * Massimo Cetra <devel at navynet.it> - Timezone functionality
  *
  *
  * softtimer.c -- Software Timer Module
@@ -426,6 +427,146 @@
 	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 @@
 	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 @@
 	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 @@
 	    
    ************************************************************************** */
 
-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;



More information about the Freeswitch-svn mailing list