branches/gmyth-0.1b/src/gmyth_util.c
branchtrunk
changeset 345 8c26cc7dc51b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/branches/gmyth-0.1b/src/gmyth_util.c	Fri Feb 09 20:42:11 2007 +0000
     1.3 @@ -0,0 +1,647 @@
     1.4 +/**
     1.5 +* GMyth Library
     1.6 +*
     1.7 +* @file gmyth/gmyth_util.c
     1.8 +* 
     1.9 +* @brief <p> This component provides utility functions.
    1.10 +* 
    1.11 +* Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
    1.12 +* @author Hallyson Luiz de Morais Melo <hallyson.melo@indt.org.br>
    1.13 +*
    1.14 +*//*
    1.15 +* 
    1.16 +* This program is free software; you can redistribute it and/or modify
    1.17 +* it under the terms of the GNU Lesser General Public License as published by
    1.18 +* the Free Software Foundation; either version 2 of the License, or
    1.19 +* (at your option) any later version.
    1.20 +*
    1.21 +* This program is distributed in the hope that it will be useful,
    1.22 +* but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.23 +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.24 +* GNU General Public License for more details.
    1.25 +*
    1.26 +* You should have received a copy of the GNU Lesser General Public License
    1.27 +* along with this program; if not, write to the Free Software
    1.28 +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.29 +*/
    1.30 + 
    1.31 +#ifdef HAVE_CONFIG_H
    1.32 +#include "config.h"
    1.33 +#endif
    1.34 +
    1.35 +#define _XOPEN_SOURCE
    1.36 +#define _XOPEN_SOURCE_EXTENDED
    1.37 +#define __USE_MISC
    1.38 +
    1.39 +#include <glib.h>
    1.40 +#include <glib/gprintf.h>
    1.41 +#include <time.h>
    1.42 +#include <sys/time.h>
    1.43 +#include <sys/timex.h>
    1.44 +
    1.45 +#include "gmyth.h"
    1.46 +
    1.47 +#if !GLIB_CHECK_VERSION (2, 10, 0)
    1.48 +gchar *
    1.49 +g_time_val_to_iso8601 (GTimeVal *time_);
    1.50 +gboolean
    1.51 +g_time_val_from_iso8601 (const gchar *iso_date,
    1.52 +                         GTimeVal    *time_);
    1.53 +void
    1.54 +g_date_set_time_val (GDate    *date,
    1.55 +             GTimeVal *timeval);
    1.56 +
    1.57 +#endif
    1.58 +
    1.59 +static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
    1.60 +
    1.61 +/** Converts a time_t struct in a GString at ISO standard format 
    1.62 + * (e.g. 2006-07-20T09:56:41).
    1.63 + * 
    1.64 + * The returned GString memory should be deallocated from 
    1.65 + * the calling function.
    1.66 + *
    1.67 + * @param time_value the time value to be converted
    1.68 + * @return GString* the converted isoformat string 
    1.69 + */
    1.70 +GString*
    1.71 +gmyth_util_time_to_isoformat (time_t time_value)
    1.72 +{
    1.73 +	struct tm tm_time;
    1.74 +	GString *result;
    1.75 +	
    1.76 +	g_static_mutex_lock ( &mutex );
    1.77 +	
    1.78 +	if (localtime_r(&time_value, &tm_time) == NULL) {
    1.79 +	    g_static_mutex_unlock ( &mutex );
    1.80 +	    g_warning ("gmyth_util_time_to_isoformat convertion error!\n");
    1.81 +            return NULL;
    1.82 +	}
    1.83 +	
    1.84 +	result = g_string_sized_new(20);
    1.85 +	g_string_printf(result, "%04d-%02d-%02dT%02d:%02d:%02d",
    1.86 +		tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
    1.87 +		tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec);
    1.88 +		
    1.89 +	gmyth_debug( "Result (ISO 8601) = %s", result->str );
    1.90 +
    1.91 +	g_static_mutex_unlock ( &mutex );
    1.92 +	
    1.93 +	return result;
    1.94 +}
    1.95 +
    1.96 +/** Converts a time_t struct in a GString at ISO standard format 
    1.97 + * (e.g. 2006-07-20T09:56:41).
    1.98 + * 
    1.99 + * The returned GString memory should be deallocated from 
   1.100 + * the calling function.
   1.101 + *
   1.102 + * @param time_value the GTimeValue to be converted
   1.103 + * @return GString* the converted isoformat string 
   1.104 + */
   1.105 +gchar*
   1.106 +gmyth_util_time_to_isoformat_from_time_val_fmt ( const gchar *fmt_string, const GTimeVal* time_val )
   1.107 +{
   1.108 +	gchar *result = NULL;
   1.109 +	struct tm *tm_time = NULL;
   1.110 +	
   1.111 +	gint buffer_len = 0;
   1.112 +	
   1.113 +	g_return_val_if_fail( fmt_string != NULL, NULL );
   1.114 +		
   1.115 +	g_return_val_if_fail( time_val != NULL, NULL );
   1.116 + 
   1.117 +	time_t time = time_val->tv_sec;// + (gint)( time_val->tv_usec / G_USEC_PER_SEC );
   1.118 +
   1.119 +	tm_time = g_malloc0( sizeof(struct tm) );
   1.120 +
   1.121 +	g_static_mutex_lock ( &mutex );
   1.122 +	
   1.123 +	if ( NULL == localtime_r( &time, tm_time ) ) {
   1.124 +		g_warning ("gmyth_util_time_to_isoformat convertion error!\n");
   1.125 +	}	else {
   1.126 +		// we first check the return of strftime to allocate a buffer of the correct size
   1.127 +	  buffer_len = strftime( NULL, SSIZE_MAX, fmt_string, tm_time );
   1.128 +	  if ( buffer_len > 0 ) {
   1.129 +	    result = g_malloc0( buffer_len + 1 );
   1.130 +	    if( result == NULL ){
   1.131 +				g_static_mutex_unlock ( &mutex );
   1.132 +				g_warning ("gmyth_util_time_to_isoformat convertion error!\n");
   1.133 +				return NULL;
   1.134 +	    }
   1.135 +	    strftime( result, buffer_len + 1, fmt_string, tm_time );
   1.136 +	    gmyth_debug( "Dateline (ISO result): %s", result );
   1.137 +	  }
   1.138 +		
   1.139 +	}
   1.140 +	
   1.141 +	gmyth_debug( "Result (strftime) = %s", result );
   1.142 +	
   1.143 +	//strptime( result, "%Y-%m-%dT%H:%M:%SZ", tm_time ); 
   1.144 +	
   1.145 +	//strftime( result, strlen(result), fmt_string, tm_time );
   1.146 +	
   1.147 +	g_static_mutex_unlock ( &mutex );
   1.148 +	
   1.149 +	gmyth_debug( "Result (ISO 8601) = %s", result  );
   1.150 +	
   1.151 +	return result;	
   1.152 +		
   1.153 +}
   1.154 +
   1.155 +/** Converts a time_t struct in a GString at ISO standard format 
   1.156 + * (e.g. 2006-07-20 09:56:41).
   1.157 + * 
   1.158 + * The returned GString memory should be deallocated from 
   1.159 + * the calling function.
   1.160 + *
   1.161 + * @param time_value the GTimeValue to be converted
   1.162 + * @return GString* the converted isoformat string 
   1.163 + */
   1.164 +gchar*
   1.165 +gmyth_util_time_to_isoformat_from_time_val ( const GTimeVal* time )
   1.166 +{
   1.167 +	gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%Y-%m-%d %H:%M:%S", time );
   1.168 +	//result[10] = ' ';
   1.169 +	//result[ strlen(result) - 1] = '\0';
   1.170 +	
   1.171 +	return result;	
   1.172 +}
   1.173 +
   1.174 +/** Converts a time_t struct in a GString at ISO standard format 2 
   1.175 + * (e.g. 2006-07-20T09:56:41).
   1.176 + * 
   1.177 + * The returned GString memory should be deallocated from 
   1.178 + * the calling function.
   1.179 + *
   1.180 + * @param time_value the GTimeValue to be converted
   1.181 + * @return GString* the converted isoformat string 
   1.182 + */
   1.183 +gchar*
   1.184 +gmyth_util_time_to_mythformat_from_time_val ( const GTimeVal* time )
   1.185 +{
   1.186 +	gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%Y-%m-%dT%H:%M:%S", time );
   1.187 +	return result;	
   1.188 +}
   1.189 +
   1.190 +/** Converts a time_t struct in a GString at ISO standard format 
   1.191 + * (e.g. 2006-07-20T09:56:41).
   1.192 + * 
   1.193 + * The returned GString memory should be deallocated from 
   1.194 + * the calling function.
   1.195 + *
   1.196 + * @param time_value the GTimeValue to be converted
   1.197 + * @return GString* the converted isoformat string 
   1.198 + */
   1.199 +gchar*
   1.200 +gmyth_util_time_to_string_only_date ( const GTimeVal* time )
   1.201 +{
   1.202 +	gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%Y-%m-%d", time );
   1.203 +	//result[10] = ' ';
   1.204 +	//result[ strlen(result) - 1] = '\0';
   1.205 +	return result;
   1.206 +}
   1.207 +
   1.208 +/** Converts a time_t struct in a GString at ISO standard format 
   1.209 + * (e.g. 2006-07-20T09:56:41).
   1.210 + * 
   1.211 + * The returned GString memory should be deallocated from 
   1.212 + * the calling function.
   1.213 + *
   1.214 + * @param time_value the GTimeValue to be converted
   1.215 + * @return GString* the converted isoformat string 
   1.216 + */
   1.217 +gchar*
   1.218 +gmyth_util_time_to_string_only_time ( const GTimeVal* time )
   1.219 +{
   1.220 +	gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%H:%M:%S", time );	 
   1.221 +	//result[10] = ' ';
   1.222 +	//result[ strlen(result) - 1] = '\0';
   1.223 +	return result;
   1.224 +}
   1.225 +
   1.226 +/** Converts a time_t struct in a GString to the following 
   1.227 + * format (e.g. 2006-07-20 09:56:41).
   1.228 + * 
   1.229 + * The returned GString memory should be deallocated from 
   1.230 + * the calling function.
   1.231 + *
   1.232 + * @param time_value the time value to be converted
   1.233 + * @return GString* the converted string 
   1.234 + */
   1.235 +GString*
   1.236 +gmyth_util_time_to_string (time_t time_value)
   1.237 +{
   1.238 +	GString *result = gmyth_util_time_to_isoformat (time_value);
   1.239 +	result->str[10] = ' ';	
   1.240 +	result->str[ strlen(result->str) - 1] = '\0';
   1.241 +
   1.242 +	return result;
   1.243 +}
   1.244 +
   1.245 +/** Converts a time_t struct in a GString to the following 
   1.246 + * format (e.g. 2006-07-20 09:56:41).
   1.247 + * 
   1.248 + * The returned GString memory should be deallocated from 
   1.249 + * the calling function.
   1.250 + *
   1.251 + * @param time_value the time value to be converted
   1.252 + * @return GString* the converted string 
   1.253 + */
   1.254 +gchar*
   1.255 +gmyth_util_time_to_string_from_time_val ( const GTimeVal *time_val )
   1.256 +{
   1.257 +	gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt ( "%Y-%m-%d %H:%M:%S", time_val );
   1.258 +	//result[10] = ' ';
   1.259 +
   1.260 +	return result;
   1.261 +}
   1.262 +
   1.263 +/** Converts a GString in the following format 
   1.264 + * (e.g. 2006-07-20 09:56:41) to a time_t struct.
   1.265 + * 
   1.266 + * @param time_str the string to be converted
   1.267 + * @return time_t the time converted value
   1.268 + */
   1.269 +time_t
   1.270 +gmyth_util_string_to_time (GString* time_str)
   1.271 +{
   1.272 +	gint year, month, day, hour, min, sec;
   1.273 +    
   1.274 +	gmyth_debug( "[%s] time_str = %s. [%s]", __FUNCTION__, time_str != NULL ? 
   1.275 +					time_str->str : "[time string is NULL!]", time_str->str );
   1.276 +
   1.277 +	if ( sscanf (time_str->str, "%04d-%02d-%02d %02d:%02d:%02d",
   1.278 +			&year, &month, &day, &hour, &min, &sec) < 3 ) {
   1.279 +		g_warning ("GMythUtil: isoformat_to_time converter error!\n");
   1.280 +		return 0;
   1.281 +	}
   1.282 +	
   1.283 +	g_static_mutex_lock ( &mutex );
   1.284 +	
   1.285 +	struct tm* tm_time = g_malloc0( sizeof(struct tm) );
   1.286 +	tm_time->tm_year = year - 1900;
   1.287 +	tm_time->tm_mon = month - 1;
   1.288 +	tm_time->tm_mday = day;
   1.289 +	tm_time->tm_hour = hour;
   1.290 +	tm_time->tm_min = min;
   1.291 +	tm_time->tm_sec = sec;
   1.292 +	
   1.293 +	g_static_mutex_unlock ( &mutex );
   1.294 +	
   1.295 +	return mktime( tm_time );
   1.296 +}
   1.297 +
   1.298 +/** Converts a GString in the following format 
   1.299 + * (e.g. 2006-07-20 09:56:41) to a time_t struct.
   1.300 + * 
   1.301 + * @param time_str the string to be converted
   1.302 + * @return time_t the time converted value
   1.303 + */
   1.304 +struct tm*
   1.305 +gmyth_util_time_val_to_date ( const GTimeVal* time )
   1.306 +{
   1.307 +	struct tm *date = g_malloc0( sizeof( struct tm ) );
   1.308 +	time_t time_micros = time->tv_sec;// + (gint)( time->tv_usec / G_USEC_PER_SEC );
   1.309 +    
   1.310 +  if ( NULL == date ) { 
   1.311 +		g_warning ( "GMythUtil: GDate *gmyth_util_time_val_to_date (GTimeVal* time) - converter error!\n" );
   1.312 +		return NULL;
   1.313 +	}
   1.314 +	
   1.315 +	if ( NULL == localtime_r( &time_micros, date ) ) {
   1.316 +		g_warning ( "gmyth_util_time_to_isoformat convertion error!\n" );
   1.317 +		return NULL;
   1.318 +	}
   1.319 +
   1.320 +	gmyth_debug( "Converted from GTimeVal == %s to GDate", asctime( date ) );
   1.321 +	
   1.322 +	return date;
   1.323 +}
   1.324 +
   1.325 +/** Converts a GString in the following format 
   1.326 + * (e.g. 2006-07-20 09:56:41) to a time_t struct.
   1.327 + * 
   1.328 + * @param time_str the string to be converted
   1.329 + * @return time_t the time converted value
   1.330 + */
   1.331 +GTimeVal*
   1.332 +gmyth_util_string_to_time_val_fmt ( const gchar *fmt_string, const gchar* time_str )
   1.333 +{
   1.334 +	GTimeVal *time = g_new0( GTimeVal, 1 );
   1.335 +	struct tm* tm_time = NULL;
   1.336 +	time_t time_micros;
   1.337 +	gint result;
   1.338 +
   1.339 +	gmyth_debug( "[%s] time_str = %s. [%s]", time_str, time_str != NULL ? 
   1.340 +					time_str : "[time string is NULL!]", time_str );
   1.341 +
   1.342 +	if ( NULL == time_str ) 
   1.343 +	{ 
   1.344 +		g_warning ("GMythUtil: isoformat_to_time converter error!\n");
   1.345 +		return NULL;
   1.346 +	}
   1.347 +	
   1.348 +	g_static_mutex_lock ( &mutex );
   1.349 +	
   1.350 +	tm_time = g_malloc0( sizeof(struct tm) );
   1.351 +
   1.352 +	/* we first check the return of strftime to allocate a buffer of the correct size */
   1.353 +  result = strptime( time_str, "%Y-%m-%dT%H:%M:%S", tm_time );
   1.354 +  if ( NULL == result ) {
   1.355 +		/* we first check the return of strftime to allocate a buffer of the correct size */
   1.356 +	  result = strptime( time_str, "%Y-%m-%dT%H:%M:%SZ", tm_time );
   1.357 +	  if ( NULL == result ) {
   1.358 +	  	/* we first check the return of strftime to allocate a buffer of the correct size */
   1.359 +		  result = strptime( time_str, "%Y-%m-%d %H:%M:%S", tm_time );
   1.360 +		  if ( NULL == result ) {
   1.361 +		  	g_static_mutex_unlock ( &mutex );
   1.362 +		    gmyth_debug( "Dateline (ISO result): %s", result );
   1.363 +		    time = NULL;
   1.364 +		    //goto done;	    
   1.365 +		  }
   1.366 +	  }
   1.367 +  }
   1.368 +  
   1.369 +  time_micros = mktime( tm_time );
   1.370 +		
   1.371 +	time->tv_sec = time_micros; // + (gint)( time_val->tv_usec / G_USEC_PER_SEC );
   1.372 +	
   1.373 +	gmyth_debug( "After mktime call... = %s", asctime(tm_time) );
   1.374 +
   1.375 +	g_static_mutex_unlock ( &mutex );
   1.376 +
   1.377 +	return time;
   1.378 +}
   1.379 +
   1.380 +/** Converts a GString in the following format 
   1.381 + * (e.g. 2006-07-20 09:56:41) to a time_t struct.
   1.382 + * 
   1.383 + * @param time_str the string to be converted
   1.384 + * @return time_t the time converted value
   1.385 + */
   1.386 +GTimeVal*
   1.387 +gmyth_util_string_to_time_val ( const gchar* time_str )
   1.388 +{
   1.389 +	GTimeVal *time = gmyth_util_string_to_time_val_fmt ( "%Y-%m-%d %H:%M:%S", time_str );	
   1.390 +	
   1.391 +	return time;
   1.392 +}
   1.393 +
   1.394 +/** Decodes a long long variable from the string list
   1.395 + * format of the myhtprotocol.
   1.396 + * 
   1.397 + * @param strlist the string list of mythprotocol values
   1.398 + * @param offset  the list node offset of the long long variable
   1.399 + * @return gint64  the long long converted value
   1.400 + */
   1.401 +gint64
   1.402 +gmyth_util_decode_long_long(GMythStringList *strlist, guint offset) 
   1.403 +{
   1.404 +
   1.405 +	gint64 ret_value = 0LL;
   1.406 +
   1.407 +	g_return_val_if_fail( strlist != NULL, ret_value );
   1.408 +
   1.409 +	if ( offset > gmyth_string_list_length( strlist ))
   1.410 +		g_printerr( "[%s] Offset is greater than the Stringlist (offset = %d)!\n", 
   1.411 +                    __FUNCTION__, offset );
   1.412 +    
   1.413 +	g_return_val_if_fail( offset < gmyth_string_list_length( strlist ), ret_value );
   1.414 +
   1.415 +	gint l1 = gmyth_string_list_get_int( strlist, offset );
   1.416 +	gint l2 = gmyth_string_list_get_int( strlist, offset + 1 );
   1.417 +
   1.418 +	ret_value = (l2 /*& 0xffffffffLL*/) | ( (gint64)l1 << 32 );
   1.419 +
   1.420 +	return ret_value;
   1.421 +
   1.422 +}
   1.423 +
   1.424 +gboolean
   1.425 +gmyth_util_file_exists (GMythBackendInfo *backend_info, const gchar* filename)
   1.426 +{
   1.427 +    GMythSocket *socket;
   1.428 +    gboolean res;
   1.429 +    
   1.430 +    socket = gmyth_socket_new ();
   1.431 +    res = gmyth_socket_connect_to_backend (socket, backend_info->hostname,
   1.432 +		    backend_info->port, TRUE);
   1.433 +
   1.434 +    if (res == TRUE) {
   1.435 +        GMythStringList *slist;
   1.436 +        GMythProgramInfo *program = NULL;
   1.437 +
   1.438 +        program = gmyth_program_info_new();
   1.439 +        program->pathname = g_string_new (filename);
   1.440 +
   1.441 +        slist = gmyth_string_list_new ();
   1.442 +        gmyth_string_list_append_char_array (slist, "QUERY_CHECKFILE");
   1.443 +
   1.444 +        gmyth_program_info_to_string_list (program, slist);
   1.445 +
   1.446 +        gmyth_socket_sendreceive_stringlist (socket, slist);
   1.447 +
   1.448 +        res = (gmyth_string_list_get_int (slist, 0) == 1);
   1.449 +    
   1.450 +        g_object_unref (program);
   1.451 +
   1.452 +        g_object_unref (slist);
   1.453 +
   1.454 +        gmyth_socket_close_connection (socket);
   1.455 +    }
   1.456 +    g_object_unref (socket);
   1.457 +    return res;    
   1.458 +}
   1.459 +
   1.460 +
   1.461 +#if !GLIB_CHECK_VERSION (2, 10, 0)
   1.462 +
   1.463 +/* Hacked from glib 2.10 <gtime.c> */
   1.464 +
   1.465 +static time_t
   1.466 +mktime_utc (struct tm *tm)
   1.467 +{
   1.468 +  time_t retval;
   1.469 +  
   1.470 +#ifndef HAVE_TIMEGM
   1.471 +  static const gint days_before[] =
   1.472 +  {
   1.473 +    0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
   1.474 +  };
   1.475 +#endif
   1.476 +
   1.477 +#ifndef HAVE_TIMEGM
   1.478 +  if (tm->tm_mon < 0 || tm->tm_mon > 11)
   1.479 +    return (time_t) -1;
   1.480 +
   1.481 +  retval = (tm->tm_year - 70) * 365;
   1.482 +  retval += (tm->tm_year - 68) / 4;
   1.483 +  retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
   1.484 +  
   1.485 +  if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
   1.486 +    retval -= 1;
   1.487 +  
   1.488 +  retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
   1.489 +#else
   1.490 +  retval = timegm (tm);
   1.491 +#endif /* !HAVE_TIMEGM */
   1.492 +  
   1.493 +  return retval;
   1.494 +}
   1.495 +
   1.496 +gboolean
   1.497 +g_time_val_from_iso8601 (const gchar *iso_date,
   1.498 +                         GTimeVal    *time_)
   1.499 +{
   1.500 +  struct tm tm;
   1.501 +  long val;
   1.502 +
   1.503 +  g_return_val_if_fail (iso_date != NULL, FALSE);
   1.504 +  g_return_val_if_fail (time_ != NULL, FALSE);
   1.505 +
   1.506 +  val = strtoul (iso_date, (char **)&iso_date, 10);
   1.507 +  if (*iso_date == '-')
   1.508 +    {
   1.509 +      /* YYYY-MM-DD */
   1.510 +      tm.tm_year = val - 1900;
   1.511 +      iso_date++;
   1.512 +      tm.tm_mon = strtoul (iso_date, (char **)&iso_date, 10) - 1;
   1.513 +      
   1.514 +      if (*iso_date++ != '-')
   1.515 +        return FALSE;
   1.516 +      
   1.517 +      tm.tm_mday = strtoul (iso_date, (char **)&iso_date, 10);
   1.518 +    }
   1.519 +  else
   1.520 +    {
   1.521 +      /* YYYYMMDD */
   1.522 +      tm.tm_mday = val % 100;
   1.523 +      tm.tm_mon = (val % 10000) / 100 - 1;
   1.524 +      tm.tm_year = val / 10000 - 1900;
   1.525 +    }
   1.526 +
   1.527 +  if (*iso_date++ != 'T')
   1.528 +    return FALSE;
   1.529 +  
   1.530 +  val = strtoul (iso_date, (char **)&iso_date, 10);
   1.531 +  if (*iso_date == ':')
   1.532 +    {
   1.533 +      /* hh:mm:ss */
   1.534 +      tm.tm_hour = val;
   1.535 +      iso_date++;
   1.536 +      tm.tm_min = strtoul (iso_date, (char **)&iso_date, 10);
   1.537 +      
   1.538 +      if (*iso_date++ != ':')
   1.539 +        return FALSE;
   1.540 +      
   1.541 +      tm.tm_sec = strtoul (iso_date, (char **)&iso_date, 10);
   1.542 +    }
   1.543 +  else
   1.544 +    {
   1.545 +      /* hhmmss */
   1.546 +      tm.tm_sec = val % 100;
   1.547 +      tm.tm_min = (val % 10000) / 100;
   1.548 +      tm.tm_hour = val / 10000;
   1.549 +    }
   1.550 +
   1.551 +  time_->tv_sec = mktime_utc (&tm);
   1.552 +  time_->tv_usec = 1;
   1.553 +  
   1.554 +  if (*iso_date == '.')
   1.555 +    time_->tv_usec = strtoul (iso_date + 1, (char **)&iso_date, 10);
   1.556 +    
   1.557 +  if (*iso_date == '+' || *iso_date == '-')
   1.558 +    {
   1.559 +      gint sign = (*iso_date == '+') ? -1 : 1;
   1.560 +      
   1.561 +      val = 60 * strtoul (iso_date + 1, (char **)&iso_date, 10);
   1.562 +      
   1.563 +      if (*iso_date == ':')
   1.564 +    val = 60 * val + strtoul (iso_date + 1, NULL, 10);
   1.565 +      else
   1.566 +        val = 60 * (val / 100) + (val % 100);
   1.567 +
   1.568 +      time_->tv_sec += (time_t) (val * sign);
   1.569 +    }
   1.570 +
   1.571 +  return TRUE;
   1.572 +}
   1.573 +
   1.574 +
   1.575 +gchar *
   1.576 +g_time_val_to_iso8601 (GTimeVal *time_)
   1.577 +{
   1.578 +  gchar *retval;
   1.579 +
   1.580 +  g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
   1.581 +
   1.582 +#define ISO_8601_LEN    21
   1.583 +#define ISO_8601_FORMAT "%Y-%m-%dT%H:%M:%SZ"
   1.584 +  retval = g_new0 (gchar, ISO_8601_LEN + 1);
   1.585 +  
   1.586 +  strftime (retval, ISO_8601_LEN,
   1.587 +        ISO_8601_FORMAT,
   1.588 +        gmtime (&(time_->tv_sec)));
   1.589 +  
   1.590 +  return retval;
   1.591 +}
   1.592 +
   1.593 +
   1.594 +/* Hacked from glib 2.10 <gdate.c> */
   1.595 +
   1.596 +void         
   1.597 +g_date_set_time_t (GDate *date,
   1.598 +           time_t timet)
   1.599 +{
   1.600 +  struct tm tm;
   1.601 +  
   1.602 +  g_return_if_fail (date != NULL);
   1.603 +  
   1.604 +#ifdef HAVE_LOCALTIME_R
   1.605 +  localtime_r (&timet, &tm);
   1.606 +#else
   1.607 +  {
   1.608 +    struct tm *ptm = localtime (&timet);
   1.609 +
   1.610 +    if (ptm == NULL)
   1.611 +      {
   1.612 +    /* Happens at least in Microsoft's C library if you pass a
   1.613 +     * negative time_t. Use 2000-01-01 as default date.
   1.614 +     */
   1.615 +#ifndef G_DISABLE_CHECKS
   1.616 +    g_return_if_fail_warning (G_LOG_DOMAIN, "g_date_set_time", "ptm != NULL");
   1.617 +#endif
   1.618 +
   1.619 +    tm.tm_mon = 0;
   1.620 +    tm.tm_mday = 1;
   1.621 +    tm.tm_year = 100;
   1.622 +      }
   1.623 +    else
   1.624 +      memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
   1.625 +  }
   1.626 +#endif
   1.627 +  
   1.628 +  date->julian = FALSE;
   1.629 +  
   1.630 +  date->month = tm.tm_mon + 1;
   1.631 +  date->day   = tm.tm_mday;
   1.632 +  date->year  = tm.tm_year + 1900;
   1.633 +  
   1.634 +  g_return_if_fail (g_date_valid_dmy (date->day, date->month, date->year));
   1.635 +  
   1.636 +  date->dmy    = TRUE;
   1.637 +}
   1.638 +
   1.639 +
   1.640 +void
   1.641 +g_date_set_time_val (GDate    *date,
   1.642 +             GTimeVal *timeval)
   1.643 +{
   1.644 +  g_date_set_time_t (date, (time_t) timeval->tv_sec);
   1.645 +}
   1.646 +
   1.647 +
   1.648 +
   1.649 +
   1.650 +#endif