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