4 * @file gmyth/gmyth_util.c
6 * @brief <p> This component provides utility functions.
8 * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
9 * @author Hallyson Luiz de Morais Melo <hallyson.melo@indt.org.br>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "gmyth_util.h"
31 #include <glib/gprintf.h>
33 /** Converts a time_t struct in a GString at ISO standard format
34 * (e.g. 2006-07-20T09:56:41).
36 * The returned GString memory should be deallocated from
37 * the calling function.
39 * @param time_value the time value to be converted
40 * @return GString* the converted isoformat string
43 gmyth_util_time_to_isoformat (time_t time_value)
48 if (localtime_r(&time_value, &tm_time) == NULL) {
49 g_warning ("gmyth_util_time_to_isoformat convertion error!\n");
53 result = g_string_sized_new(20);
54 g_string_printf(result, "%04d-%02d-%02dT%02d:%02d:%02d",
55 tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
56 tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec);
61 /** Converts a time_t struct in a GString to the following
62 * format (e.g. 2006-07-20 09:56:41).
64 * The returned GString memory should be deallocated from
65 * the calling function.
67 * @param time_value the time value to be converted
68 * @return GString* the converted string
71 gmyth_util_time_to_string (time_t time_value)
73 GString *result = gmyth_util_time_to_isoformat (time_value);
74 result->str[10] = ' ';
79 /** Converts a GString in the following format
80 * (e.g. 2006-07-20 09:56:41) to a time_t struct.
82 * @param time_str the string to be converted
83 * @return time_t the time converted value
86 gmyth_util_string_to_time (GString* time_str)
88 int year, month, day, hour, min, sec;
90 g_debug( "[%s] time_str = %s.\n", __FUNCTION__, time_str != NULL ? time_str->str : "[time string is NULL!]" );
92 if (sscanf (time_str->str, "%04d-%02d-%02d %02d:%02d:%02d",
93 &year, &month, &day, &hour, &min, &sec) < 3) { /* At least date */
94 g_warning ("GMythUtil: isoformat_to_time converter error!\n");
98 tm_time.tm_year = year - 1900;
99 tm_time.tm_mon = month - 1;
100 tm_time.tm_mday = day;
101 tm_time.tm_hour = hour;
102 tm_time.tm_min = min;
103 tm_time.tm_sec = sec;
105 return mktime (&tm_time);
109 /** Decodes a long long variable from the string list
110 * format of the myhtprotocol.
112 * @param strlist the string list of mythprotocol values
113 * @param offset the list node offset of the long long variable
114 * @return guint64 the long long converted value
117 gmyth_util_decode_long_long(GMythStringList *strlist, guint offset)
120 guint64 ret_value = 0LL;
122 g_return_val_if_fail( strlist != NULL, ret_value );
124 if ( offset < gmyth_string_list_length( strlist ))
125 g_printerr( "[%s] Offset is lower than the Stringlist (offset = %d)!\n",
126 __FUNCTION__, offset );
128 g_return_val_if_fail( offset < gmyth_string_list_length( strlist ), ret_value );
130 gint l1 = gmyth_string_list_get_int( strlist, offset );
131 gint l2 = gmyth_string_list_get_int( strlist, offset + 1 );
133 ret_value = ((guint64)(l2) & 0xffffffffLL) | ((guint64)(l1) << 32);