leo_sobral@1
|
1 |
/**
|
leo_sobral@1
|
2 |
* GMyth Library
|
leo_sobral@1
|
3 |
*
|
leo_sobral@1
|
4 |
* @file gmyth/gmyth_util.c
|
leo_sobral@1
|
5 |
*
|
leo_sobral@1
|
6 |
* @brief <p> This component provides utility functions.
|
leo_sobral@1
|
7 |
*
|
leo_sobral@1
|
8 |
* Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
|
leo_sobral@1
|
9 |
* @author Hallyson Luiz de Morais Melo <hallyson.melo@indt.org.br>
|
leo_sobral@1
|
10 |
*
|
leo_sobral@1
|
11 |
*//*
|
leo_sobral@1
|
12 |
*
|
leo_sobral@1
|
13 |
* This program is free software; you can redistribute it and/or modify
|
leo_sobral@1
|
14 |
* it under the terms of the GNU Lesser General Public License as published by
|
leo_sobral@1
|
15 |
* the Free Software Foundation; either version 2 of the License, or
|
leo_sobral@1
|
16 |
* (at your option) any later version.
|
leo_sobral@1
|
17 |
*
|
leo_sobral@1
|
18 |
* This program is distributed in the hope that it will be useful,
|
leo_sobral@1
|
19 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
leo_sobral@1
|
20 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
leo_sobral@1
|
21 |
* GNU General Public License for more details.
|
leo_sobral@1
|
22 |
*
|
leo_sobral@1
|
23 |
* You should have received a copy of the GNU Lesser General Public License
|
leo_sobral@1
|
24 |
* along with this program; if not, write to the Free Software
|
leo_sobral@1
|
25 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
leo_sobral@1
|
26 |
*/
|
leo_sobral@213
|
27 |
|
leo_sobral@213
|
28 |
#ifdef HAVE_CONFIG_H
|
leo_sobral@213
|
29 |
#include "config.h"
|
leo_sobral@213
|
30 |
#endif
|
leo_sobral@213
|
31 |
|
rosfran@214
|
32 |
#define _XOPEN_SOURCE
|
rosfran@214
|
33 |
#define _XOPEN_SOURCE_EXTENDED
|
rosfran@223
|
34 |
#define __USE_MISC
|
leo_sobral@1
|
35 |
|
melunko@125
|
36 |
#include <glib.h>
|
melunko@125
|
37 |
#include <glib/gprintf.h>
|
rosfran@214
|
38 |
#include <time.h>
|
rosfran@214
|
39 |
#include <sys/time.h>
|
rosfran@223
|
40 |
#include <sys/timex.h>
|
melunko@125
|
41 |
|
rosfran@214
|
42 |
#include "gmyth.h"
|
melunko@125
|
43 |
|
renatofilho@222
|
44 |
#if !GLIB_CHECK_VERSION (2, 10, 0)
|
renatofilho@222
|
45 |
gchar *
|
renatofilho@222
|
46 |
g_time_val_to_iso8601 (GTimeVal *time_);
|
renatofilho@222
|
47 |
gboolean
|
renatofilho@222
|
48 |
g_time_val_from_iso8601 (const gchar *iso_date,
|
renatofilho@222
|
49 |
GTimeVal *time_);
|
renatofilho@222
|
50 |
void
|
renatofilho@222
|
51 |
g_date_set_time_val (GDate *date,
|
renatofilho@222
|
52 |
GTimeVal *timeval);
|
renatofilho@222
|
53 |
|
renatofilho@222
|
54 |
#endif
|
renatofilho@222
|
55 |
|
rosfran@214
|
56 |
static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
|
leo_sobral@1
|
57 |
|
leo_sobral@1
|
58 |
/** Converts a time_t struct in a GString at ISO standard format
|
leo_sobral@1
|
59 |
* (e.g. 2006-07-20T09:56:41).
|
leo_sobral@1
|
60 |
*
|
leo_sobral@1
|
61 |
* The returned GString memory should be deallocated from
|
leo_sobral@1
|
62 |
* the calling function.
|
leo_sobral@1
|
63 |
*
|
leo_sobral@1
|
64 |
* @param time_value the time value to be converted
|
leo_sobral@1
|
65 |
* @return GString* the converted isoformat string
|
leo_sobral@1
|
66 |
*/
|
leo_sobral@1
|
67 |
GString*
|
leo_sobral@1
|
68 |
gmyth_util_time_to_isoformat (time_t time_value)
|
leo_sobral@1
|
69 |
{
|
leo_sobral@1
|
70 |
struct tm tm_time;
|
leo_sobral@1
|
71 |
GString *result;
|
leo_sobral@1
|
72 |
|
rosfran@214
|
73 |
g_static_mutex_lock ( &mutex );
|
rosfran@214
|
74 |
|
leo_sobral@1
|
75 |
if (localtime_r(&time_value, &tm_time) == NULL) {
|
rosfran@214
|
76 |
g_static_mutex_unlock ( &mutex );
|
leo_sobral@1
|
77 |
g_warning ("gmyth_util_time_to_isoformat convertion error!\n");
|
leo_sobral@1
|
78 |
return NULL;
|
leo_sobral@1
|
79 |
}
|
leo_sobral@1
|
80 |
|
leo_sobral@1
|
81 |
result = g_string_sized_new(20);
|
leo_sobral@1
|
82 |
g_string_printf(result, "%04d-%02d-%02dT%02d:%02d:%02d",
|
leo_sobral@1
|
83 |
tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
|
leo_sobral@1
|
84 |
tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec);
|
rosfran@214
|
85 |
|
rosfran@214
|
86 |
gmyth_debug( "Result (ISO 8601) = %s", result->str );
|
rosfran@223
|
87 |
|
rosfran@214
|
88 |
g_static_mutex_unlock ( &mutex );
|
leo_sobral@1
|
89 |
|
leo_sobral@1
|
90 |
return result;
|
rosfran@214
|
91 |
|
rosfran@214
|
92 |
|
rosfran@214
|
93 |
}
|
rosfran@214
|
94 |
|
rosfran@214
|
95 |
/** Converts a time_t struct in a GString at ISO standard format
|
rosfran@214
|
96 |
* (e.g. 2006-07-20T09:56:41).
|
rosfran@214
|
97 |
*
|
rosfran@214
|
98 |
* The returned GString memory should be deallocated from
|
rosfran@214
|
99 |
* the calling function.
|
rosfran@214
|
100 |
*
|
rosfran@214
|
101 |
* @param time_value the GTimeValue to be converted
|
rosfran@214
|
102 |
* @return GString* the converted isoformat string
|
rosfran@214
|
103 |
*/
|
rosfran@223
|
104 |
gchar*
|
rosfran@223
|
105 |
gmyth_util_time_to_isoformat_from_time_val_fmt ( const gchar *fmt_string, const GTimeVal* time_val )
|
rosfran@214
|
106 |
{
|
rosfran@214
|
107 |
|
rosfran@221
|
108 |
gchar *result = NULL;
|
rosfran@221
|
109 |
struct tm *tm_time = NULL;
|
rosfran@214
|
110 |
|
rosfran@221
|
111 |
gint buffer_len = 0;
|
rosfran@214
|
112 |
|
rosfran@223
|
113 |
g_return_val_if_fail( fmt_string != NULL, NULL );
|
rosfran@223
|
114 |
|
rosfran@221
|
115 |
g_return_val_if_fail( time_val != NULL, NULL );
|
rosfran@221
|
116 |
|
rosfran@223
|
117 |
time_t time = time_val->tv_sec;// + (gint)( time_val->tv_usec / G_USEC_PER_SEC );
|
rosfran@214
|
118 |
|
rosfran@221
|
119 |
tm_time = g_malloc0( sizeof(struct tm) );
|
rosfran@221
|
120 |
|
rosfran@221
|
121 |
g_static_mutex_lock ( &mutex );
|
rosfran@221
|
122 |
|
rosfran@223
|
123 |
if ( NULL == localtime_r( &time, tm_time ) ) {
|
rosfran@221
|
124 |
g_warning ("gmyth_util_time_to_isoformat convertion error!\n");
|
rosfran@221
|
125 |
} else {
|
rosfran@221
|
126 |
// we first check the return of strftime to allocate a buffer of the correct size
|
rosfran@221
|
127 |
buffer_len = strftime( NULL, SSIZE_MAX, fmt_string, tm_time );
|
rosfran@223
|
128 |
if ( buffer_len > 0 ) {
|
rosfran@221
|
129 |
result = g_malloc0( buffer_len + 1 );
|
rosfran@223
|
130 |
if( result == NULL ){
|
rosfran@221
|
131 |
g_static_mutex_unlock ( &mutex );
|
rosfran@221
|
132 |
g_warning ("gmyth_util_time_to_isoformat convertion error!\n");
|
rosfran@221
|
133 |
return NULL;
|
rosfran@221
|
134 |
}
|
rosfran@221
|
135 |
strftime( result, buffer_len + 1, fmt_string, tm_time );
|
rosfran@221
|
136 |
gmyth_debug( "Dateline (ISO result): %s\n", result );
|
rosfran@221
|
137 |
}
|
rosfran@221
|
138 |
|
rosfran@214
|
139 |
}
|
rosfran@214
|
140 |
|
rosfran@223
|
141 |
gmyth_debug( "Result (strftime) = %s", result );
|
rosfran@214
|
142 |
|
rosfran@221
|
143 |
//strptime( result, "%Y-%m-%dT%H:%M:%SZ", tm_time );
|
rosfran@214
|
144 |
|
rosfran@221
|
145 |
//strftime( result, strlen(result), fmt_string, tm_time );
|
rosfran@214
|
146 |
|
rosfran@214
|
147 |
g_static_mutex_unlock ( &mutex );
|
rosfran@214
|
148 |
|
rosfran@214
|
149 |
gmyth_debug( "Result (ISO 8601) = %s", result );
|
rosfran@214
|
150 |
|
rosfran@214
|
151 |
return result;
|
rosfran@214
|
152 |
|
rosfran@214
|
153 |
}
|
rosfran@214
|
154 |
|
rosfran@214
|
155 |
/** Converts a time_t struct in a GString at ISO standard format
|
rosfran@214
|
156 |
* (e.g. 2006-07-20T09:56:41).
|
rosfran@214
|
157 |
*
|
rosfran@214
|
158 |
* The returned GString memory should be deallocated from
|
rosfran@214
|
159 |
* the calling function.
|
rosfran@214
|
160 |
*
|
rosfran@214
|
161 |
* @param time_value the GTimeValue to be converted
|
rosfran@214
|
162 |
* @return GString* the converted isoformat string
|
rosfran@214
|
163 |
*/
|
rosfran@214
|
164 |
gchar*
|
rosfran@223
|
165 |
gmyth_util_time_to_isoformat_from_time_val ( const GTimeVal* time )
|
rosfran@214
|
166 |
{
|
rosfran@223
|
167 |
gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%Y-%m-%d %H:%M:%S", time );
|
rosfran@214
|
168 |
//result[10] = ' ';
|
rosfran@214
|
169 |
//result[ strlen(result) - 1] = '\0';
|
rosfran@214
|
170 |
|
rosfran@214
|
171 |
return result;
|
rosfran@214
|
172 |
}
|
rosfran@214
|
173 |
|
rosfran@214
|
174 |
/** Converts a time_t struct in a GString at ISO standard format
|
rosfran@214
|
175 |
* (e.g. 2006-07-20T09:56:41).
|
rosfran@214
|
176 |
*
|
rosfran@214
|
177 |
* The returned GString memory should be deallocated from
|
rosfran@214
|
178 |
* the calling function.
|
rosfran@214
|
179 |
*
|
rosfran@214
|
180 |
* @param time_value the GTimeValue to be converted
|
rosfran@214
|
181 |
* @return GString* the converted isoformat string
|
rosfran@214
|
182 |
*/
|
rosfran@214
|
183 |
gchar*
|
rosfran@223
|
184 |
gmyth_util_time_to_string_only_date ( const GTimeVal* time )
|
rosfran@214
|
185 |
{
|
rosfran@223
|
186 |
gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%Y-%m-%d", time );
|
rosfran@223
|
187 |
//result[10] = ' ';
|
rosfran@223
|
188 |
//result[ strlen(result) - 1] = '\0';
|
rosfran@214
|
189 |
return result;
|
rosfran@214
|
190 |
}
|
rosfran@214
|
191 |
|
rosfran@214
|
192 |
/** Converts a time_t struct in a GString at ISO standard format
|
rosfran@214
|
193 |
* (e.g. 2006-07-20T09:56:41).
|
rosfran@214
|
194 |
*
|
rosfran@214
|
195 |
* The returned GString memory should be deallocated from
|
rosfran@214
|
196 |
* the calling function.
|
rosfran@214
|
197 |
*
|
rosfran@214
|
198 |
* @param time_value the GTimeValue to be converted
|
rosfran@214
|
199 |
* @return GString* the converted isoformat string
|
rosfran@214
|
200 |
*/
|
rosfran@214
|
201 |
gchar*
|
rosfran@223
|
202 |
gmyth_util_time_to_string_only_time ( const GTimeVal* time )
|
rosfran@214
|
203 |
{
|
rosfran@223
|
204 |
gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%H:%M:%S", time );
|
rosfran@223
|
205 |
//result[10] = ' ';
|
rosfran@223
|
206 |
//result[ strlen(result) - 1] = '\0';
|
rosfran@214
|
207 |
return result;
|
leo_sobral@1
|
208 |
}
|
leo_sobral@1
|
209 |
|
leo_sobral@1
|
210 |
/** Converts a time_t struct in a GString to the following
|
leo_sobral@1
|
211 |
* format (e.g. 2006-07-20 09:56:41).
|
leo_sobral@1
|
212 |
*
|
leo_sobral@1
|
213 |
* The returned GString memory should be deallocated from
|
leo_sobral@1
|
214 |
* the calling function.
|
leo_sobral@1
|
215 |
*
|
leo_sobral@1
|
216 |
* @param time_value the time value to be converted
|
leo_sobral@1
|
217 |
* @return GString* the converted string
|
leo_sobral@1
|
218 |
*/
|
leo_sobral@1
|
219 |
GString*
|
leo_sobral@1
|
220 |
gmyth_util_time_to_string (time_t time_value)
|
leo_sobral@1
|
221 |
{
|
leo_sobral@1
|
222 |
GString *result = gmyth_util_time_to_isoformat (time_value);
|
leo_sobral@1
|
223 |
result->str[10] = ' ';
|
rosfran@223
|
224 |
result->str[ strlen(result->str) - 1] = '\0';
|
leo_sobral@1
|
225 |
|
leo_sobral@1
|
226 |
return result;
|
leo_sobral@1
|
227 |
}
|
leo_sobral@1
|
228 |
|
rosfran@214
|
229 |
/** Converts a time_t struct in a GString to the following
|
rosfran@214
|
230 |
* format (e.g. 2006-07-20 09:56:41).
|
rosfran@214
|
231 |
*
|
rosfran@214
|
232 |
* The returned GString memory should be deallocated from
|
rosfran@214
|
233 |
* the calling function.
|
rosfran@214
|
234 |
*
|
rosfran@214
|
235 |
* @param time_value the time value to be converted
|
rosfran@214
|
236 |
* @return GString* the converted string
|
rosfran@214
|
237 |
*/
|
rosfran@214
|
238 |
gchar*
|
rosfran@223
|
239 |
gmyth_util_time_to_string_from_time_val ( const GTimeVal *time_val )
|
rosfran@214
|
240 |
{
|
rosfran@223
|
241 |
gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt ( "%Y-%m-%d %H:%M:%S", time_val );
|
rosfran@223
|
242 |
//result[10] = ' ';
|
rosfran@214
|
243 |
|
rosfran@214
|
244 |
return result;
|
rosfran@214
|
245 |
}
|
rosfran@214
|
246 |
|
leo_sobral@1
|
247 |
/** Converts a GString in the following format
|
leo_sobral@1
|
248 |
* (e.g. 2006-07-20 09:56:41) to a time_t struct.
|
leo_sobral@1
|
249 |
*
|
leo_sobral@1
|
250 |
* @param time_str the string to be converted
|
leo_sobral@1
|
251 |
* @return time_t the time converted value
|
leo_sobral@1
|
252 |
*/
|
leo_sobral@1
|
253 |
time_t
|
leo_sobral@1
|
254 |
gmyth_util_string_to_time (GString* time_str)
|
leo_sobral@1
|
255 |
{
|
rosfran@223
|
256 |
gint year, month, day, hour, min, sec;
|
leo_sobral@49
|
257 |
|
rosfran@214
|
258 |
gmyth_debug( "[%s] time_str = %s. [%s]\n", __FUNCTION__, time_str != NULL ?
|
rosfran@223
|
259 |
time_str->str : "[time string is NULL!]", time_str->str );
|
leo_sobral@1
|
260 |
|
rosfran@223
|
261 |
if ( sscanf (time_str->str, "%04d-%02d-%02d %02d:%02d:%02d",
|
rosfran@223
|
262 |
&year, &month, &day, &hour, &min, &sec) < 3 ) {
|
leo_sobral@1
|
263 |
g_warning ("GMythUtil: isoformat_to_time converter error!\n");
|
leo_sobral@1
|
264 |
return 0;
|
rosfran@214
|
265 |
}
|
rosfran@214
|
266 |
|
rosfran@214
|
267 |
g_static_mutex_lock ( &mutex );
|
rosfran@214
|
268 |
|
rosfran@214
|
269 |
struct tm* tm_time = g_malloc0( sizeof(struct tm) );
|
rosfran@223
|
270 |
tm_time->tm_year = year - 1900;
|
rosfran@223
|
271 |
tm_time->tm_mon = month - 1;
|
rosfran@223
|
272 |
tm_time->tm_mday = day;
|
rosfran@223
|
273 |
tm_time->tm_hour = hour;
|
rosfran@223
|
274 |
tm_time->tm_min = min;
|
rosfran@223
|
275 |
tm_time->tm_sec = sec;
|
rosfran@214
|
276 |
|
rosfran@214
|
277 |
g_static_mutex_unlock ( &mutex );
|
rosfran@214
|
278 |
|
rosfran@214
|
279 |
return mktime( tm_time );
|
rosfran@214
|
280 |
}
|
rosfran@214
|
281 |
|
rosfran@214
|
282 |
/** Converts a GString in the following format
|
rosfran@214
|
283 |
* (e.g. 2006-07-20 09:56:41) to a time_t struct.
|
rosfran@214
|
284 |
*
|
rosfran@214
|
285 |
* @param time_str the string to be converted
|
rosfran@214
|
286 |
* @return time_t the time converted value
|
rosfran@214
|
287 |
*/
|
rosfran@223
|
288 |
struct tm*
|
rosfran@223
|
289 |
gmyth_util_time_val_to_date ( const GTimeVal* time )
|
rosfran@214
|
290 |
{
|
rosfran@223
|
291 |
struct tm *date = g_malloc0( sizeof( struct tm ) );
|
rosfran@223
|
292 |
time_t time_micros = time->tv_sec;// + (gint)( time->tv_usec / G_USEC_PER_SEC );
|
rosfran@214
|
293 |
|
rosfran@214
|
294 |
if ( NULL == date ) {
|
rosfran@223
|
295 |
g_warning ( "GMythUtil: GDate *gmyth_util_time_val_to_date (GTimeVal* time) - converter error!\n" );
|
rosfran@223
|
296 |
return NULL;
|
leo_sobral@1
|
297 |
}
|
rosfran@214
|
298 |
|
rosfran@223
|
299 |
if ( NULL == localtime_r( &time_micros, date ) ) {
|
rosfran@223
|
300 |
g_warning ( "gmyth_util_time_to_isoformat convertion error!\n" );
|
rosfran@223
|
301 |
return NULL;
|
rosfran@223
|
302 |
}
|
rosfran@223
|
303 |
|
rosfran@223
|
304 |
gmyth_debug( "Converted from GTimeVal == %s to GDate\n", asctime( date ) );
|
rosfran@214
|
305 |
|
rosfran@214
|
306 |
return date;
|
rosfran@214
|
307 |
}
|
rosfran@214
|
308 |
|
rosfran@214
|
309 |
/** Converts a GString in the following format
|
rosfran@214
|
310 |
* (e.g. 2006-07-20 09:56:41) to a time_t struct.
|
rosfran@214
|
311 |
*
|
rosfran@214
|
312 |
* @param time_str the string to be converted
|
rosfran@214
|
313 |
* @return time_t the time converted value
|
rosfran@214
|
314 |
*/
|
rosfran@214
|
315 |
GTimeVal*
|
rosfran@223
|
316 |
gmyth_util_string_to_time_val_fmt ( const gchar *fmt_string, const gchar* time_str )
|
rosfran@214
|
317 |
{
|
rosfran@223
|
318 |
GTimeVal *time = g_new0( GTimeVal, 1 );
|
rosfran@223
|
319 |
struct tm* tm_time = NULL;
|
rosfran@223
|
320 |
time_t time_micros;
|
rosfran@223
|
321 |
gchar *result = NULL;
|
rosfran@214
|
322 |
|
rosfran@223
|
323 |
gmyth_debug( "[%s] time_str = %s. [%s]\n", time_str, time_str != NULL ?
|
rosfran@223
|
324 |
time_str : "[time string is NULL!]", time_str );
|
rosfran@214
|
325 |
|
rosfran@223
|
326 |
if ( NULL == time_str ) {
|
rosfran@214
|
327 |
g_warning ("GMythUtil: isoformat_to_time converter error!\n");
|
rosfran@223
|
328 |
time = NULL;
|
rosfran@223
|
329 |
goto done;
|
rosfran@214
|
330 |
}
|
rosfran@214
|
331 |
|
rosfran@214
|
332 |
g_static_mutex_lock ( &mutex );
|
rosfran@214
|
333 |
|
rosfran@223
|
334 |
tm_time = g_malloc0( sizeof(struct tm) );
|
rosfran@223
|
335 |
|
rosfran@223
|
336 |
/* we first check the return of strftime to allocate a buffer of the correct size */
|
rosfran@223
|
337 |
result = strptime( time_str, "%Y-%m-%dT%H:%M:%S", tm_time );
|
rosfran@223
|
338 |
if ( NULL == result ) {
|
rosfran@223
|
339 |
/* we first check the return of strftime to allocate a buffer of the correct size */
|
rosfran@223
|
340 |
result = strptime( time_str, "%Y-%m-%dT%H:%M:%SZ", tm_time );
|
rosfran@223
|
341 |
if ( NULL == result ) {
|
rosfran@223
|
342 |
/* we first check the return of strftime to allocate a buffer of the correct size */
|
rosfran@223
|
343 |
result = strptime( time_str, "%Y-%m-%d %H:%M:%S", tm_time );
|
rosfran@223
|
344 |
if ( NULL == result ) {
|
rosfran@223
|
345 |
g_static_mutex_unlock ( &mutex );
|
rosfran@223
|
346 |
gmyth_debug( "Dateline (ISO result): %s\n", result );
|
rosfran@223
|
347 |
time = NULL;
|
rosfran@223
|
348 |
goto done;
|
rosfran@223
|
349 |
}
|
rosfran@223
|
350 |
}
|
rosfran@223
|
351 |
}
|
rosfran@223
|
352 |
|
rosfran@223
|
353 |
time_micros = mktime( tm_time );
|
rosfran@223
|
354 |
|
rosfran@223
|
355 |
time->tv_sec = time_micros; // + (gint)( time_val->tv_usec / G_USEC_PER_SEC );
|
rosfran@214
|
356 |
|
rosfran@223
|
357 |
//strftime( result, SSIZE_MAX, "%Y-%m-%dT%H:%M:%S", tm_time );
|
rosfran@214
|
358 |
|
rosfran@223
|
359 |
gmyth_debug( "After mktime call... = %s", asctime(tm_time) );
|
rosfran@223
|
360 |
|
rosfran@214
|
361 |
g_static_mutex_unlock ( &mutex );
|
rosfran@223
|
362 |
|
rosfran@223
|
363 |
done:
|
rosfran@223
|
364 |
//if ( tm_time )
|
rosfran@223
|
365 |
// g_free( tm_time );
|
rosfran@223
|
366 |
|
rosfran@223
|
367 |
//if ( result )
|
rosfran@223
|
368 |
// g_free( result );
|
rosfran@223
|
369 |
|
rosfran@223
|
370 |
return time;
|
rosfran@223
|
371 |
}
|
rosfran@223
|
372 |
|
rosfran@223
|
373 |
/** Converts a GString in the following format
|
rosfran@223
|
374 |
* (e.g. 2006-07-20 09:56:41) to a time_t struct.
|
rosfran@223
|
375 |
*
|
rosfran@223
|
376 |
* @param time_str the string to be converted
|
rosfran@223
|
377 |
* @return time_t the time converted value
|
rosfran@223
|
378 |
*/
|
rosfran@223
|
379 |
GTimeVal*
|
rosfran@223
|
380 |
gmyth_util_string_to_time_val ( const gchar* time_str )
|
rosfran@223
|
381 |
{
|
rosfran@223
|
382 |
GTimeVal *time = gmyth_util_string_to_time_val_fmt ( "%Y-%m-%d %H:%M:%S", time_str );
|
rosfran@214
|
383 |
|
rosfran@214
|
384 |
return time;
|
leo_sobral@1
|
385 |
}
|
leo_sobral@1
|
386 |
|
leo_sobral@1
|
387 |
/** Decodes a long long variable from the string list
|
leo_sobral@1
|
388 |
* format of the myhtprotocol.
|
leo_sobral@1
|
389 |
*
|
leo_sobral@1
|
390 |
* @param strlist the string list of mythprotocol values
|
leo_sobral@1
|
391 |
* @param offset the list node offset of the long long variable
|
rosfran@35
|
392 |
* @return gint64 the long long converted value
|
leo_sobral@1
|
393 |
*/
|
rosfran@35
|
394 |
gint64
|
leo_sobral@1
|
395 |
gmyth_util_decode_long_long(GMythStringList *strlist, guint offset)
|
leo_sobral@1
|
396 |
{
|
leo_sobral@1
|
397 |
|
rosfran@35
|
398 |
gint64 ret_value = 0LL;
|
leo_sobral@1
|
399 |
|
leo_sobral@1
|
400 |
g_return_val_if_fail( strlist != NULL, ret_value );
|
leo_sobral@1
|
401 |
|
rosfran@35
|
402 |
if ( offset > gmyth_string_list_length( strlist ))
|
rosfran@35
|
403 |
g_printerr( "[%s] Offset is greater than the Stringlist (offset = %d)!\n",
|
leo_sobral@1
|
404 |
__FUNCTION__, offset );
|
leo_sobral@1
|
405 |
|
leo_sobral@1
|
406 |
g_return_val_if_fail( offset < gmyth_string_list_length( strlist ), ret_value );
|
leo_sobral@1
|
407 |
|
leo_sobral@1
|
408 |
gint l1 = gmyth_string_list_get_int( strlist, offset );
|
leo_sobral@1
|
409 |
gint l2 = gmyth_string_list_get_int( strlist, offset + 1 );
|
leo_sobral@1
|
410 |
|
rosfran@35
|
411 |
ret_value = (l2 /*& 0xffffffffLL*/) | ( (gint64)l1 << 32 );
|
leo_sobral@1
|
412 |
|
leo_sobral@1
|
413 |
return ret_value;
|
leo_sobral@1
|
414 |
|
leo_sobral@1
|
415 |
}
|
leo_sobral@1
|
416 |
|
melunko@125
|
417 |
gboolean
|
rosfran@214
|
418 |
gmyth_util_file_exists (GMythBackendInfo *backend_info, const gchar* filename)
|
melunko@125
|
419 |
{
|
melunko@125
|
420 |
GMythSocket *socket;
|
melunko@125
|
421 |
gboolean res;
|
melunko@125
|
422 |
|
melunko@276
|
423 |
socket = gmyth_socket_new ();
|
renatofilho@147
|
424 |
res = gmyth_socket_connect_to_backend (socket, backend_info->hostname,
|
melunko@125
|
425 |
backend_info->port, TRUE);
|
rosfran@35
|
426 |
|
renatofilho@147
|
427 |
if (res == TRUE) {
|
renatofilho@147
|
428 |
GMythStringList *slist;
|
renatofilho@147
|
429 |
GMythProgramInfo *program;
|
melunko@125
|
430 |
|
renatofilho@147
|
431 |
program = g_new0 (GMythProgramInfo, 1);
|
renatofilho@147
|
432 |
program->pathname = g_string_new (filename);
|
melunko@125
|
433 |
|
renatofilho@147
|
434 |
slist = gmyth_string_list_new ();
|
renatofilho@147
|
435 |
gmyth_string_list_append_char_array (slist, "QUERY_CHECKFILE");
|
melunko@125
|
436 |
|
renatofilho@147
|
437 |
gmyth_program_info_to_string_list (program, slist);
|
melunko@125
|
438 |
|
renatofilho@147
|
439 |
gmyth_socket_sendreceive_stringlist (socket, slist);
|
renatofilho@147
|
440 |
|
renatofilho@147
|
441 |
res = (gmyth_string_list_get_int (slist, 0) == 1);
|
melunko@125
|
442 |
|
renatofilho@147
|
443 |
// fixme: we should do this in a program_info_free() function
|
renatofilho@147
|
444 |
g_string_free (program->pathname, TRUE);
|
renatofilho@147
|
445 |
g_free (program);
|
melunko@125
|
446 |
|
renatofilho@147
|
447 |
g_object_unref (slist);
|
melunko@125
|
448 |
|
renatofilho@147
|
449 |
gmyth_socket_close_connection (socket);
|
renatofilho@147
|
450 |
}
|
renatofilho@147
|
451 |
g_object_unref (socket);
|
melunko@125
|
452 |
return res;
|
melunko@125
|
453 |
}
|
melunko@125
|
454 |
|
renatofilho@222
|
455 |
|
renatofilho@222
|
456 |
#if !GLIB_CHECK_VERSION (2, 10, 0)
|
renatofilho@222
|
457 |
|
renatofilho@222
|
458 |
|
renatofilho@222
|
459 |
/* Hacked from glib 2.10 <gtime.c> */
|
renatofilho@222
|
460 |
|
renatofilho@222
|
461 |
static time_t
|
renatofilho@222
|
462 |
mktime_utc (struct tm *tm)
|
renatofilho@222
|
463 |
{
|
renatofilho@222
|
464 |
time_t retval;
|
renatofilho@222
|
465 |
|
renatofilho@222
|
466 |
#ifndef HAVE_TIMEGM
|
renatofilho@222
|
467 |
static const gint days_before[] =
|
renatofilho@222
|
468 |
{
|
renatofilho@222
|
469 |
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
|
renatofilho@222
|
470 |
};
|
renatofilho@222
|
471 |
#endif
|
renatofilho@222
|
472 |
|
renatofilho@222
|
473 |
#ifndef HAVE_TIMEGM
|
renatofilho@222
|
474 |
if (tm->tm_mon < 0 || tm->tm_mon > 11)
|
renatofilho@222
|
475 |
return (time_t) -1;
|
renatofilho@222
|
476 |
|
renatofilho@222
|
477 |
retval = (tm->tm_year - 70) * 365;
|
renatofilho@222
|
478 |
retval += (tm->tm_year - 68) / 4;
|
renatofilho@222
|
479 |
retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
|
renatofilho@222
|
480 |
|
renatofilho@222
|
481 |
if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
|
renatofilho@222
|
482 |
retval -= 1;
|
renatofilho@222
|
483 |
|
renatofilho@222
|
484 |
retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
|
renatofilho@222
|
485 |
#else
|
renatofilho@222
|
486 |
retval = timegm (tm);
|
renatofilho@222
|
487 |
#endif /* !HAVE_TIMEGM */
|
renatofilho@222
|
488 |
|
renatofilho@222
|
489 |
return retval;
|
renatofilho@222
|
490 |
}
|
renatofilho@222
|
491 |
|
renatofilho@222
|
492 |
gboolean
|
renatofilho@222
|
493 |
g_time_val_from_iso8601 (const gchar *iso_date,
|
renatofilho@222
|
494 |
GTimeVal *time_)
|
renatofilho@222
|
495 |
{
|
renatofilho@222
|
496 |
struct tm tm;
|
renatofilho@222
|
497 |
long val;
|
renatofilho@222
|
498 |
|
renatofilho@222
|
499 |
g_return_val_if_fail (iso_date != NULL, FALSE);
|
renatofilho@222
|
500 |
g_return_val_if_fail (time_ != NULL, FALSE);
|
renatofilho@222
|
501 |
|
renatofilho@222
|
502 |
val = strtoul (iso_date, (char **)&iso_date, 10);
|
renatofilho@222
|
503 |
if (*iso_date == '-')
|
renatofilho@222
|
504 |
{
|
renatofilho@222
|
505 |
/* YYYY-MM-DD */
|
renatofilho@222
|
506 |
tm.tm_year = val - 1900;
|
renatofilho@222
|
507 |
iso_date++;
|
renatofilho@222
|
508 |
tm.tm_mon = strtoul (iso_date, (char **)&iso_date, 10) - 1;
|
renatofilho@222
|
509 |
|
renatofilho@222
|
510 |
if (*iso_date++ != '-')
|
renatofilho@222
|
511 |
return FALSE;
|
renatofilho@222
|
512 |
|
renatofilho@222
|
513 |
tm.tm_mday = strtoul (iso_date, (char **)&iso_date, 10);
|
renatofilho@222
|
514 |
}
|
renatofilho@222
|
515 |
else
|
renatofilho@222
|
516 |
{
|
renatofilho@222
|
517 |
/* YYYYMMDD */
|
renatofilho@222
|
518 |
tm.tm_mday = val % 100;
|
renatofilho@222
|
519 |
tm.tm_mon = (val % 10000) / 100 - 1;
|
renatofilho@222
|
520 |
tm.tm_year = val / 10000 - 1900;
|
renatofilho@222
|
521 |
}
|
renatofilho@222
|
522 |
|
renatofilho@222
|
523 |
if (*iso_date++ != 'T')
|
renatofilho@222
|
524 |
return FALSE;
|
renatofilho@222
|
525 |
|
renatofilho@222
|
526 |
val = strtoul (iso_date, (char **)&iso_date, 10);
|
renatofilho@222
|
527 |
if (*iso_date == ':')
|
renatofilho@222
|
528 |
{
|
renatofilho@222
|
529 |
/* hh:mm:ss */
|
renatofilho@222
|
530 |
tm.tm_hour = val;
|
renatofilho@222
|
531 |
iso_date++;
|
renatofilho@222
|
532 |
tm.tm_min = strtoul (iso_date, (char **)&iso_date, 10);
|
renatofilho@222
|
533 |
|
renatofilho@222
|
534 |
if (*iso_date++ != ':')
|
renatofilho@222
|
535 |
return FALSE;
|
renatofilho@222
|
536 |
|
renatofilho@222
|
537 |
tm.tm_sec = strtoul (iso_date, (char **)&iso_date, 10);
|
renatofilho@222
|
538 |
}
|
renatofilho@222
|
539 |
else
|
renatofilho@222
|
540 |
{
|
renatofilho@222
|
541 |
/* hhmmss */
|
renatofilho@222
|
542 |
tm.tm_sec = val % 100;
|
renatofilho@222
|
543 |
tm.tm_min = (val % 10000) / 100;
|
renatofilho@222
|
544 |
tm.tm_hour = val / 10000;
|
renatofilho@222
|
545 |
}
|
renatofilho@222
|
546 |
|
renatofilho@222
|
547 |
time_->tv_sec = mktime_utc (&tm);
|
renatofilho@222
|
548 |
time_->tv_usec = 1;
|
renatofilho@222
|
549 |
|
renatofilho@222
|
550 |
if (*iso_date == '.')
|
renatofilho@222
|
551 |
time_->tv_usec = strtoul (iso_date + 1, (char **)&iso_date, 10);
|
renatofilho@222
|
552 |
|
renatofilho@222
|
553 |
if (*iso_date == '+' || *iso_date == '-')
|
renatofilho@222
|
554 |
{
|
renatofilho@222
|
555 |
gint sign = (*iso_date == '+') ? -1 : 1;
|
renatofilho@222
|
556 |
|
renatofilho@222
|
557 |
val = 60 * strtoul (iso_date + 1, (char **)&iso_date, 10);
|
renatofilho@222
|
558 |
|
renatofilho@222
|
559 |
if (*iso_date == ':')
|
renatofilho@222
|
560 |
val = 60 * val + strtoul (iso_date + 1, NULL, 10);
|
renatofilho@222
|
561 |
else
|
renatofilho@222
|
562 |
val = 60 * (val / 100) + (val % 100);
|
renatofilho@222
|
563 |
|
renatofilho@222
|
564 |
time_->tv_sec += (time_t) (val * sign);
|
renatofilho@222
|
565 |
}
|
renatofilho@222
|
566 |
|
renatofilho@222
|
567 |
return TRUE;
|
renatofilho@222
|
568 |
}
|
renatofilho@222
|
569 |
|
renatofilho@222
|
570 |
|
renatofilho@222
|
571 |
gchar *
|
renatofilho@222
|
572 |
g_time_val_to_iso8601 (GTimeVal *time_)
|
renatofilho@222
|
573 |
{
|
renatofilho@222
|
574 |
gchar *retval;
|
renatofilho@222
|
575 |
|
renatofilho@222
|
576 |
g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
|
renatofilho@222
|
577 |
|
renatofilho@222
|
578 |
#define ISO_8601_LEN 21
|
renatofilho@222
|
579 |
#define ISO_8601_FORMAT "%Y-%m-%dT%H:%M:%SZ"
|
renatofilho@222
|
580 |
retval = g_new0 (gchar, ISO_8601_LEN + 1);
|
renatofilho@222
|
581 |
|
renatofilho@222
|
582 |
strftime (retval, ISO_8601_LEN,
|
renatofilho@222
|
583 |
ISO_8601_FORMAT,
|
renatofilho@222
|
584 |
gmtime (&(time_->tv_sec)));
|
renatofilho@222
|
585 |
|
renatofilho@222
|
586 |
return retval;
|
renatofilho@222
|
587 |
}
|
renatofilho@222
|
588 |
|
renatofilho@222
|
589 |
|
renatofilho@222
|
590 |
/* Hacked from glib 2.10 <gdate.c> */
|
renatofilho@222
|
591 |
|
renatofilho@222
|
592 |
void
|
renatofilho@222
|
593 |
g_date_set_time_t (GDate *date,
|
renatofilho@222
|
594 |
time_t timet)
|
renatofilho@222
|
595 |
{
|
renatofilho@222
|
596 |
struct tm tm;
|
renatofilho@222
|
597 |
|
renatofilho@222
|
598 |
g_return_if_fail (date != NULL);
|
renatofilho@222
|
599 |
|
renatofilho@222
|
600 |
#ifdef HAVE_LOCALTIME_R
|
renatofilho@222
|
601 |
localtime_r (&timet, &tm);
|
renatofilho@222
|
602 |
#else
|
renatofilho@222
|
603 |
{
|
renatofilho@222
|
604 |
struct tm *ptm = localtime (&timet);
|
renatofilho@222
|
605 |
|
renatofilho@222
|
606 |
if (ptm == NULL)
|
renatofilho@222
|
607 |
{
|
renatofilho@222
|
608 |
/* Happens at least in Microsoft's C library if you pass a
|
renatofilho@222
|
609 |
* negative time_t. Use 2000-01-01 as default date.
|
renatofilho@222
|
610 |
*/
|
renatofilho@222
|
611 |
#ifndef G_DISABLE_CHECKS
|
renatofilho@222
|
612 |
g_return_if_fail_warning (G_LOG_DOMAIN, "g_date_set_time", "ptm != NULL");
|
renatofilho@222
|
613 |
#endif
|
renatofilho@222
|
614 |
|
renatofilho@222
|
615 |
tm.tm_mon = 0;
|
renatofilho@222
|
616 |
tm.tm_mday = 1;
|
renatofilho@222
|
617 |
tm.tm_year = 100;
|
renatofilho@222
|
618 |
}
|
renatofilho@222
|
619 |
else
|
renatofilho@222
|
620 |
memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
|
renatofilho@222
|
621 |
}
|
renatofilho@222
|
622 |
#endif
|
renatofilho@222
|
623 |
|
renatofilho@222
|
624 |
date->julian = FALSE;
|
renatofilho@222
|
625 |
|
renatofilho@222
|
626 |
date->month = tm.tm_mon + 1;
|
renatofilho@222
|
627 |
date->day = tm.tm_mday;
|
renatofilho@222
|
628 |
date->year = tm.tm_year + 1900;
|
renatofilho@222
|
629 |
|
renatofilho@222
|
630 |
g_return_if_fail (g_date_valid_dmy (date->day, date->month, date->year));
|
renatofilho@222
|
631 |
|
renatofilho@222
|
632 |
date->dmy = TRUE;
|
renatofilho@222
|
633 |
}
|
renatofilho@222
|
634 |
|
renatofilho@222
|
635 |
|
renatofilho@222
|
636 |
void
|
renatofilho@222
|
637 |
g_date_set_time_val (GDate *date,
|
renatofilho@222
|
638 |
GTimeVal *timeval)
|
renatofilho@222
|
639 |
{
|
renatofilho@222
|
640 |
g_date_set_time_t (date, (time_t) timeval->tv_sec);
|
renatofilho@222
|
641 |
}
|
renatofilho@222
|
642 |
|
renatofilho@222
|
643 |
|
renatofilho@222
|
644 |
|
renatofilho@222
|
645 |
|
renatofilho@222
|
646 |
#endif
|