# HG changeset patch
# User rosfran
# Date 1166480337 0
# Node ID 657379d8f3b6d2125f1fabfa82468207af1ed965
# Parent  803672c406ec7c1e11d79203309061521ec6f30b
[svn r224] Fixed problem on g_time_val_* functions.

diff -r 803672c406ec -r 657379d8f3b6 gmyth/src/gmyth_livetv.c
--- a/gmyth/src/gmyth_livetv.c	Mon Dec 18 18:18:26 2006 +0000
+++ b/gmyth/src/gmyth_livetv.c	Mon Dec 18 22:18:57 2006 +0000
@@ -321,7 +321,7 @@
 		res = FALSE;
 		goto error;
 	} else {
-		gmyth_debug ("[%s] GMythLiveTV: All requests to backend to start TV were OK.\n", __FUNCTION__ );
+		gmyth_debug ("GMythLiveTV: All requests to backend to start TV were OK. [%s]\n", livetv->proginfo->pathname->str );
 	}
 	
 	livetv->setup_done = TRUE;
diff -r 803672c406ec -r 657379d8f3b6 gmyth/src/gmyth_scheduler.c
--- a/gmyth/src/gmyth_scheduler.c	Mon Dec 18 18:18:26 2006 +0000
+++ b/gmyth/src/gmyth_scheduler.c	Mon Dec 18 22:18:57 2006 +0000
@@ -291,7 +291,7 @@
              * we are not using the date field */
             /* generate a time_t from a time and a date db field */
             /* g_string_printf (date_time, "%s %s", row[4], row[3]); */
-            g_sprintf (date_time, "%s", row[3]); 
+            g_sprintf (date_time, "%sT%s", row[4], row[3]); 
             
             record->start_time = gmyth_util_string_to_time_val (date_time);
 
@@ -299,7 +299,7 @@
              * we are not using the date field */
             /* generate a time_t from a time and a date db field */
             /* g_string_printf (date_time, "%s %s", row[6], row[5]); */
-            g_sprintf (date_time, "%s", row[5]); 
+            g_sprintf (date_time, "%sT%s", row[6], row[5]); 
 
             record->end_time = gmyth_util_string_to_time_val (date_time);
 	
@@ -374,10 +374,12 @@
         gmyth_util_time_to_string_only_date( schedule_info->end_time ), 
         schedule_info->title->str, //title
     	schedule_info->channel_id,//station
-    	g_date_get_weekday( gmyth_util_time_val_to_date( schedule_info->start_time ) ), //findday
+    	(gmyth_util_time_val_to_date( schedule_info->start_time ))->tm_wday, //findday
    		gmyth_util_time_to_string_only_time( schedule_info->start_time ), //findtime
    		(gint)(schedule_info->start_time->tv_sec/60/60/24 + 719528)//findid
     );
+    
+    gmyth_debug ( "Sending query to MySQL = %s.", query_str->str );
 
     msql_res = gmyth_query_process_statement (scheduler->msqlquery, query_str->str);
     
diff -r 803672c406ec -r 657379d8f3b6 gmyth/src/gmyth_tvchain.c
--- a/gmyth/src/gmyth_tvchain.c	Mon Dec 18 18:18:26 2006 +0000
+++ b/gmyth/src/gmyth_tvchain.c	Mon Dec 18 22:18:57 2006 +0000
@@ -132,9 +132,10 @@
     if (tvchain->tvchain_id == NULL) {
 	    gchar *isodate = NULL;
     	GTimeVal *cur_time = g_new0( GTimeVal, 1 );
+    	//struct tm* gmyth_util_time_val_to_date ( const GTimeVal* time )
 
 	    g_get_current_time(cur_time);
-    	isodate = gmyth_util_time_to_isoformat_from_time_val (cur_time);
+    	isodate = gmyth_util_time_to_isoformat_from_time_val_fmt ( "%Y-%m-%dT%H:%M:%S", cur_time );
 
 	    tvchain->tvchain_id = g_string_sized_new (7 + strlen (hostname) + strlen(isodate));
     	g_string_printf(tvchain->tvchain_id, 
@@ -215,9 +216,9 @@
 		while ((msql_row = mysql_fetch_row (msql_res)) != NULL) {
 			struct LiveTVChainEntry *entry = g_new0 (struct LiveTVChainEntry, 1);
 			entry->chanid = g_string_new (msql_row[0]);
-			entry->starttime = gmyth_util_string_to_time_val ((gchar*)msql_row[1]);
-			entry->endtime = gmyth_util_string_to_time_val (msql_row[2]);
-			entry->discontinuity = atoi (msql_row[3]) != 0;
+			entry->starttime = gmyth_util_string_to_time_val ( g_strdup( msql_row[1] ) );
+			entry->endtime = gmyth_util_string_to_time_val ( g_strdup( msql_row[2] ) );
+			entry->discontinuity = g_ascii_strtoull( msql_row[3], NULL, 10 ) != 0;
 			entry->hostprefix = g_string_new (msql_row[5]);
 			entry->cardtype = g_string_new (msql_row[6]);
 			entry->channum = g_string_new (msql_row[7]);
@@ -258,7 +259,7 @@
 
 done:
 	if ( stmt_str != NULL )
-		g_string_free (stmt_str, TRUE);
+		g_string_free (stmt_str, FALSE);
 
 	if ( msql_res != NULL )
 		mysql_free_result (msql_res);
@@ -366,7 +367,7 @@
 
 	g_return_val_if_fail( tvchain != NULL, NULL );
 
-	if (!entry || !tvchain) {
+	if ( !entry || !tvchain ) {
 		g_warning ("gmyth_tvchain_entry_to_program() received NULL argument");
 		return NULL;
 	}
@@ -381,7 +382,7 @@
 	if (proginfo) {
 		proginfo->pathname = g_string_prepend (proginfo->pathname, entry->hostprefix->str);
 	} else {
-		g_warning ("tvchain_entry_to_program( chan id = %s, starttime = %ld) failed!", entry->chanid->str, entry->starttime);
+		g_warning ("tvchain_entry_to_program( chan id = %s, starttime = %lld) failed!", entry->chanid->str, entry->starttime);
 	}
 
 	return proginfo;
diff -r 803672c406ec -r 657379d8f3b6 gmyth/src/gmyth_util.c
--- a/gmyth/src/gmyth_util.c	Mon Dec 18 18:18:26 2006 +0000
+++ b/gmyth/src/gmyth_util.c	Mon Dec 18 22:18:57 2006 +0000
@@ -31,11 +31,13 @@
 
 #define _XOPEN_SOURCE
 #define _XOPEN_SOURCE_EXTENDED
+#define __USE_MISC
 
 #include <glib.h>
 #include <glib/gprintf.h>
 #include <time.h>
 #include <sys/time.h>
+#include <sys/timex.h>
 
 #include "gmyth.h"
 
@@ -82,7 +84,7 @@
 		tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec);
 		
 	gmyth_debug( "Result (ISO 8601) = %s", result->str );
-		
+
 	g_static_mutex_unlock ( &mutex );
 	
 	return result;
@@ -99,8 +101,8 @@
  * @param time_value the GTimeValue to be converted
  * @return GString* the converted isoformat string 
  */
-static gchar*
-gmyth_util_time_to_isoformat_from_time_val_fmt ( const gchar *fmt_string, GTimeVal* time_val )
+gchar*
+gmyth_util_time_to_isoformat_from_time_val_fmt ( const gchar *fmt_string, const GTimeVal* time_val )
 {
 	
 	gchar *result = NULL;
@@ -108,23 +110,24 @@
 	
 	gint buffer_len = 0;
 	
-	g_return_val_if_fail( fmt_string != NULL, NULL );	
+	g_return_val_if_fail( fmt_string != NULL, NULL );
+		
 	g_return_val_if_fail( time_val != NULL, NULL );
  
-	time_t time = time_val->tv_sec + (gint)(time_val->tv_usec / G_USEC_PER_SEC);
+	time_t time = time_val->tv_sec;// + (gint)( time_val->tv_usec / G_USEC_PER_SEC );
 
 	tm_time = g_malloc0( sizeof(struct tm) );
 
 	g_static_mutex_lock ( &mutex );
 	
-	if ( NULL == gmtime_r( &time, tm_time ) ) {
+	if ( NULL == localtime_r( &time, tm_time ) ) {
 		g_warning ("gmyth_util_time_to_isoformat convertion error!\n");
 	}	else {
 		// we first check the return of strftime to allocate a buffer of the correct size
 	  buffer_len = strftime( NULL, SSIZE_MAX, fmt_string, tm_time );
-	  if( buffer_len > 0 ){
+	  if ( buffer_len > 0 ) {
 	    result = g_malloc0( buffer_len + 1 );
-	    if( result == NULL ){     
+	    if( result == NULL ){
 				g_static_mutex_unlock ( &mutex );
 				g_warning ("gmyth_util_time_to_isoformat convertion error!\n");
 				return NULL;
@@ -135,15 +138,7 @@
 		
 	}
 	
-	/* 
-	if ( NULL == asctime_r( tm_time, result ) ) {
-		g_static_mutex_unlock ( &mutex );
-		g_warning ("gmyth_util_time_to_isoformat convertion error!\n");
-    return NULL;
-	}
-	*/
-	
-	gmyth_debug( "Result (after asctime_r) = %s", result );
+	gmyth_debug( "Result (strftime) = %s", result );
 	
 	//strptime( result, "%Y-%m-%dT%H:%M:%SZ", tm_time ); 
 	
@@ -167,9 +162,9 @@
  * @return GString* the converted isoformat string 
  */
 gchar*
-gmyth_util_time_to_isoformat_from_time_val ( GTimeVal* time )
+gmyth_util_time_to_isoformat_from_time_val ( const GTimeVal* time )
 {
-	gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%Y-%m-%dT%T", time );
+	gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%Y-%m-%d %H:%M:%S", time );
 	//result[10] = ' ';
 	//result[ strlen(result) - 1] = '\0';
 	
@@ -186,11 +181,11 @@
  * @return GString* the converted isoformat string 
  */
 gchar*
-gmyth_util_time_to_string_only_date ( GTimeVal* time )
+gmyth_util_time_to_string_only_date ( const GTimeVal* time )
 {
-	gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%y-%m-%d", time );
-	result[10] = ' ';
-	result[ strlen(result) - 1] = '\0';
+	gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%Y-%m-%d", time );
+	//result[10] = ' ';
+	//result[ strlen(result) - 1] = '\0';
 	return result;
 }
 
@@ -204,11 +199,11 @@
  * @return GString* the converted isoformat string 
  */
 gchar*
-gmyth_util_time_to_string_only_time ( GTimeVal* time )
+gmyth_util_time_to_string_only_time ( const GTimeVal* time )
 {
-	gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%T", time );
-	result[10] = ' ';
-	result[ strlen(result) - 1] = '\0';
+	gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%H:%M:%S", time );	 
+	//result[10] = ' ';
+	//result[ strlen(result) - 1] = '\0';
 	return result;
 }
 
@@ -226,6 +221,7 @@
 {
 	GString *result = gmyth_util_time_to_isoformat (time_value);
 	result->str[10] = ' ';	
+	result->str[ strlen(result->str) - 1] = '\0';
 
 	return result;
 }
@@ -240,10 +236,10 @@
  * @return GString* the converted string 
  */
 gchar*
-gmyth_util_time_to_string_from_time_val (GTimeVal *time_val)
+gmyth_util_time_to_string_from_time_val ( const GTimeVal *time_val )
 {
-	gchar *result = gmyth_util_time_to_isoformat_from_time_val (time_val);
-	result[10] = ' ';
+	gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt ( "%Y-%m-%d %H:%M:%S", time_val );
+	//result[10] = ' ';
 
 	return result;
 }
@@ -257,46 +253,26 @@
 time_t
 gmyth_util_string_to_time (GString* time_str)
 {
-	//gint year, month, day, hour, min, sec;
-	GDate *date = g_date_new();
+	gint year, month, day, hour, min, sec;
     
 	gmyth_debug( "[%s] time_str = %s. [%s]\n", __FUNCTION__, time_str != NULL ? 
-					time_str->str : "[time string is NULL!]", g_strdelimit( time_str->str, " ", 'T' ) );
+					time_str->str : "[time string is NULL!]", time_str->str );
 
-
-	//if (sscanf (time_str->str, "%04d-%02d-%02d %02d:%02d:%02d",
-	//		&year, &month, &day, &hour, &min, &sec) < 3) {				
-	//g_date_set_parse( date, time_str->str );
-  if ( NULL == date ) { 
+	if ( sscanf (time_str->str, "%04d-%02d-%02d %02d:%02d:%02d",
+			&year, &month, &day, &hour, &min, &sec) < 3 ) {
 		g_warning ("GMythUtil: isoformat_to_time converter error!\n");
 		return 0;
 	}
 	
 	g_static_mutex_lock ( &mutex );
-	//GDate *date = g_date_new_dmy( day, month - 1, year - 1900 );
 	
 	struct tm* tm_time = g_malloc0( sizeof(struct tm) );
-/*		
-		tm_time->tm_year = year - 1900;
-		tm_time->tm_mon = month - 1;
-		tm_time->tm_mday = day;
-		tm_time->tm_hour = hour;
-		tm_time->tm_min = min;
-		tm_time->tm_sec = sec;
-	*/
-	
-	GTimeVal *time = g_new0( GTimeVal, 1 ); 
-	
-	if ( !g_time_val_from_iso8601( g_strdelimit( time_str->str, " ", 'T' ), time ) )
-	{
-		gmyth_debug( "ERROR! Problems converting to GTimeVal == %s\n", g_time_val_to_iso8601( time ) );
-	} 
-	
-	gmyth_debug( "Converted to GTimeVal == %s\n", g_time_val_to_iso8601( time ) );
-	
-	g_date_set_time_val( date, time );
-	
-	g_date_to_struct_tm( date, tm_time ); 
+	tm_time->tm_year = year - 1900;
+	tm_time->tm_mon = month - 1;
+	tm_time->tm_mday = day;
+	tm_time->tm_hour = hour;
+	tm_time->tm_min = min;
+	tm_time->tm_sec = sec;
 	
 	g_static_mutex_unlock ( &mutex );
 	
@@ -309,19 +285,23 @@
  * @param time_str the string to be converted
  * @return time_t the time converted value
  */
-GDate *
-gmyth_util_time_val_to_date (GTimeVal* time)
+struct tm*
+gmyth_util_time_val_to_date ( const GTimeVal* time )
 {
-	GDate *date = g_date_new();
+	struct tm *date = g_malloc0( sizeof( struct tm ) );
+	time_t time_micros = time->tv_sec;// + (gint)( time->tv_usec / G_USEC_PER_SEC );
     
   if ( NULL == date ) { 
-		g_warning ("GMythUtil: GDate *gmyth_util_time_val_to_date (GTimeVal* time) - converter error!\n");
-		return 0;
+		g_warning ( "GMythUtil: GDate *gmyth_util_time_val_to_date (GTimeVal* time) - converter error!\n" );
+		return NULL;
 	}
 	
-	g_date_set_time_val( date, time );
-	
-	gmyth_debug( "Converted from GTimeVal == %s to GDate\n", g_time_val_to_iso8601( time ) );
+	if ( NULL == localtime_r( &time_micros, date ) ) {
+		g_warning ( "gmyth_util_time_to_isoformat convertion error!\n" );
+		return NULL;
+	}
+
+	gmyth_debug( "Converted from GTimeVal == %s to GDate\n", asctime( date ) );
 	
 	return date;
 }
@@ -333,28 +313,73 @@
  * @return time_t the time converted value
  */
 GTimeVal*
-gmyth_util_string_to_time_val (gchar* time_str)
+gmyth_util_string_to_time_val_fmt ( const gchar *fmt_string, const gchar* time_str )
 {
-	GTimeVal *time = g_new0( GTimeVal, 1 ); 
+	GTimeVal *time = g_new0( GTimeVal, 1 );
+	struct tm* tm_time = NULL;
+	time_t time_micros;
+	gchar *result = NULL;
     
-	gmyth_debug( "[%s] time_str = %s. [%s]\n", __FUNCTION__, time_str != NULL ? 
-					time_str : "[time string is NULL!]", g_strdelimit( time_str, " ", 'T' ) );
+	gmyth_debug( "[%s] time_str = %s. [%s]\n", time_str, time_str != NULL ? 
+					time_str : "[time string is NULL!]", time_str );
 
-  if ( NULL == time ) { 
+  if ( NULL == time_str ) { 
 		g_warning ("GMythUtil: isoformat_to_time converter error!\n");
-		return NULL;
+		time = NULL;
+		goto done;
 	}
 	
 	g_static_mutex_lock ( &mutex );
 	
-	if ( !g_time_val_from_iso8601( g_strdelimit( time_str, " ", 'T' ), time ) )
-	{
-		gmyth_debug( "ERROR! Problems converting to GTimeVal == %s\n", g_strdelimit( time_str, " ", 'T' ) );
-	} 
+	tm_time = g_malloc0( sizeof(struct tm) );
+
+	/* we first check the return of strftime to allocate a buffer of the correct size */
+  result = strptime( time_str, "%Y-%m-%dT%H:%M:%S", tm_time );
+  if ( NULL == result ) {
+		/* we first check the return of strftime to allocate a buffer of the correct size */
+	  result = strptime( time_str, "%Y-%m-%dT%H:%M:%SZ", tm_time );
+	  if ( NULL == result ) {
+	  	/* we first check the return of strftime to allocate a buffer of the correct size */
+		  result = strptime( time_str, "%Y-%m-%d %H:%M:%S", tm_time );
+		  if ( NULL == result ) {
+		  	g_static_mutex_unlock ( &mutex );
+		    gmyth_debug( "Dateline (ISO result): %s\n", result );
+		    time = NULL;
+		    goto done;	    
+		  }
+	  }
+  }
+  
+  time_micros = mktime( tm_time );
+		
+	time->tv_sec = time_micros; // + (gint)( time_val->tv_usec / G_USEC_PER_SEC );
 	
-	gmyth_debug( "Converted to GTimeVal == %s\n", g_time_val_to_iso8601( time ) );
+	//strftime( result, SSIZE_MAX, "%Y-%m-%dT%H:%M:%S", tm_time );
 	
+	gmyth_debug( "After mktime call... = %s", asctime(tm_time) );
+
 	g_static_mutex_unlock ( &mutex );
+
+done:	
+	//if ( tm_time )
+	//	g_free( tm_time );
+		
+	//if ( result )
+	//	g_free( result );		
+	
+	return time;
+}
+
+/** Converts a GString in the following format 
+ * (e.g. 2006-07-20 09:56:41) to a time_t struct.
+ * 
+ * @param time_str the string to be converted
+ * @return time_t the time converted value
+ */
+GTimeVal*
+gmyth_util_string_to_time_val ( const gchar* time_str )
+{
+	GTimeVal *time = gmyth_util_string_to_time_val_fmt ( "%Y-%m-%d %H:%M:%S", time_str );	
 	
 	return time;
 }
diff -r 803672c406ec -r 657379d8f3b6 gmyth/src/gmyth_util.h
--- a/gmyth/src/gmyth_util.h	Mon Dec 18 18:18:26 2006 +0000
+++ b/gmyth/src/gmyth_util.h	Mon Dec 18 22:18:57 2006 +0000
@@ -41,18 +41,22 @@
 time_t      gmyth_util_string_to_time   (GString* time_str);
 gint64     	gmyth_util_decode_long_long (GMythStringList *strlist, 
                                          guint offset);
+
+gchar* 			gmyth_util_time_to_isoformat_from_time_val_fmt ( const gchar *fmt_string, const GTimeVal* time_val );
+
+GTimeVal		*gmyth_util_string_to_time_val_fmt ( const gchar *fmt_string, const gchar* time_str );
                                          
-GTimeVal		*gmyth_util_string_to_time_val (gchar* time_str);
+GTimeVal		*gmyth_util_string_to_time_val ( const gchar* time_str );
 
-gchar 			*gmyth_util_time_to_isoformat_from_time_val(GTimeVal *time);
+gchar 			*gmyth_util_time_to_isoformat_from_time_val( const GTimeVal *time);
 
-gchar				*gmyth_util_time_to_string_only_date ( GTimeVal* time );
+gchar				*gmyth_util_time_to_string_only_date ( const GTimeVal* time );
 
-gchar				*gmyth_util_time_to_string_only_time ( GTimeVal* time );
+gchar				*gmyth_util_time_to_string_only_time ( const GTimeVal* time );
 
-gchar				*gmyth_util_time_to_string_from_time_val (GTimeVal *time_val);
+gchar				*gmyth_util_time_to_string_from_time_val ( const GTimeVal *time_val );
 
-GDate 			*gmyth_util_time_val_to_date (GTimeVal* time);
+struct tm		*gmyth_util_time_val_to_date ( const GTimeVal* time );
 
 gboolean 		gmyth_util_file_exists (GMythBackendInfo *backend_info, const gchar* filename);