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@1
|
27 |
|
leo_sobral@1
|
28 |
#include "gmyth_util.h"
|
leo_sobral@1
|
29 |
|
leo_sobral@1
|
30 |
#include <glib.h>
|
leo_sobral@1
|
31 |
#include <glib/gprintf.h>
|
leo_sobral@1
|
32 |
|
leo_sobral@1
|
33 |
/** Converts a time_t struct in a GString at ISO standard format
|
leo_sobral@1
|
34 |
* (e.g. 2006-07-20T09:56:41).
|
leo_sobral@1
|
35 |
*
|
leo_sobral@1
|
36 |
* The returned GString memory should be deallocated from
|
leo_sobral@1
|
37 |
* the calling function.
|
leo_sobral@1
|
38 |
*
|
leo_sobral@1
|
39 |
* @param time_value the time value to be converted
|
leo_sobral@1
|
40 |
* @return GString* the converted isoformat string
|
leo_sobral@1
|
41 |
*/
|
leo_sobral@1
|
42 |
GString*
|
leo_sobral@1
|
43 |
gmyth_util_time_to_isoformat (time_t time_value)
|
leo_sobral@1
|
44 |
{
|
leo_sobral@1
|
45 |
struct tm tm_time;
|
leo_sobral@1
|
46 |
GString *result;
|
leo_sobral@1
|
47 |
|
leo_sobral@1
|
48 |
if (localtime_r(&time_value, &tm_time) == NULL) {
|
leo_sobral@1
|
49 |
g_warning ("gmyth_util_time_to_isoformat convertion error!\n");
|
leo_sobral@1
|
50 |
return NULL;
|
leo_sobral@1
|
51 |
}
|
leo_sobral@1
|
52 |
|
leo_sobral@1
|
53 |
result = g_string_sized_new(20);
|
leo_sobral@1
|
54 |
g_string_printf(result, "%04d-%02d-%02dT%02d:%02d:%02d",
|
leo_sobral@1
|
55 |
tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
|
leo_sobral@1
|
56 |
tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec);
|
leo_sobral@1
|
57 |
|
leo_sobral@1
|
58 |
return result;
|
leo_sobral@1
|
59 |
}
|
leo_sobral@1
|
60 |
|
leo_sobral@1
|
61 |
/** Converts a time_t struct in a GString to the following
|
leo_sobral@1
|
62 |
* format (e.g. 2006-07-20 09:56:41).
|
leo_sobral@1
|
63 |
*
|
leo_sobral@1
|
64 |
* The returned GString memory should be deallocated from
|
leo_sobral@1
|
65 |
* the calling function.
|
leo_sobral@1
|
66 |
*
|
leo_sobral@1
|
67 |
* @param time_value the time value to be converted
|
leo_sobral@1
|
68 |
* @return GString* the converted string
|
leo_sobral@1
|
69 |
*/
|
leo_sobral@1
|
70 |
GString*
|
leo_sobral@1
|
71 |
gmyth_util_time_to_string (time_t time_value)
|
leo_sobral@1
|
72 |
{
|
leo_sobral@1
|
73 |
GString *result = gmyth_util_time_to_isoformat (time_value);
|
leo_sobral@1
|
74 |
result->str[10] = ' ';
|
leo_sobral@1
|
75 |
|
leo_sobral@1
|
76 |
return result;
|
leo_sobral@1
|
77 |
}
|
leo_sobral@1
|
78 |
|
leo_sobral@1
|
79 |
/** Converts a GString in the following format
|
leo_sobral@1
|
80 |
* (e.g. 2006-07-20 09:56:41) to a time_t struct.
|
leo_sobral@1
|
81 |
*
|
leo_sobral@1
|
82 |
* @param time_str the string to be converted
|
leo_sobral@1
|
83 |
* @return time_t the time converted value
|
leo_sobral@1
|
84 |
*/
|
leo_sobral@1
|
85 |
time_t
|
leo_sobral@1
|
86 |
gmyth_util_string_to_time (GString* time_str)
|
leo_sobral@1
|
87 |
{
|
leo_sobral@1
|
88 |
int year, month, day, hour, min, sec;
|
leo_sobral@1
|
89 |
|
leo_sobral@1
|
90 |
g_debug( "[%s] time_str = %s.\n", __FUNCTION__, time_str != NULL ? time_str->str : "[time string is NULL!]" );
|
leo_sobral@1
|
91 |
|
leo_sobral@1
|
92 |
if (sscanf (time_str->str, "%04d-%02d-%02d %02d:%02d:%02d",
|
leo_sobral@1
|
93 |
&year, &month, &day, &hour, &min, &sec) < 3) { /* At least date */
|
leo_sobral@1
|
94 |
g_warning ("GMythUtil: isoformat_to_time converter error!\n");
|
leo_sobral@1
|
95 |
return 0;
|
leo_sobral@1
|
96 |
} else {
|
leo_sobral@1
|
97 |
struct tm tm_time;
|
leo_sobral@1
|
98 |
tm_time.tm_year = year - 1900;
|
leo_sobral@1
|
99 |
tm_time.tm_mon = month - 1;
|
leo_sobral@1
|
100 |
tm_time.tm_mday = day;
|
leo_sobral@1
|
101 |
tm_time.tm_hour = hour;
|
leo_sobral@1
|
102 |
tm_time.tm_min = min;
|
leo_sobral@1
|
103 |
tm_time.tm_sec = sec;
|
leo_sobral@1
|
104 |
|
leo_sobral@1
|
105 |
return mktime (&tm_time);
|
leo_sobral@1
|
106 |
}
|
leo_sobral@1
|
107 |
}
|
leo_sobral@1
|
108 |
|
leo_sobral@1
|
109 |
/** Decodes a long long variable from the string list
|
leo_sobral@1
|
110 |
* format of the myhtprotocol.
|
leo_sobral@1
|
111 |
*
|
leo_sobral@1
|
112 |
* @param strlist the string list of mythprotocol values
|
leo_sobral@1
|
113 |
* @param offset the list node offset of the long long variable
|
rosfran@35
|
114 |
* @return gint64 the long long converted value
|
leo_sobral@1
|
115 |
*/
|
rosfran@35
|
116 |
gint64
|
leo_sobral@1
|
117 |
gmyth_util_decode_long_long(GMythStringList *strlist, guint offset)
|
leo_sobral@1
|
118 |
{
|
leo_sobral@1
|
119 |
|
rosfran@35
|
120 |
gint64 ret_value = 0LL;
|
leo_sobral@1
|
121 |
|
leo_sobral@1
|
122 |
g_return_val_if_fail( strlist != NULL, ret_value );
|
leo_sobral@1
|
123 |
|
rosfran@35
|
124 |
if ( offset > gmyth_string_list_length( strlist ))
|
rosfran@35
|
125 |
g_printerr( "[%s] Offset is greater than the Stringlist (offset = %d)!\n",
|
leo_sobral@1
|
126 |
__FUNCTION__, offset );
|
leo_sobral@1
|
127 |
|
leo_sobral@1
|
128 |
g_return_val_if_fail( offset < gmyth_string_list_length( strlist ), ret_value );
|
leo_sobral@1
|
129 |
|
leo_sobral@1
|
130 |
gint l1 = gmyth_string_list_get_int( strlist, offset );
|
leo_sobral@1
|
131 |
gint l2 = gmyth_string_list_get_int( strlist, offset + 1 );
|
leo_sobral@1
|
132 |
|
rosfran@35
|
133 |
ret_value = (l2 /*& 0xffffffffLL*/) | ( (gint64)l1 << 32 );
|
leo_sobral@1
|
134 |
|
leo_sobral@1
|
135 |
return ret_value;
|
leo_sobral@1
|
136 |
|
leo_sobral@1
|
137 |
}
|
leo_sobral@1
|
138 |
|
rosfran@35
|
139 |
|