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) {
|
melunko@313
|
76 |
g_static_mutex_unlock ( &mutex );
|
melunko@313
|
77 |
g_warning ("gmyth_util_time_to_isoformat convertion error!\n");
|
melunko@313
|
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 |
/** Converts a time_t struct in a GString at ISO standard format
|
rosfran@214
|
94 |
* (e.g. 2006-07-20T09:56:41).
|
rosfran@214
|
95 |
*
|
rosfran@214
|
96 |
* The returned GString memory should be deallocated from
|
rosfran@214
|
97 |
* the calling function.
|
rosfran@214
|
98 |
*
|
rosfran@214
|
99 |
* @param time_value the GTimeValue to be converted
|
rosfran@214
|
100 |
* @return GString* the converted isoformat string
|
rosfran@214
|
101 |
*/
|
rosfran@223
|
102 |
gchar*
|
rosfran@223
|
103 |
gmyth_util_time_to_isoformat_from_time_val_fmt ( const gchar *fmt_string, const GTimeVal* time_val )
|
rosfran@214
|
104 |
{
|
rosfran@221
|
105 |
gchar *result = NULL;
|
rosfran@221
|
106 |
struct tm *tm_time = NULL;
|
rosfran@214
|
107 |
|
rosfran@221
|
108 |
gint buffer_len = 0;
|
rosfran@214
|
109 |
|
rosfran@223
|
110 |
g_return_val_if_fail( fmt_string != NULL, NULL );
|
rosfran@223
|
111 |
|
rosfran@221
|
112 |
g_return_val_if_fail( time_val != NULL, NULL );
|
rosfran@221
|
113 |
|
rosfran@223
|
114 |
time_t time = time_val->tv_sec;// + (gint)( time_val->tv_usec / G_USEC_PER_SEC );
|
rosfran@214
|
115 |
|
rosfran@221
|
116 |
tm_time = g_malloc0( sizeof(struct tm) );
|
rosfran@221
|
117 |
|
rosfran@221
|
118 |
g_static_mutex_lock ( &mutex );
|
rosfran@221
|
119 |
|
rosfran@223
|
120 |
if ( NULL == localtime_r( &time, tm_time ) ) {
|
rosfran@221
|
121 |
g_warning ("gmyth_util_time_to_isoformat convertion error!\n");
|
rosfran@221
|
122 |
} else {
|
rosfran@221
|
123 |
// we first check the return of strftime to allocate a buffer of the correct size
|
rosfran@221
|
124 |
buffer_len = strftime( NULL, SSIZE_MAX, fmt_string, tm_time );
|
rosfran@223
|
125 |
if ( buffer_len > 0 ) {
|
rosfran@221
|
126 |
result = g_malloc0( buffer_len + 1 );
|
rosfran@223
|
127 |
if( result == NULL ){
|
rosfran@221
|
128 |
g_static_mutex_unlock ( &mutex );
|
rosfran@221
|
129 |
g_warning ("gmyth_util_time_to_isoformat convertion error!\n");
|
rosfran@221
|
130 |
return NULL;
|
rosfran@221
|
131 |
}
|
rosfran@221
|
132 |
strftime( result, buffer_len + 1, fmt_string, tm_time );
|
melunko@313
|
133 |
gmyth_debug( "Dateline (ISO result): %s", result );
|
rosfran@221
|
134 |
}
|
rosfran@221
|
135 |
|
rosfran@214
|
136 |
}
|
rosfran@214
|
137 |
|
rosfran@223
|
138 |
gmyth_debug( "Result (strftime) = %s", result );
|
rosfran@214
|
139 |
|
rosfran@221
|
140 |
//strptime( result, "%Y-%m-%dT%H:%M:%SZ", tm_time );
|
rosfran@214
|
141 |
|
rosfran@221
|
142 |
//strftime( result, strlen(result), fmt_string, tm_time );
|
rosfran@214
|
143 |
|
rosfran@214
|
144 |
g_static_mutex_unlock ( &mutex );
|
rosfran@214
|
145 |
|
rosfran@214
|
146 |
gmyth_debug( "Result (ISO 8601) = %s", result );
|
rosfran@214
|
147 |
|
rosfran@214
|
148 |
return result;
|
rosfran@214
|
149 |
|
rosfran@214
|
150 |
}
|
rosfran@214
|
151 |
|
rosfran@214
|
152 |
/** Converts a time_t struct in a GString at ISO standard format
|
morphbr@306
|
153 |
* (e.g. 2006-07-20 09:56:41).
|
rosfran@214
|
154 |
*
|
rosfran@214
|
155 |
* The returned GString memory should be deallocated from
|
rosfran@214
|
156 |
* the calling function.
|
rosfran@214
|
157 |
*
|
rosfran@214
|
158 |
* @param time_value the GTimeValue to be converted
|
rosfran@214
|
159 |
* @return GString* the converted isoformat string
|
rosfran@214
|
160 |
*/
|
rosfran@214
|
161 |
gchar*
|
rosfran@223
|
162 |
gmyth_util_time_to_isoformat_from_time_val ( const GTimeVal* time )
|
rosfran@214
|
163 |
{
|
rosfran@223
|
164 |
gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%Y-%m-%d %H:%M:%S", time );
|
rosfran@214
|
165 |
//result[10] = ' ';
|
rosfran@214
|
166 |
//result[ strlen(result) - 1] = '\0';
|
rosfran@214
|
167 |
|
rosfran@214
|
168 |
return result;
|
rosfran@214
|
169 |
}
|
rosfran@214
|
170 |
|
morphbr@306
|
171 |
/** Converts a time_t struct in a GString at ISO standard format 2
|
morphbr@306
|
172 |
* (e.g. 2006-07-20T09:56:41).
|
morphbr@306
|
173 |
*
|
morphbr@306
|
174 |
* The returned GString memory should be deallocated from
|
morphbr@306
|
175 |
* the calling function.
|
morphbr@306
|
176 |
*
|
morphbr@306
|
177 |
* @param time_value the GTimeValue to be converted
|
morphbr@306
|
178 |
* @return GString* the converted isoformat string
|
morphbr@306
|
179 |
*/
|
morphbr@306
|
180 |
gchar*
|
morphbr@306
|
181 |
gmyth_util_time_to_mythformat_from_time_val ( const GTimeVal* time )
|
morphbr@306
|
182 |
{
|
morphbr@306
|
183 |
gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%Y-%m-%dT%H:%M:%S", time );
|
morphbr@306
|
184 |
return result;
|
morphbr@306
|
185 |
}
|
morphbr@306
|
186 |
|
rosfran@214
|
187 |
/** Converts a time_t struct in a GString at ISO standard format
|
rosfran@214
|
188 |
* (e.g. 2006-07-20T09:56:41).
|
rosfran@214
|
189 |
*
|
rosfran@214
|
190 |
* The returned GString memory should be deallocated from
|
rosfran@214
|
191 |
* the calling function.
|
rosfran@214
|
192 |
*
|
rosfran@214
|
193 |
* @param time_value the GTimeValue to be converted
|
rosfran@214
|
194 |
* @return GString* the converted isoformat string
|
rosfran@214
|
195 |
*/
|
rosfran@214
|
196 |
gchar*
|
rosfran@223
|
197 |
gmyth_util_time_to_string_only_date ( const GTimeVal* time )
|
rosfran@214
|
198 |
{
|
rosfran@223
|
199 |
gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%Y-%m-%d", time );
|
rosfran@223
|
200 |
//result[10] = ' ';
|
rosfran@223
|
201 |
//result[ strlen(result) - 1] = '\0';
|
rosfran@214
|
202 |
return result;
|
rosfran@214
|
203 |
}
|
rosfran@214
|
204 |
|
rosfran@214
|
205 |
/** Converts a time_t struct in a GString at ISO standard format
|
rosfran@214
|
206 |
* (e.g. 2006-07-20T09:56:41).
|
rosfran@214
|
207 |
*
|
rosfran@214
|
208 |
* The returned GString memory should be deallocated from
|
rosfran@214
|
209 |
* the calling function.
|
rosfran@214
|
210 |
*
|
rosfran@214
|
211 |
* @param time_value the GTimeValue to be converted
|
rosfran@214
|
212 |
* @return GString* the converted isoformat string
|
rosfran@214
|
213 |
*/
|
rosfran@214
|
214 |
gchar*
|
rosfran@223
|
215 |
gmyth_util_time_to_string_only_time ( const GTimeVal* time )
|
rosfran@214
|
216 |
{
|
rosfran@223
|
217 |
gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%H:%M:%S", time );
|
rosfran@223
|
218 |
//result[10] = ' ';
|
rosfran@223
|
219 |
//result[ strlen(result) - 1] = '\0';
|
rosfran@214
|
220 |
return result;
|
leo_sobral@1
|
221 |
}
|
leo_sobral@1
|
222 |
|
leo_sobral@1
|
223 |
/** Converts a time_t struct in a GString to the following
|
leo_sobral@1
|
224 |
* format (e.g. 2006-07-20 09:56:41).
|
leo_sobral@1
|
225 |
*
|
leo_sobral@1
|
226 |
* The returned GString memory should be deallocated from
|
leo_sobral@1
|
227 |
* the calling function.
|
leo_sobral@1
|
228 |
*
|
leo_sobral@1
|
229 |
* @param time_value the time value to be converted
|
leo_sobral@1
|
230 |
* @return GString* the converted string
|
leo_sobral@1
|
231 |
*/
|
leo_sobral@1
|
232 |
GString*
|
leo_sobral@1
|
233 |
gmyth_util_time_to_string (time_t time_value)
|
leo_sobral@1
|
234 |
{
|
leo_sobral@1
|
235 |
GString *result = gmyth_util_time_to_isoformat (time_value);
|
leo_sobral@1
|
236 |
result->str[10] = ' ';
|
rosfran@223
|
237 |
result->str[ strlen(result->str) - 1] = '\0';
|
leo_sobral@1
|
238 |
|
leo_sobral@1
|
239 |
return result;
|
leo_sobral@1
|
240 |
}
|
leo_sobral@1
|
241 |
|
rosfran@214
|
242 |
/** Converts a time_t struct in a GString to the following
|
rosfran@214
|
243 |
* format (e.g. 2006-07-20 09:56:41).
|
rosfran@214
|
244 |
*
|
rosfran@214
|
245 |
* The returned GString memory should be deallocated from
|
rosfran@214
|
246 |
* the calling function.
|
rosfran@214
|
247 |
*
|
rosfran@214
|
248 |
* @param time_value the time value to be converted
|
rosfran@214
|
249 |
* @return GString* the converted string
|
rosfran@214
|
250 |
*/
|
rosfran@214
|
251 |
gchar*
|
rosfran@223
|
252 |
gmyth_util_time_to_string_from_time_val ( const GTimeVal *time_val )
|
rosfran@214
|
253 |
{
|
rosfran@223
|
254 |
gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt ( "%Y-%m-%d %H:%M:%S", time_val );
|
rosfran@223
|
255 |
//result[10] = ' ';
|
rosfran@214
|
256 |
|
rosfran@214
|
257 |
return result;
|
rosfran@214
|
258 |
}
|
rosfran@214
|
259 |
|
leo_sobral@1
|
260 |
/** Converts a GString in the following format
|
leo_sobral@1
|
261 |
* (e.g. 2006-07-20 09:56:41) to a time_t struct.
|
leo_sobral@1
|
262 |
*
|
leo_sobral@1
|
263 |
* @param time_str the string to be converted
|
leo_sobral@1
|
264 |
* @return time_t the time converted value
|
leo_sobral@1
|
265 |
*/
|
leo_sobral@1
|
266 |
time_t
|
leo_sobral@1
|
267 |
gmyth_util_string_to_time (GString* time_str)
|
leo_sobral@1
|
268 |
{
|
rosfran@223
|
269 |
gint year, month, day, hour, min, sec;
|
leo_sobral@49
|
270 |
|
melunko@313
|
271 |
gmyth_debug( "[%s] time_str = %s. [%s]", __FUNCTION__, time_str != NULL ?
|
rosfran@223
|
272 |
time_str->str : "[time string is NULL!]", time_str->str );
|
leo_sobral@1
|
273 |
|
rosfran@223
|
274 |
if ( sscanf (time_str->str, "%04d-%02d-%02d %02d:%02d:%02d",
|
rosfran@223
|
275 |
&year, &month, &day, &hour, &min, &sec) < 3 ) {
|
leo_sobral@1
|
276 |
g_warning ("GMythUtil: isoformat_to_time converter error!\n");
|
leo_sobral@1
|
277 |
return 0;
|
rosfran@214
|
278 |
}
|
rosfran@214
|
279 |
|
rosfran@214
|
280 |
g_static_mutex_lock ( &mutex );
|
rosfran@214
|
281 |
|
rosfran@214
|
282 |
struct tm* tm_time = g_malloc0( sizeof(struct tm) );
|
rosfran@223
|
283 |
tm_time->tm_year = year - 1900;
|
rosfran@223
|
284 |
tm_time->tm_mon = month - 1;
|
rosfran@223
|
285 |
tm_time->tm_mday = day;
|
rosfran@223
|
286 |
tm_time->tm_hour = hour;
|
rosfran@223
|
287 |
tm_time->tm_min = min;
|
rosfran@223
|
288 |
tm_time->tm_sec = sec;
|
rosfran@214
|
289 |
|
rosfran@214
|
290 |
g_static_mutex_unlock ( &mutex );
|
rosfran@214
|
291 |
|
rosfran@214
|
292 |
return mktime( tm_time );
|
rosfran@214
|
293 |
}
|
rosfran@214
|
294 |
|
rosfran@214
|
295 |
/** Converts a GString in the following format
|
rosfran@214
|
296 |
* (e.g. 2006-07-20 09:56:41) to a time_t struct.
|
rosfran@214
|
297 |
*
|
rosfran@214
|
298 |
* @param time_str the string to be converted
|
rosfran@214
|
299 |
* @return time_t the time converted value
|
rosfran@214
|
300 |
*/
|
rosfran@223
|
301 |
struct tm*
|
rosfran@223
|
302 |
gmyth_util_time_val_to_date ( const GTimeVal* time )
|
rosfran@214
|
303 |
{
|
rosfran@223
|
304 |
struct tm *date = g_malloc0( sizeof( struct tm ) );
|
rosfran@223
|
305 |
time_t time_micros = time->tv_sec;// + (gint)( time->tv_usec / G_USEC_PER_SEC );
|
rosfran@214
|
306 |
|
rosfran@214
|
307 |
if ( NULL == date ) {
|
rosfran@223
|
308 |
g_warning ( "GMythUtil: GDate *gmyth_util_time_val_to_date (GTimeVal* time) - converter error!\n" );
|
rosfran@223
|
309 |
return NULL;
|
leo_sobral@1
|
310 |
}
|
rosfran@214
|
311 |
|
rosfran@223
|
312 |
if ( NULL == localtime_r( &time_micros, date ) ) {
|
rosfran@223
|
313 |
g_warning ( "gmyth_util_time_to_isoformat convertion error!\n" );
|
rosfran@223
|
314 |
return NULL;
|
rosfran@223
|
315 |
}
|
rosfran@223
|
316 |
|
melunko@313
|
317 |
gmyth_debug( "Converted from GTimeVal == %s to GDate", asctime( date ) );
|
rosfran@214
|
318 |
|
rosfran@214
|
319 |
return date;
|
rosfran@214
|
320 |
}
|
rosfran@214
|
321 |
|
rosfran@214
|
322 |
/** Converts a GString in the following format
|
rosfran@214
|
323 |
* (e.g. 2006-07-20 09:56:41) to a time_t struct.
|
rosfran@214
|
324 |
*
|
rosfran@214
|
325 |
* @param time_str the string to be converted
|
rosfran@214
|
326 |
* @return time_t the time converted value
|
rosfran@214
|
327 |
*/
|
rosfran@214
|
328 |
GTimeVal*
|
rosfran@223
|
329 |
gmyth_util_string_to_time_val_fmt ( const gchar *fmt_string, const gchar* time_str )
|
rosfran@214
|
330 |
{
|
rosfran@223
|
331 |
GTimeVal *time = g_new0( GTimeVal, 1 );
|
rosfran@223
|
332 |
struct tm* tm_time = NULL;
|
rosfran@223
|
333 |
time_t time_micros;
|
rosfran@337
|
334 |
gchar* result;
|
morphbr@349
|
335 |
|
melunko@313
|
336 |
gmyth_debug( "[%s] time_str = %s. [%s]", time_str, time_str != NULL ?
|
morphbr@349
|
337 |
time_str : "[time string is NULL!]", time_str );
|
morphbr@349
|
338 |
|
morphbr@305
|
339 |
if ( NULL == time_str )
|
morphbr@349
|
340 |
{
|
rosfran@214
|
341 |
g_warning ("GMythUtil: isoformat_to_time converter error!\n");
|
morphbr@305
|
342 |
return NULL;
|
morphbr@349
|
343 |
}
|
rosfran@214
|
344 |
|
rosfran@214
|
345 |
g_static_mutex_lock ( &mutex );
|
rosfran@214
|
346 |
|
rosfran@223
|
347 |
tm_time = g_malloc0( sizeof(struct tm) );
|
morphbr@349
|
348 |
|
melunko@313
|
349 |
/* we first check the return of strftime to allocate a buffer of the correct size */
|
morphbr@349
|
350 |
result = strptime( time_str, "%Y-%m-%dT%H:%M:%S", tm_time );
|
morphbr@349
|
351 |
if ( NULL == result ) {
|
morphbr@349
|
352 |
/* we first check the return of strftime to allocate a buffer of the correct size */
|
melunko@313
|
353 |
result = strptime( time_str, "%Y-%m-%dT%H:%M:%SZ", tm_time );
|
melunko@313
|
354 |
if ( NULL == result ) {
|
melunko@313
|
355 |
/* we first check the return of strftime to allocate a buffer of the correct size */
|
morphbr@349
|
356 |
result = strptime( time_str, "%Y-%m-%d %H:%M:%S", tm_time );
|
morphbr@349
|
357 |
if ( NULL == result) {
|
morphbr@349
|
358 |
result = strptime( time_str, "%Y-%m-%dT%H:%M", tm_time );
|
morphbr@349
|
359 |
if ( NULL == result ) {
|
morphbr@349
|
360 |
g_static_mutex_unlock ( &mutex );
|
morphbr@349
|
361 |
gmyth_debug( "Dateline (ISO result): %s", result );
|
morphbr@349
|
362 |
g_free(tm_time);
|
morphbr@349
|
363 |
return NULL;
|
morphbr@349
|
364 |
//goto done;
|
morphbr@349
|
365 |
}
|
morphbr@349
|
366 |
}
|
melunko@313
|
367 |
}
|
morphbr@349
|
368 |
}
|
morphbr@349
|
369 |
|
morphbr@349
|
370 |
time_micros = mktime( tm_time );
|
morphbr@349
|
371 |
|
morphbr@349
|
372 |
time->tv_sec = time_micros; // + (gint)( time_val->tv_usec / G_USEC_PER_SEC );
|
morphbr@349
|
373 |
|
morphbr@349
|
374 |
gmyth_debug( "After mktime call... = %s", asctime(tm_time) );
|
morphbr@349
|
375 |
|
morphbr@349
|
376 |
g_static_mutex_unlock ( &mutex );
|
morphbr@349
|
377 |
|
morphbr@349
|
378 |
return time;
|
rosfran@223
|
379 |
}
|
rosfran@223
|
380 |
|
rosfran@223
|
381 |
/** Converts a GString in the following format
|
rosfran@223
|
382 |
* (e.g. 2006-07-20 09:56:41) to a time_t struct.
|
rosfran@223
|
383 |
*
|
rosfran@223
|
384 |
* @param time_str the string to be converted
|
rosfran@223
|
385 |
* @return time_t the time converted value
|
rosfran@223
|
386 |
*/
|
rosfran@223
|
387 |
GTimeVal*
|
rosfran@223
|
388 |
gmyth_util_string_to_time_val ( const gchar* time_str )
|
rosfran@223
|
389 |
{
|
rosfran@223
|
390 |
GTimeVal *time = gmyth_util_string_to_time_val_fmt ( "%Y-%m-%d %H:%M:%S", time_str );
|
rosfran@214
|
391 |
|
rosfran@214
|
392 |
return time;
|
leo_sobral@1
|
393 |
}
|
leo_sobral@1
|
394 |
|
leo_sobral@1
|
395 |
/** Decodes a long long variable from the string list
|
leo_sobral@1
|
396 |
* format of the myhtprotocol.
|
leo_sobral@1
|
397 |
*
|
leo_sobral@1
|
398 |
* @param strlist the string list of mythprotocol values
|
leo_sobral@1
|
399 |
* @param offset the list node offset of the long long variable
|
rosfran@35
|
400 |
* @return gint64 the long long converted value
|
leo_sobral@1
|
401 |
*/
|
rosfran@35
|
402 |
gint64
|
leo_sobral@1
|
403 |
gmyth_util_decode_long_long(GMythStringList *strlist, guint offset)
|
leo_sobral@1
|
404 |
{
|
leo_sobral@1
|
405 |
|
rosfran@35
|
406 |
gint64 ret_value = 0LL;
|
leo_sobral@1
|
407 |
|
leo_sobral@1
|
408 |
g_return_val_if_fail( strlist != NULL, ret_value );
|
leo_sobral@1
|
409 |
|
rosfran@35
|
410 |
if ( offset > gmyth_string_list_length( strlist ))
|
rosfran@35
|
411 |
g_printerr( "[%s] Offset is greater than the Stringlist (offset = %d)!\n",
|
leo_sobral@1
|
412 |
__FUNCTION__, offset );
|
leo_sobral@1
|
413 |
|
leo_sobral@1
|
414 |
g_return_val_if_fail( offset < gmyth_string_list_length( strlist ), ret_value );
|
leo_sobral@1
|
415 |
|
leo_sobral@1
|
416 |
gint l1 = gmyth_string_list_get_int( strlist, offset );
|
leo_sobral@1
|
417 |
gint l2 = gmyth_string_list_get_int( strlist, offset + 1 );
|
leo_sobral@1
|
418 |
|
rosfran@35
|
419 |
ret_value = (l2 /*& 0xffffffffLL*/) | ( (gint64)l1 << 32 );
|
leo_sobral@1
|
420 |
|
leo_sobral@1
|
421 |
return ret_value;
|
leo_sobral@1
|
422 |
|
leo_sobral@1
|
423 |
}
|
leo_sobral@1
|
424 |
|
melunko@125
|
425 |
gboolean
|
rosfran@214
|
426 |
gmyth_util_file_exists (GMythBackendInfo *backend_info, const gchar* filename)
|
melunko@125
|
427 |
{
|
melunko@125
|
428 |
GMythSocket *socket;
|
melunko@125
|
429 |
gboolean res;
|
melunko@125
|
430 |
|
melunko@276
|
431 |
socket = gmyth_socket_new ();
|
renatofilho@147
|
432 |
res = gmyth_socket_connect_to_backend (socket, backend_info->hostname,
|
melunko@125
|
433 |
backend_info->port, TRUE);
|
rosfran@35
|
434 |
|
renatofilho@147
|
435 |
if (res == TRUE) {
|
renatofilho@147
|
436 |
GMythStringList *slist;
|
rosfran@298
|
437 |
GMythProgramInfo *program = NULL;
|
melunko@125
|
438 |
|
rosfran@291
|
439 |
program = gmyth_program_info_new();
|
renatofilho@147
|
440 |
program->pathname = g_string_new (filename);
|
melunko@125
|
441 |
|
renatofilho@147
|
442 |
slist = gmyth_string_list_new ();
|
renatofilho@147
|
443 |
gmyth_string_list_append_char_array (slist, "QUERY_CHECKFILE");
|
melunko@125
|
444 |
|
renatofilho@147
|
445 |
gmyth_program_info_to_string_list (program, slist);
|
melunko@125
|
446 |
|
renatofilho@147
|
447 |
gmyth_socket_sendreceive_stringlist (socket, slist);
|
renatofilho@147
|
448 |
|
renatofilho@147
|
449 |
res = (gmyth_string_list_get_int (slist, 0) == 1);
|
melunko@125
|
450 |
|
rosfran@298
|
451 |
g_object_unref (program);
|
melunko@125
|
452 |
|
renatofilho@147
|
453 |
g_object_unref (slist);
|
melunko@125
|
454 |
|
renatofilho@147
|
455 |
gmyth_socket_close_connection (socket);
|
renatofilho@147
|
456 |
}
|
renatofilho@147
|
457 |
g_object_unref (socket);
|
melunko@125
|
458 |
return res;
|
melunko@125
|
459 |
}
|
melunko@125
|
460 |
|
rosfran@384
|
461 |
gboolean
|
rosfran@384
|
462 |
gmyth_util_file_exists_and_get_remote_dir (GMythBackendInfo *backend_info,
|
rosfran@384
|
463 |
const gchar* filename, gchar** current_dir)
|
rosfran@384
|
464 |
{
|
rosfran@384
|
465 |
GMythSocket *socket;
|
rosfran@384
|
466 |
gboolean res;
|
rosfran@384
|
467 |
|
rosfran@384
|
468 |
*current_dir = NULL;
|
rosfran@384
|
469 |
|
rosfran@384
|
470 |
socket = gmyth_socket_new ();
|
rosfran@384
|
471 |
res = gmyth_socket_connect_to_backend (socket, backend_info->hostname,
|
rosfran@384
|
472 |
backend_info->port, TRUE);
|
rosfran@384
|
473 |
|
rosfran@384
|
474 |
if (res == TRUE) {
|
rosfran@384
|
475 |
GMythStringList *slist;
|
rosfran@384
|
476 |
GMythProgramInfo *program = NULL;
|
rosfran@384
|
477 |
|
rosfran@384
|
478 |
program = gmyth_program_info_new();
|
rosfran@384
|
479 |
program->pathname = g_string_new (filename);
|
rosfran@384
|
480 |
|
rosfran@384
|
481 |
slist = gmyth_string_list_new ();
|
rosfran@384
|
482 |
gmyth_string_list_append_char_array (slist, "QUERY_CHECKFILE");
|
rosfran@384
|
483 |
|
rosfran@384
|
484 |
gmyth_program_info_to_string_list (program, slist);
|
rosfran@384
|
485 |
|
rosfran@384
|
486 |
gmyth_socket_sendreceive_stringlist (socket, slist);
|
rosfran@384
|
487 |
|
rosfran@384
|
488 |
res = (gmyth_string_list_get_int (slist, 0) == 1);
|
rosfran@384
|
489 |
|
rosfran@384
|
490 |
if ( ( gmyth_string_list_length( slist ) > 1 ) &&
|
rosfran@384
|
491 |
gmyth_string_list_get_char_array (slist, 1) != NULL )
|
rosfran@384
|
492 |
*current_dir = g_strdup( gmyth_string_list_get_char_array (slist, 1) );
|
rosfran@384
|
493 |
|
rosfran@384
|
494 |
g_object_unref (program);
|
rosfran@384
|
495 |
|
rosfran@384
|
496 |
g_object_unref (slist);
|
rosfran@384
|
497 |
|
rosfran@384
|
498 |
gmyth_socket_close_connection (socket);
|
rosfran@384
|
499 |
}
|
rosfran@384
|
500 |
g_object_unref (socket);
|
rosfran@384
|
501 |
return res;
|
rosfran@384
|
502 |
}
|
rosfran@384
|
503 |
|
rosfran@384
|
504 |
gchar*
|
rosfran@384
|
505 |
gmyth_util_create_filename( const gint chan_id, const GTimeVal* start_time )
|
rosfran@384
|
506 |
{
|
rosfran@384
|
507 |
gchar* basename = NULL;
|
rosfran@384
|
508 |
|
rosfran@384
|
509 |
g_return_val_if_fail( start_time != NULL, NULL );
|
rosfran@384
|
510 |
|
rosfran@384
|
511 |
gchar *isodate = gmyth_util_time_to_isoformat_from_time_val_fmt ( "%Y%m%d%H%M%S",
|
rosfran@384
|
512 |
start_time );
|
rosfran@384
|
513 |
|
rosfran@384
|
514 |
basename = g_strdup_printf( "%d_%s", chan_id, isodate );
|
rosfran@384
|
515 |
|
rosfran@384
|
516 |
gmyth_debug ("Basename (from chan_id and start_time): %s", basename);
|
rosfran@384
|
517 |
|
rosfran@384
|
518 |
if (isodate)
|
rosfran@384
|
519 |
g_free(isodate);
|
rosfran@384
|
520 |
|
rosfran@384
|
521 |
return basename;
|
rosfran@384
|
522 |
}
|
renatofilho@222
|
523 |
|
renatofilho@222
|
524 |
#if !GLIB_CHECK_VERSION (2, 10, 0)
|
renatofilho@222
|
525 |
|
renatofilho@222
|
526 |
/* Hacked from glib 2.10 <gtime.c> */
|
renatofilho@222
|
527 |
|
renatofilho@222
|
528 |
static time_t
|
renatofilho@222
|
529 |
mktime_utc (struct tm *tm)
|
renatofilho@222
|
530 |
{
|
renatofilho@222
|
531 |
time_t retval;
|
renatofilho@222
|
532 |
|
renatofilho@222
|
533 |
#ifndef HAVE_TIMEGM
|
renatofilho@222
|
534 |
static const gint days_before[] =
|
renatofilho@222
|
535 |
{
|
renatofilho@222
|
536 |
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
|
renatofilho@222
|
537 |
};
|
renatofilho@222
|
538 |
#endif
|
renatofilho@222
|
539 |
|
renatofilho@222
|
540 |
#ifndef HAVE_TIMEGM
|
renatofilho@222
|
541 |
if (tm->tm_mon < 0 || tm->tm_mon > 11)
|
renatofilho@222
|
542 |
return (time_t) -1;
|
renatofilho@222
|
543 |
|
renatofilho@222
|
544 |
retval = (tm->tm_year - 70) * 365;
|
renatofilho@222
|
545 |
retval += (tm->tm_year - 68) / 4;
|
renatofilho@222
|
546 |
retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
|
renatofilho@222
|
547 |
|
renatofilho@222
|
548 |
if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
|
renatofilho@222
|
549 |
retval -= 1;
|
renatofilho@222
|
550 |
|
renatofilho@222
|
551 |
retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
|
renatofilho@222
|
552 |
#else
|
renatofilho@222
|
553 |
retval = timegm (tm);
|
renatofilho@222
|
554 |
#endif /* !HAVE_TIMEGM */
|
renatofilho@222
|
555 |
|
renatofilho@222
|
556 |
return retval;
|
renatofilho@222
|
557 |
}
|
renatofilho@222
|
558 |
|
renatofilho@222
|
559 |
gboolean
|
renatofilho@222
|
560 |
g_time_val_from_iso8601 (const gchar *iso_date,
|
renatofilho@222
|
561 |
GTimeVal *time_)
|
renatofilho@222
|
562 |
{
|
renatofilho@222
|
563 |
struct tm tm;
|
renatofilho@222
|
564 |
long val;
|
renatofilho@222
|
565 |
|
renatofilho@222
|
566 |
g_return_val_if_fail (iso_date != NULL, FALSE);
|
renatofilho@222
|
567 |
g_return_val_if_fail (time_ != NULL, FALSE);
|
renatofilho@222
|
568 |
|
renatofilho@222
|
569 |
val = strtoul (iso_date, (char **)&iso_date, 10);
|
renatofilho@222
|
570 |
if (*iso_date == '-')
|
renatofilho@222
|
571 |
{
|
renatofilho@222
|
572 |
/* YYYY-MM-DD */
|
renatofilho@222
|
573 |
tm.tm_year = val - 1900;
|
renatofilho@222
|
574 |
iso_date++;
|
renatofilho@222
|
575 |
tm.tm_mon = strtoul (iso_date, (char **)&iso_date, 10) - 1;
|
renatofilho@222
|
576 |
|
renatofilho@222
|
577 |
if (*iso_date++ != '-')
|
renatofilho@222
|
578 |
return FALSE;
|
renatofilho@222
|
579 |
|
renatofilho@222
|
580 |
tm.tm_mday = strtoul (iso_date, (char **)&iso_date, 10);
|
renatofilho@222
|
581 |
}
|
renatofilho@222
|
582 |
else
|
renatofilho@222
|
583 |
{
|
renatofilho@222
|
584 |
/* YYYYMMDD */
|
renatofilho@222
|
585 |
tm.tm_mday = val % 100;
|
renatofilho@222
|
586 |
tm.tm_mon = (val % 10000) / 100 - 1;
|
renatofilho@222
|
587 |
tm.tm_year = val / 10000 - 1900;
|
renatofilho@222
|
588 |
}
|
renatofilho@222
|
589 |
|
renatofilho@222
|
590 |
if (*iso_date++ != 'T')
|
renatofilho@222
|
591 |
return FALSE;
|
renatofilho@222
|
592 |
|
renatofilho@222
|
593 |
val = strtoul (iso_date, (char **)&iso_date, 10);
|
renatofilho@222
|
594 |
if (*iso_date == ':')
|
renatofilho@222
|
595 |
{
|
renatofilho@222
|
596 |
/* hh:mm:ss */
|
renatofilho@222
|
597 |
tm.tm_hour = val;
|
renatofilho@222
|
598 |
iso_date++;
|
renatofilho@222
|
599 |
tm.tm_min = strtoul (iso_date, (char **)&iso_date, 10);
|
renatofilho@222
|
600 |
|
renatofilho@222
|
601 |
if (*iso_date++ != ':')
|
renatofilho@222
|
602 |
return FALSE;
|
renatofilho@222
|
603 |
|
renatofilho@222
|
604 |
tm.tm_sec = strtoul (iso_date, (char **)&iso_date, 10);
|
renatofilho@222
|
605 |
}
|
renatofilho@222
|
606 |
else
|
renatofilho@222
|
607 |
{
|
renatofilho@222
|
608 |
/* hhmmss */
|
renatofilho@222
|
609 |
tm.tm_sec = val % 100;
|
renatofilho@222
|
610 |
tm.tm_min = (val % 10000) / 100;
|
renatofilho@222
|
611 |
tm.tm_hour = val / 10000;
|
renatofilho@222
|
612 |
}
|
renatofilho@222
|
613 |
|
renatofilho@222
|
614 |
time_->tv_sec = mktime_utc (&tm);
|
renatofilho@222
|
615 |
time_->tv_usec = 1;
|
renatofilho@222
|
616 |
|
renatofilho@222
|
617 |
if (*iso_date == '.')
|
renatofilho@222
|
618 |
time_->tv_usec = strtoul (iso_date + 1, (char **)&iso_date, 10);
|
renatofilho@222
|
619 |
|
renatofilho@222
|
620 |
if (*iso_date == '+' || *iso_date == '-')
|
renatofilho@222
|
621 |
{
|
renatofilho@222
|
622 |
gint sign = (*iso_date == '+') ? -1 : 1;
|
renatofilho@222
|
623 |
|
renatofilho@222
|
624 |
val = 60 * strtoul (iso_date + 1, (char **)&iso_date, 10);
|
renatofilho@222
|
625 |
|
renatofilho@222
|
626 |
if (*iso_date == ':')
|
renatofilho@222
|
627 |
val = 60 * val + strtoul (iso_date + 1, NULL, 10);
|
renatofilho@222
|
628 |
else
|
renatofilho@222
|
629 |
val = 60 * (val / 100) + (val % 100);
|
renatofilho@222
|
630 |
|
renatofilho@222
|
631 |
time_->tv_sec += (time_t) (val * sign);
|
renatofilho@222
|
632 |
}
|
renatofilho@222
|
633 |
|
renatofilho@222
|
634 |
return TRUE;
|
renatofilho@222
|
635 |
}
|
renatofilho@222
|
636 |
|
renatofilho@222
|
637 |
|
renatofilho@222
|
638 |
gchar *
|
renatofilho@222
|
639 |
g_time_val_to_iso8601 (GTimeVal *time_)
|
renatofilho@222
|
640 |
{
|
renatofilho@222
|
641 |
gchar *retval;
|
renatofilho@222
|
642 |
|
renatofilho@222
|
643 |
g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
|
renatofilho@222
|
644 |
|
renatofilho@222
|
645 |
#define ISO_8601_LEN 21
|
renatofilho@222
|
646 |
#define ISO_8601_FORMAT "%Y-%m-%dT%H:%M:%SZ"
|
renatofilho@222
|
647 |
retval = g_new0 (gchar, ISO_8601_LEN + 1);
|
renatofilho@222
|
648 |
|
renatofilho@222
|
649 |
strftime (retval, ISO_8601_LEN,
|
renatofilho@222
|
650 |
ISO_8601_FORMAT,
|
renatofilho@222
|
651 |
gmtime (&(time_->tv_sec)));
|
renatofilho@222
|
652 |
|
renatofilho@222
|
653 |
return retval;
|
renatofilho@222
|
654 |
}
|
renatofilho@222
|
655 |
|
renatofilho@222
|
656 |
|
renatofilho@222
|
657 |
/* Hacked from glib 2.10 <gdate.c> */
|
renatofilho@222
|
658 |
|
renatofilho@222
|
659 |
void
|
renatofilho@222
|
660 |
g_date_set_time_t (GDate *date,
|
renatofilho@222
|
661 |
time_t timet)
|
renatofilho@222
|
662 |
{
|
renatofilho@222
|
663 |
struct tm tm;
|
renatofilho@222
|
664 |
|
renatofilho@222
|
665 |
g_return_if_fail (date != NULL);
|
renatofilho@222
|
666 |
|
renatofilho@222
|
667 |
#ifdef HAVE_LOCALTIME_R
|
renatofilho@222
|
668 |
localtime_r (&timet, &tm);
|
renatofilho@222
|
669 |
#else
|
renatofilho@222
|
670 |
{
|
renatofilho@222
|
671 |
struct tm *ptm = localtime (&timet);
|
renatofilho@222
|
672 |
|
renatofilho@222
|
673 |
if (ptm == NULL)
|
renatofilho@222
|
674 |
{
|
renatofilho@222
|
675 |
/* Happens at least in Microsoft's C library if you pass a
|
renatofilho@222
|
676 |
* negative time_t. Use 2000-01-01 as default date.
|
renatofilho@222
|
677 |
*/
|
renatofilho@222
|
678 |
#ifndef G_DISABLE_CHECKS
|
renatofilho@222
|
679 |
g_return_if_fail_warning (G_LOG_DOMAIN, "g_date_set_time", "ptm != NULL");
|
renatofilho@222
|
680 |
#endif
|
renatofilho@222
|
681 |
|
renatofilho@222
|
682 |
tm.tm_mon = 0;
|
renatofilho@222
|
683 |
tm.tm_mday = 1;
|
renatofilho@222
|
684 |
tm.tm_year = 100;
|
renatofilho@222
|
685 |
}
|
renatofilho@222
|
686 |
else
|
renatofilho@222
|
687 |
memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
|
renatofilho@222
|
688 |
}
|
renatofilho@222
|
689 |
#endif
|
renatofilho@222
|
690 |
|
renatofilho@222
|
691 |
date->julian = FALSE;
|
renatofilho@222
|
692 |
|
renatofilho@222
|
693 |
date->month = tm.tm_mon + 1;
|
renatofilho@222
|
694 |
date->day = tm.tm_mday;
|
renatofilho@222
|
695 |
date->year = tm.tm_year + 1900;
|
renatofilho@222
|
696 |
|
renatofilho@222
|
697 |
g_return_if_fail (g_date_valid_dmy (date->day, date->month, date->year));
|
renatofilho@222
|
698 |
|
renatofilho@222
|
699 |
date->dmy = TRUE;
|
renatofilho@222
|
700 |
}
|
renatofilho@222
|
701 |
|
renatofilho@222
|
702 |
|
renatofilho@222
|
703 |
void
|
renatofilho@222
|
704 |
g_date_set_time_val (GDate *date,
|
renatofilho@222
|
705 |
GTimeVal *timeval)
|
renatofilho@222
|
706 |
{
|
renatofilho@222
|
707 |
g_date_set_time_t (date, (time_t) timeval->tv_sec);
|
renatofilho@222
|
708 |
}
|
renatofilho@222
|
709 |
|
renatofilho@222
|
710 |
|
renatofilho@222
|
711 |
|
renatofilho@222
|
712 |
|
renatofilho@222
|
713 |
#endif
|