gmyth/gmyth/gmyth_util.c
author ali@juiblex.co.uk
Wed Dec 16 10:06:21 2009 +0000 (2009-12-16)
branchtrunk
changeset 951 7b0d87ef5e63
parent 950 6308269b026e
permissions -rw-r--r--
Add rules and upcoming lists to gmyth_ls
leo_sobral@1
     1
/**
rosfran@420
     2
 * GMyth Library
rosfran@420
     3
 *
rosfran@420
     4
 * @file gmyth/gmyth_util.c
rosfran@420
     5
 * 
rosfran@420
     6
 * @brief <p> This component provides utility functions 
rosfran@420
     7
 * 	(dealing with dates, time, string formatting, etc.).
rosfran@420
     8
 * 
rosfran@420
     9
 * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
rosfran@420
    10
 * @author Hallyson Luiz de Morais Melo <hallyson.melo@indt.org.br>
rosfran@420
    11
 * @author Rosfran Borges <rosfran.borges@indt.org.br>
rosfran@420
    12
 *
rosfran@701
    13
 * 
rosfran@701
    14
 * This program is free software; you can redistribute it and/or modify
rosfran@701
    15
 * it under the terms of the GNU Lesser General Public License as published by
rosfran@701
    16
 * the Free Software Foundation; either version 2 of the License, or
rosfran@701
    17
 * (at your option) any later version.
rosfran@701
    18
 *
rosfran@701
    19
 * This program is distributed in the hope that it will be useful,
rosfran@701
    20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rosfran@701
    21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
rosfran@701
    22
 * GNU General Public License for more details.
rosfran@701
    23
 *
rosfran@701
    24
 * You should have received a copy of the GNU Lesser General Public License
rosfran@701
    25
 * along with this program; if not, write to the Free Software
rosfran@701
    26
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
rosfran@701
    27
 */
rosfran@698
    28
leo_sobral@213
    29
#ifdef HAVE_CONFIG_H
leo_sobral@213
    30
#include "config.h"
leo_sobral@213
    31
#endif
leo_sobral@213
    32
rosfran@214
    33
#define _XOPEN_SOURCE
rosfran@214
    34
#define _XOPEN_SOURCE_EXTENDED
rosfran@223
    35
#define __USE_MISC
leo_sobral@1
    36
melunko@125
    37
#include <glib.h>
melunko@125
    38
#include <glib/gprintf.h>
rosfran@214
    39
#include <time.h>
rosfran@214
    40
#include <sys/time.h>
rosfran@223
    41
#include <sys/timex.h>
melunko@125
    42
melunko@587
    43
#include "gmyth_socket.h"
melunko@587
    44
#include "gmyth_recorder.h"
melunko@587
    45
#include "gmyth_common.h"
melunko@587
    46
#include "gmyth_debug.h"
melunko@125
    47
melunko@798
    48
#include "gmyth_util.h"
melunko@798
    49
renatofilho@222
    50
#if !GLIB_CHECK_VERSION (2, 10, 0)
renatofilho@754
    51
gchar          *g_time_val_to_iso8601(GTimeVal * time_);
renatofilho@754
    52
gboolean        g_time_val_from_iso8601(const gchar * iso_date,
renatofilho@754
    53
                                        GTimeVal * time_);
renatofilho@754
    54
void            g_date_set_time_val(GDate * date, GTimeVal * timeval);
renatofilho@222
    55
renatofilho@222
    56
#endif
renatofilho@222
    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
 */
renatofilho@754
    67
GString        *
renatofilho@750
    68
gmyth_util_time_to_isoformat(time_t time_value)
leo_sobral@1
    69
{
renatofilho@754
    70
    struct tm       tm_time;
renatofilho@754
    71
    GString        *result;
rosfran@223
    72
renatofilho@754
    73
    if (localtime_r(&time_value, &tm_time) == NULL) {
renatofilho@754
    74
        gmyth_debug("gmyth_util_time_to_isoformat convertion error!\n");
renatofilho@754
    75
        return NULL;
renatofilho@754
    76
    }
rosfran@698
    77
renatofilho@754
    78
    result = g_string_sized_new(20);
renatofilho@754
    79
    g_string_printf(result, "%04d-%02d-%02dT%02d:%02d:%02d",
renatofilho@754
    80
                    tm_time.tm_year + 1900, tm_time.tm_mon + 1,
renatofilho@754
    81
                    tm_time.tm_mday, tm_time.tm_hour, tm_time.tm_min,
renatofilho@754
    82
                    tm_time.tm_sec);
rosfran@698
    83
renatofilho@754
    84
    gmyth_debug("Result (ISO 8601) = %s", result->str);
rosfran@698
    85
renatofilho@754
    86
    return result;
rosfran@214
    87
}
rosfran@214
    88
rosfran@214
    89
/** Converts a time_t struct in a GString at ISO standard format 
rosfran@214
    90
 * (e.g. 2006-07-20T09:56:41).
rosfran@214
    91
 * 
rosfran@214
    92
 * The returned GString memory should be deallocated from 
rosfran@214
    93
 * the calling function.
rosfran@214
    94
 *
rosfran@214
    95
 * @param time_value the GTimeValue to be converted
rosfran@214
    96
 * @return GString* the converted isoformat string 
rosfran@214
    97
 */
melunko@935
    98
gchar*
renatofilho@750
    99
gmyth_util_time_to_isoformat_from_time_val_fmt(const gchar * fmt_string,
renatofilho@754
   100
                                               const GTimeVal * time_val)
rosfran@214
   101
{
renatofilho@754
   102
    gchar          *result = NULL;
renatofilho@754
   103
    struct tm      *tm_time = NULL;
renatofilho@754
   104
    time_t          time;
rosfran@399
   105
renatofilho@754
   106
    gint            buffer_len = 0;
rosfran@214
   107
renatofilho@754
   108
    g_return_val_if_fail(fmt_string != NULL, NULL);
rosfran@221
   109
renatofilho@754
   110
    g_return_val_if_fail(time_val != NULL, NULL);
leo_sobral@446
   111
renatofilho@754
   112
    time = time_val->tv_sec;    // + (gint)( time_val->tv_usec /
renatofilho@754
   113
    // G_USEC_PER_SEC );
rosfran@698
   114
renatofilho@754
   115
    tm_time = g_malloc0(sizeof(struct tm));
rosfran@698
   116
renatofilho@754
   117
    if (NULL == localtime_r(&time, tm_time)) {
renatofilho@754
   118
        gmyth_debug("gmyth_util_time_to_isoformat convertion error!\n");
renatofilho@754
   119
    } else {
renatofilho@754
   120
        /*
renatofilho@754
   121
         * we first check the return of strftime to allocate a buffer of
renatofilho@754
   122
         * the correct size 
renatofilho@754
   123
         */
renatofilho@754
   124
        buffer_len = strftime(NULL, SSIZE_MAX, fmt_string, tm_time);
renatofilho@754
   125
        if (buffer_len > 0) {
renatofilho@754
   126
            result = g_malloc0(buffer_len + 1);
renatofilho@754
   127
            if (result == NULL) {
renatofilho@754
   128
                gmyth_debug
renatofilho@754
   129
                    ("gmyth_util_time_to_isoformat convertion error!\n");
renatofilho@754
   130
                return NULL;
renatofilho@754
   131
            }
renatofilho@754
   132
            strftime(result, buffer_len + 1, fmt_string, tm_time);
renatofilho@754
   133
            gmyth_debug("Dateline (ISO result): %s", result);
renatofilho@754
   134
        }
renatofilho@754
   135
    }                           /* if */
rosfran@698
   136
renatofilho@754
   137
    gmyth_debug("Result (strftime) = %s", result);
rosfran@698
   138
renatofilho@754
   139
    // strptime( result, "%Y-%m-%dT%H:%M:%SZ", tm_time ); 
rosfran@698
   140
renatofilho@754
   141
    // strftime( result, strlen(result), fmt_string, tm_time );
rosfran@698
   142
renatofilho@754
   143
    g_free(tm_time);
rosfran@698
   144
renatofilho@754
   145
    gmyth_debug("Result (ISO 8601) = %s", result);
rosfran@698
   146
renatofilho@754
   147
    return result;
rosfran@214
   148
}
rosfran@214
   149
rosfran@214
   150
/** Converts a time_t struct in a GString at ISO standard format 
morphbr@306
   151
 * (e.g. 2006-07-20 09:56:41).
rosfran@214
   152
 * 
rosfran@214
   153
 * The returned GString memory should be deallocated from 
rosfran@214
   154
 * the calling function.
rosfran@214
   155
 *
rosfran@214
   156
 * @param time_value the GTimeValue to be converted
rosfran@214
   157
 * @return GString* the converted isoformat string 
rosfran@214
   158
 */
renatofilho@754
   159
gchar          *
renatofilho@750
   160
gmyth_util_time_to_isoformat_from_time_val(const GTimeVal * time)
rosfran@214
   161
{
renatofilho@754
   162
    gchar          *result =
renatofilho@754
   163
        gmyth_util_time_to_isoformat_from_time_val_fmt("%Y-%m-%d %H:%M:%S",
renatofilho@754
   164
                                                       time);
rosfran@698
   165
renatofilho@754
   166
    // result[10] = ' ';
renatofilho@754
   167
    // result[ strlen(result) - 1] = '\0';
rosfran@698
   168
renatofilho@754
   169
    return result;
rosfran@214
   170
}
rosfran@214
   171
morphbr@306
   172
/** Converts a time_t struct in a GString at ISO standard format 2 
morphbr@306
   173
 * (e.g. 2006-07-20T09:56:41).
morphbr@306
   174
 * 
morphbr@306
   175
 * The returned GString memory should be deallocated from 
morphbr@306
   176
 * the calling function.
morphbr@306
   177
 *
morphbr@306
   178
 * @param time_value the GTimeValue to be converted
morphbr@306
   179
 * @return GString* the converted isoformat string 
morphbr@306
   180
 */
renatofilho@754
   181
gchar          *
renatofilho@750
   182
gmyth_util_time_to_mythformat_from_time_val(const GTimeVal * time)
morphbr@306
   183
{
renatofilho@754
   184
    gchar          *result =
renatofilho@754
   185
        gmyth_util_time_to_isoformat_from_time_val_fmt("%Y-%m-%dT%H:%M:%S",
renatofilho@754
   186
                                                       time);
rosfran@698
   187
renatofilho@754
   188
    return result;
morphbr@306
   189
}
morphbr@306
   190
rosfran@214
   191
/** Converts a time_t struct in a GString at ISO standard format 
rosfran@214
   192
 * (e.g. 2006-07-20T09:56:41).
rosfran@214
   193
 * 
rosfran@214
   194
 * The returned GString memory should be deallocated from 
rosfran@214
   195
 * the calling function.
rosfran@214
   196
 *
rosfran@214
   197
 * @param time_value the GTimeValue to be converted
rosfran@214
   198
 * @return GString* the converted isoformat string 
rosfran@214
   199
 */
renatofilho@754
   200
gchar          *
renatofilho@750
   201
gmyth_util_time_to_string_only_date(const GTimeVal * time)
rosfran@214
   202
{
renatofilho@754
   203
    gchar          *result =
renatofilho@754
   204
        gmyth_util_time_to_isoformat_from_time_val_fmt("%Y-%m-%d", time);
renatofilho@754
   205
    // result[10] = ' ';
renatofilho@754
   206
    // result[ strlen(result) - 1] = '\0';
renatofilho@754
   207
    return result;
rosfran@214
   208
}
rosfran@214
   209
rosfran@214
   210
/** Converts a time_t struct in a GString at ISO standard format 
rosfran@214
   211
 * (e.g. 2006-07-20T09:56:41).
rosfran@214
   212
 * 
rosfran@214
   213
 * The returned GString memory should be deallocated from 
rosfran@214
   214
 * the calling function.
rosfran@214
   215
 *
rosfran@214
   216
 * @param time_value the GTimeValue to be converted
rosfran@214
   217
 * @return GString* the converted isoformat string 
rosfran@214
   218
 */
renatofilho@754
   219
gchar          *
renatofilho@750
   220
gmyth_util_time_to_string_only_time(const GTimeVal * time)
rosfran@214
   221
{
renatofilho@754
   222
    gchar          *result =
renatofilho@754
   223
        gmyth_util_time_to_isoformat_from_time_val_fmt("%H:%M:%S", time);
renatofilho@754
   224
    // result[10] = ' ';
renatofilho@754
   225
    // result[ strlen(result) - 1] = '\0';
renatofilho@754
   226
    return result;
leo_sobral@1
   227
}
leo_sobral@1
   228
leo_sobral@1
   229
/** Converts a time_t struct in a GString to the following 
leo_sobral@1
   230
 * format (e.g. 2006-07-20 09:56:41).
leo_sobral@1
   231
 * 
leo_sobral@1
   232
 * The returned GString memory should be deallocated from 
leo_sobral@1
   233
 * the calling function.
leo_sobral@1
   234
 *
leo_sobral@1
   235
 * @param time_value the time value to be converted
leo_sobral@1
   236
 * @return GString* the converted string 
leo_sobral@1
   237
 */
renatofilho@754
   238
GString        *
renatofilho@750
   239
gmyth_util_time_to_string(time_t time_value)
leo_sobral@1
   240
{
renatofilho@754
   241
    GString        *result = gmyth_util_time_to_isoformat(time_value);
leo_sobral@1
   242
renatofilho@754
   243
    result->str[10] = ' ';
renatofilho@754
   244
    result->str[strlen(result->str) - 1] = '\0';
rosfran@698
   245
renatofilho@754
   246
    return result;
leo_sobral@1
   247
}
leo_sobral@1
   248
rosfran@214
   249
/** Converts a time_t struct in a GString to the following 
rosfran@214
   250
 * format (e.g. 2006-07-20 09:56:41).
rosfran@214
   251
 * 
rosfran@214
   252
 * The returned GString memory should be deallocated from 
rosfran@214
   253
 * the calling function.
rosfran@214
   254
 *
rosfran@214
   255
 * @param time_value the time value to be converted
rosfran@214
   256
 * @return GString* the converted string 
rosfran@214
   257
 */
melunko@934
   258
gchar*
renatofilho@750
   259
gmyth_util_time_to_string_from_time_val(const GTimeVal * time_val)
rosfran@214
   260
{
melunko@934
   261
    gchar *result =
renatofilho@754
   262
        gmyth_util_time_to_isoformat_from_time_val_fmt("%Y-%m-%d %H:%M:%S",
renatofilho@754
   263
                                                       time_val);
rosfran@214
   264
renatofilho@754
   265
    // result[10] = ' ';
rosfran@698
   266
renatofilho@754
   267
    return result;
rosfran@214
   268
}
rosfran@214
   269
leo_sobral@1
   270
/** Converts a GString in the following format 
leo_sobral@1
   271
 * (e.g. 2006-07-20 09:56:41) to a time_t struct.
leo_sobral@1
   272
 * 
leo_sobral@1
   273
 * @param time_str the string to be converted
leo_sobral@1
   274
 * @return time_t the time converted value
leo_sobral@1
   275
 */
leo_sobral@1
   276
time_t
renatofilho@750
   277
gmyth_util_string_to_time(GString * time_str)
leo_sobral@1
   278
{
renatofilho@754
   279
    gint            year,
renatofilho@754
   280
                    month,
renatofilho@754
   281
                    day,
renatofilho@754
   282
                    hour,
renatofilho@754
   283
                    min,
renatofilho@754
   284
                    sec;
leo_sobral@1
   285
renatofilho@754
   286
    gmyth_debug("[%s] time_str = %s. [%s]", __FUNCTION__,
renatofilho@754
   287
                time_str !=
renatofilho@754
   288
                NULL ? time_str->str : "[time string is NULL!]",
renatofilho@754
   289
                time_str->str);
rosfran@698
   290
renatofilho@754
   291
    if (sscanf(time_str->str, "%04d-%02d-%02d %02d:%02d:%02d",
renatofilho@754
   292
               &year, &month, &day, &hour, &min, &sec) < 3) {
renatofilho@754
   293
        gmyth_debug("GMythUtil: isoformat_to_time converter error!\n");
renatofilho@754
   294
        return 0;
renatofilho@754
   295
    }
rosfran@698
   296
renatofilho@754
   297
    struct tm      *tm_time = g_malloc0(sizeof(struct tm));
rosfran@698
   298
renatofilho@754
   299
    tm_time->tm_year = year - 1900;
renatofilho@754
   300
    tm_time->tm_mon = month - 1;
renatofilho@754
   301
    tm_time->tm_mday = day;
renatofilho@754
   302
    tm_time->tm_hour = hour;
renatofilho@754
   303
    tm_time->tm_min = min;
renatofilho@754
   304
    tm_time->tm_sec = sec;
rosfran@698
   305
renatofilho@754
   306
    return mktime(tm_time);
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
 */
renatofilho@754
   315
struct tm      *
renatofilho@750
   316
gmyth_util_time_val_to_date(const GTimeVal * time)
rosfran@214
   317
{
renatofilho@754
   318
    struct tm      *date = g_malloc0(sizeof(struct tm));
renatofilho@754
   319
    time_t          time_micros = time->tv_sec; // + (gint)( time->tv_usec 
renatofilho@754
   320
                                                // 
renatofilho@754
   321
    // 
renatofilho@754
   322
    // / G_USEC_PER_SEC );
rosfran@223
   323
renatofilho@754
   324
    if (NULL == date) {
renatofilho@754
   325
        gmyth_debug
renatofilho@754
   326
            ("GMythUtil: GDate *gmyth_util_time_val_to_date (GTimeVal* time) - converter error!\n");
renatofilho@754
   327
        return NULL;
renatofilho@754
   328
    }
rosfran@698
   329
renatofilho@754
   330
    if (NULL == localtime_r(&time_micros, date)) {
renatofilho@754
   331
        gmyth_debug("gmyth_util_time_to_isoformat convertion error!\n");
renatofilho@754
   332
        return NULL;
renatofilho@754
   333
    }
rosfran@698
   334
renatofilho@754
   335
    gmyth_debug("Converted from GTimeVal == %s to GDate", asctime(date));
rosfran@698
   336
renatofilho@754
   337
    return date;
rosfran@214
   338
}
rosfran@214
   339
rosfran@214
   340
/** Converts a GString in the following format 
rosfran@214
   341
 * (e.g. 2006-07-20 09:56:41) to a time_t struct.
rosfran@214
   342
 * 
rosfran@214
   343
 * @param time_str the string to be converted
rosfran@214
   344
 * @return time_t the time converted value
rosfran@214
   345
 */
renatofilho@754
   346
GTimeVal       *
renatofilho@750
   347
gmyth_util_string_to_time_val_fmt(const gchar * fmt_string,
renatofilho@754
   348
                                  const gchar * time_str)
rosfran@214
   349
{
renatofilho@754
   350
    GTimeVal       *time = g_new0(GTimeVal, 1);
renatofilho@754
   351
    struct tm      *tm_time = NULL;
renatofilho@754
   352
    time_t          time_micros;
renatofilho@754
   353
    gchar          *result;
rosfran@698
   354
renatofilho@754
   355
    gmyth_debug("[%s] time_str = %s. [%s]", time_str, time_str != NULL ?
renatofilho@754
   356
                time_str : "[time string is NULL!]", time_str);
rosfran@698
   357
renatofilho@754
   358
    if (NULL == time_str) {
renatofilho@754
   359
        gmyth_debug("GMythUtil: isoformat_to_time converter error!\n");
renatofilho@754
   360
        return NULL;
renatofilho@754
   361
    }
rosfran@698
   362
renatofilho@754
   363
    tm_time = g_malloc0(sizeof(struct tm));
rosfran@698
   364
renatofilho@754
   365
    /*
renatofilho@754
   366
     * we first check the return of strftime to allocate a buffer of the
renatofilho@754
   367
     * correct size 
renatofilho@754
   368
     */
renatofilho@754
   369
    result = strptime(time_str, "%Y-%m-%dT%H:%M:%S", tm_time);
renatofilho@754
   370
    if (NULL == result) {
renatofilho@754
   371
        /*
renatofilho@754
   372
         * we first check the return of strftime to allocate a buffer of
renatofilho@754
   373
         * the correct size 
renatofilho@754
   374
         */
renatofilho@754
   375
        result = strptime(time_str, "%Y-%m-%dT%H:%M:%SZ", tm_time);
renatofilho@754
   376
        if (NULL == result) {
renatofilho@754
   377
            /*
renatofilho@754
   378
             * we first check the return of strftime to allocate a buffer
renatofilho@754
   379
             * of the correct size 
renatofilho@754
   380
             */
renatofilho@754
   381
            result = strptime(time_str, "%Y-%m-%d %H:%M:%S", tm_time);
renatofilho@754
   382
            if (NULL == result) {
renatofilho@754
   383
                result = strptime(time_str, "%Y-%m-%dT%H:%M", tm_time);
renatofilho@754
   384
                if (NULL == result) {
renatofilho@754
   385
                    gmyth_debug("Dateline (ISO result): %s", result);
renatofilho@754
   386
                    g_free(tm_time);
renatofilho@754
   387
                    return NULL;
renatofilho@754
   388
                    // goto done; 
renatofilho@754
   389
                }
renatofilho@754
   390
            }
renatofilho@754
   391
        }
renatofilho@754
   392
    }
rosfran@698
   393
renatofilho@754
   394
    time_micros = mktime(tm_time);
rosfran@698
   395
renatofilho@754
   396
    time->tv_sec = time_micros; // + (gint)( time_val->tv_usec /
renatofilho@754
   397
    // G_USEC_PER_SEC );
rosfran@698
   398
renatofilho@754
   399
    gmyth_debug("After mktime call... = %s", asctime(tm_time));
rosfran@698
   400
renatofilho@754
   401
    g_free(tm_time);
rosfran@698
   402
renatofilho@754
   403
    return time;
rosfran@223
   404
}
rosfran@223
   405
rosfran@223
   406
/** Converts a GString in the following format 
rosfran@223
   407
 * (e.g. 2006-07-20 09:56:41) to a time_t struct.
rosfran@223
   408
 * 
rosfran@223
   409
 * @param time_str the string to be converted
rosfran@223
   410
 * @return time_t the time converted value
rosfran@223
   411
 */
renatofilho@754
   412
GTimeVal       *
renatofilho@750
   413
gmyth_util_string_to_time_val(const gchar * time_str)
rosfran@223
   414
{
renatofilho@754
   415
    GTimeVal       *time =
renatofilho@754
   416
        gmyth_util_string_to_time_val_fmt("%Y-%m-%d %H:%M:%S", time_str);
rosfran@698
   417
renatofilho@754
   418
    return time;
leo_sobral@1
   419
}
leo_sobral@1
   420
rosfran@420
   421
/** 
rosfran@420
   422
 * Checks if the given remote file exists.
rosfran@420
   423
 * 
rosfran@420
   424
 * @param backend_info The GMythBackendInfo instance.
rosfran@420
   425
 * @param filename The file name of the remote file.
rosfran@420
   426
 * 
rosfran@420
   427
 * @return <code>true</code>, if the remote file exists.
rosfran@420
   428
 */
melunko@125
   429
gboolean
renatofilho@750
   430
gmyth_util_file_exists(GMythBackendInfo * backend_info,
renatofilho@754
   431
                       const gchar * filename)
melunko@125
   432
{
renatofilho@754
   433
    GMythSocket    *socket;
renatofilho@769
   434
    gboolean        res = FALSE;
rosfran@698
   435
renatofilho@754
   436
    gmyth_debug("Check if file %s exists", filename);
melunko@412
   437
renatofilho@754
   438
    g_return_val_if_fail(backend_info != NULL, FALSE);
renatofilho@754
   439
    g_return_val_if_fail(filename != NULL, FALSE);
melunko@412
   440
renatofilho@769
   441
    socket = gmyth_backend_info_get_connected_socket (backend_info);
renatofilho@769
   442
    if (socket != NULL) {
renatofilho@769
   443
        res = gmyth_util_file_exists_from_socket (socket, filename);
renatofilho@770
   444
        g_object_unref(socket);
renatofilho@754
   445
    }
renatofilho@754
   446
    return res;
melunko@125
   447
}
melunko@125
   448
renatofilho@769
   449
gboolean
renatofilho@769
   450
gmyth_util_file_exists_from_socket (GMythSocket *sock, 
renatofilho@769
   451
                                    const gchar *filename)
renatofilho@769
   452
{
melunko@798
   453
    gboolean res = FALSE;
ali@950
   454
    GString *path;
ali@950
   455
    GMythProgramInfo *program;
renatofilho@769
   456
renatofilho@769
   457
    program = gmyth_program_info_new();
renatofilho@769
   458
    program->pathname = g_string_new(filename);
renatofilho@769
   459
ali@950
   460
    path = gmyth_util_check_file_from_socket (sock, program);
ali@950
   461
ali@950
   462
    if (path) {
ali@950
   463
        res = TRUE;
ali@950
   464
        g_string_free(path, TRUE);
ali@950
   465
    }
ali@950
   466
ali@950
   467
    g_object_unref(program);
ali@950
   468
ali@950
   469
    return res;
ali@950
   470
}
ali@950
   471
 
ali@950
   472
GString *
ali@950
   473
gmyth_util_check_file_from_socket (GMythSocket *sock,
ali@950
   474
                                   GMythProgramInfo *program)
ali@950
   475
{
ali@950
   476
    GString *res;
ali@950
   477
    gint length;
ali@950
   478
    GMythStringList *slist;
ali@950
   479
renatofilho@769
   480
    slist = gmyth_string_list_new();
renatofilho@769
   481
    gmyth_string_list_append_char_array(slist, "QUERY_CHECKFILE");
ali@950
   482
    if (gmyth_socket_get_protocol_version(sock) >= 32)
ali@950
   483
	gmyth_string_list_append_int(slist, 1);
melunko@798
   484
    gmyth_program_info_to_string_list(program, slist);
renatofilho@769
   485
melunko@798
   486
    length = gmyth_socket_sendreceive_stringlist (sock, slist);
ali@950
   487
    if (length > 1 && gmyth_string_list_get_int(slist, 0) == 1)
ali@950
   488
        res = gmyth_string_list_get_string(slist, 1);
ali@950
   489
    else
ali@950
   490
        res = NULL;
renatofilho@769
   491
renatofilho@769
   492
    g_object_unref(slist);
renatofilho@769
   493
renatofilho@769
   494
    return res;
renatofilho@769
   495
}
melunko@798
   496
 
melunko@798
   497
gboolean
melunko@798
   498
gmyth_util_get_backend_details (GMythSocket *sock, GMythBackendDetails **details)
melunko@798
   499
{
melunko@798
   500
    gboolean res = FALSE;
melunko@798
   501
    gint length = 0;
melunko@798
   502
    GMythStringList *slist;
melunko@798
   503
melunko@798
   504
    slist = gmyth_string_list_new();
melunko@798
   505
    gmyth_string_list_append_char_array(slist, "QUERY_FREE_SPACE");
melunko@798
   506
melunko@798
   507
    length = gmyth_socket_sendreceive_stringlist (sock, slist);
melunko@798
   508
    if (length >= 8) {
melunko@798
   509
        *details = g_new0 (GMythBackendDetails, 1);
melunko@799
   510
        (*details)->total_space = gmyth_string_list_get_uint64 (slist, 4) * 1024;
melunko@799
   511
        (*details)->used_space = gmyth_string_list_get_uint64 (slist, 6) * 1024;
melunko@798
   512
        res = TRUE;
melunko@798
   513
    }
melunko@798
   514
melunko@798
   515
    g_object_unref(slist);
melunko@798
   516
melunko@798
   517
    return res;
melunko@798
   518
}
melunko@798
   519
melunko@798
   520
void
melunko@798
   521
gmyth_util_backend_details_free (GMythBackendDetails *details)
melunko@798
   522
{
melunko@798
   523
    g_free (details);
melunko@798
   524
}
melunko@798
   525
                     
renatofilho@769
   526
rosfran@420
   527
/** 
rosfran@420
   528
 * Checks if the given remote file exists, and gets its remote directory.
rosfran@420
   529
 * 
rosfran@420
   530
 * @param backend_info The GMythBackendInfo instance.
rosfran@420
   531
 * @param filename The file name of the remote file.
rosfran@420
   532
 * @param current_dir String pointer to the directory where the remote file is stored.
rosfran@420
   533
 * 
rosfran@420
   534
 * @return <code>true</code>, if the remote file exists.
rosfran@420
   535
 */
rosfran@384
   536
gboolean
renatofilho@750
   537
gmyth_util_file_exists_and_get_remote_dir(GMythBackendInfo * backend_info,
renatofilho@754
   538
                                          const gchar * filename,
renatofilho@754
   539
                                          gchar ** current_dir)
rosfran@384
   540
{
renatofilho@754
   541
    GMythSocket    *socket;
renatofilho@754
   542
    gboolean        res;
rosfran@698
   543
renatofilho@754
   544
    *current_dir = NULL;
rosfran@698
   545
renatofilho@754
   546
    socket = gmyth_socket_new();
renatofilho@754
   547
    res = gmyth_socket_connect_to_backend(socket, backend_info->hostname,
renatofilho@754
   548
                                          backend_info->port, TRUE);
rosfran@384
   549
renatofilho@754
   550
    if (res == TRUE) {
ali@950
   551
        GMythProgramInfo *program;
ali@950
   552
	GString *path;
rosfran@384
   553
renatofilho@754
   554
        program = gmyth_program_info_new();
renatofilho@754
   555
        program->pathname = g_string_new(filename);
rosfran@384
   556
ali@950
   557
        path = gmyth_util_check_file_from_socket (socket, program);
rosfran@384
   558
ali@950
   559
        res = !!path;
rosfran@384
   560
ali@950
   561
        if (path)
ali@950
   562
            *current_dir = g_string_free(path, FALSE);
rosfran@698
   563
renatofilho@754
   564
        if (*current_dir != NULL)
renatofilho@754
   565
            gmyth_debug("Current directory = %s.", (*current_dir != NULL)
renatofilho@754
   566
                        ? *current_dir : "[directory not found]");
rosfran@698
   567
renatofilho@754
   568
        g_object_unref(program);
rosfran@384
   569
renatofilho@754
   570
        gmyth_socket_close_connection(socket);
renatofilho@754
   571
    }
renatofilho@754
   572
    g_object_unref(socket);
renatofilho@754
   573
    return res;
rosfran@384
   574
}
rosfran@384
   575
rosfran@420
   576
/** 
rosfran@420
   577
 * Creates a file name to a possible existing remote file,
rosfran@420
   578
 * based on some fields of the LiveTV/recorded program info.
rosfran@420
   579
 * 
rosfran@420
   580
 * @param chan_id The channel ID number.
rosfran@420
   581
 * @param start_time The start time of the recording.
rosfran@420
   582
 * 
rosfran@420
   583
 * @return The string representing the file name.
rosfran@420
   584
 */
renatofilho@754
   585
gchar          *
renatofilho@750
   586
gmyth_util_create_filename(const gint chan_id, const GTimeVal * start_time)
rosfran@698
   587
{
renatofilho@754
   588
    gchar          *basename = NULL;
rosfran@384
   589
renatofilho@754
   590
    g_return_val_if_fail(start_time != NULL, NULL);
rosfran@384
   591
renatofilho@754
   592
    gchar          *isodate =
renatofilho@754
   593
        gmyth_util_time_to_isoformat_from_time_val_fmt("%Y%m%d%H%M%S",
renatofilho@754
   594
                                                       start_time);
rosfran@384
   595
renatofilho@754
   596
    basename = g_strdup_printf("%d_%s", chan_id, isodate);
rosfran@698
   597
renatofilho@754
   598
    gmyth_debug("Basename (from chan_id and start_time): %s", basename);
rosfran@698
   599
renatofilho@754
   600
    if (isodate)
renatofilho@754
   601
        g_free(isodate);
rosfran@698
   602
renatofilho@754
   603
    return basename;
rosfran@384
   604
}
renatofilho@222
   605
rosfran@479
   606
/** 
rosfran@479
   607
 * Gets the channel list.
rosfran@479
   608
 * 
rosfran@479
   609
 * @param backend_info The GMythBackendInfo instance.
rosfran@479
   610
 * 
rosfran@479
   611
 * @return a pointer to a GList with all the channels.
rosfran@479
   612
 */
melunko@934
   613
GList*
renatofilho@750
   614
gmyth_util_get_channel_list(GMythBackendInfo * backend_info)
rosfran@479
   615
{
renatofilho@754
   616
    GMythRecorder  *recorder;
renatofilho@754
   617
    GList          *channel_list = NULL;
renatofilho@754
   618
    gboolean        res = FALSE;
rosfran@698
   619
renatofilho@754
   620
    gmyth_debug("Gets channel list.");
rosfran@479
   621
renatofilho@754
   622
    g_return_val_if_fail(backend_info != NULL, FALSE);
rosfran@479
   623
renatofilho@754
   624
    recorder =
renatofilho@754
   625
        gmyth_recorder_new(1,
renatofilho@754
   626
                           g_string_new(gmyth_backend_info_get_hostname
renatofilho@754
   627
                                        (backend_info)),
renatofilho@754
   628
                           gmyth_backend_info_get_port(backend_info));
renatofilho@754
   629
    res = gmyth_recorder_setup(recorder);
rosfran@479
   630
renatofilho@754
   631
    if (res == TRUE) {
renatofilho@754
   632
        // GList* channel_list = gmyth_recorder_get_channel_list( recorder 
renatofilho@754
   633
        // 
renatofilho@754
   634
        // 
renatofilho@754
   635
        // ); 
renatofilho@754
   636
        gmyth_debug("Yeah, got channel list!!!");
renatofilho@754
   637
        GList          *ch = NULL;
renatofilho@754
   638
        GMythChannelInfo *channel_info = NULL;
rosfran@698
   639
renatofilho@754
   640
        for (ch = gmyth_recorder_get_channel_list(recorder); ch != NULL;) {
renatofilho@754
   641
            channel_info = g_malloc0(sizeof(GMythChannelInfo));
renatofilho@754
   642
            channel_info->channel_ID = 0;
renatofilho@754
   643
            channel_info->channel_num =
renatofilho@754
   644
                g_string_new(g_strdup((gchar *) ch->data));
renatofilho@754
   645
            channel_info->channel_name = g_string_new("");
renatofilho@754
   646
            gmyth_debug("Printing channel info... (%s)",
renatofilho@754
   647
                        channel_info->channel_num->str);
renatofilho@754
   648
            channel_list =
renatofilho@754
   649
                g_list_append(channel_list,
renatofilho@754
   650
                              g_memdup(channel_info,
renatofilho@754
   651
                                       sizeof(GMythChannelInfo)));
rosfran@698
   652
renatofilho@754
   653
            ch = g_list_next(ch);
rosfran@698
   654
renatofilho@754
   655
            if (channel_info != NULL)
renatofilho@754
   656
                g_free(channel_info);
renatofilho@754
   657
        }
rosfran@480
   658
renatofilho@754
   659
    } /* if */
renatofilho@754
   660
    else {
renatofilho@754
   661
        gmyth_debug("No, couldn't get the channel list!!!");
renatofilho@754
   662
    }
rosfran@698
   663
renatofilho@754
   664
    gmyth_debug("Got %d channels!!!", g_list_length(channel_list));
rosfran@698
   665
rosfran@698
   666
renatofilho@754
   667
    g_object_unref(recorder);
rosfran@698
   668
renatofilho@754
   669
    return channel_list;
rosfran@479
   670
}
rosfran@479
   671
rosfran@568
   672
/** 
rosfran@568
   673
 * Gets all the recordings from remote encoder.
rosfran@568
   674
 * 
rosfran@568
   675
 * @param backend_info The GMythBackendInfo instance.
rosfran@568
   676
 * 
rosfran@568
   677
 * @return The program info's listage.
rosfran@568
   678
 */
renatofilho@754
   679
GSList         *
renatofilho@750
   680
gmyth_util_get_all_recordings(GMythBackendInfo * backend_info)
rosfran@568
   681
{
renatofilho@754
   682
    GSList         *program_list = NULL;
renatofilho@754
   683
    GMythSocket    *socket;
renatofilho@754
   684
    gboolean        res;
rosfran@698
   685
renatofilho@754
   686
    socket = gmyth_socket_new();
renatofilho@754
   687
    res = gmyth_socket_connect_to_backend(socket, backend_info->hostname,
renatofilho@754
   688
                                          backend_info->port, TRUE);
rosfran@568
   689
renatofilho@754
   690
    if (res == TRUE) {
renatofilho@754
   691
        GMythStringList *slist = gmyth_string_list_new();
renatofilho@754
   692
        guint           pos = 0;
rosfran@698
   693
renatofilho@754
   694
        gmyth_string_list_append_char_array(slist,
renatofilho@754
   695
                                            "QUERY_RECORDINGS Play");
rosfran@568
   696
renatofilho@754
   697
        gmyth_socket_sendreceive_stringlist(socket, slist);
rosfran@568
   698
renatofilho@754
   699
        if (slist != NULL && (gmyth_string_list_length(slist) > 0)) {
renatofilho@754
   700
            GMythProgramInfo *program = NULL;
rosfran@698
   701
renatofilho@754
   702
            gmyth_debug("OK! Got the program list [size=%d].",
renatofilho@754
   703
                        gmyth_string_list_length(slist));
rosfran@698
   704
renatofilho@754
   705
            do {
renatofilho@754
   706
                program =
renatofilho@754
   707
                    gmyth_program_info_from_string_list_from_pos(slist,
renatofilho@754
   708
                                                                 pos);
rosfran@698
   709
renatofilho@754
   710
                if (program != NULL) {
renatofilho@754
   711
                    pos += 41;
rosfran@698
   712
renatofilho@754
   713
                    program_list = g_slist_append(program_list, program);
renatofilho@754
   714
                } else
renatofilho@754
   715
                    break;
rosfran@698
   716
renatofilho@754
   717
            }
renatofilho@754
   718
            while (gmyth_string_list_length(slist) > pos);
rosfran@698
   719
renatofilho@754
   720
        }
renatofilho@754
   721
        /*
renatofilho@754
   722
         * if 
renatofilho@754
   723
         */
renatofilho@754
   724
        g_object_unref(slist);
rosfran@568
   725
renatofilho@754
   726
        gmyth_socket_close_connection(socket);
renatofilho@754
   727
    }
renatofilho@754
   728
    g_object_unref(socket);
rosfran@698
   729
renatofilho@754
   730
    return program_list;
rosfran@568
   731
}
rosfran@568
   732
rosfran@568
   733
/** 
rosfran@568
   734
 * Checks if the given remote file exists, and gets its remote directory.
rosfran@568
   735
 * 
rosfran@568
   736
 * @param backend_info The GMythBackendInfo instance.
rosfran@568
   737
 * @param channel The channel name of the program info.
rosfran@568
   738
 * 
rosfran@568
   739
 * @return The requested program info.
rosfran@568
   740
 */
rosfran@568
   741
GMythProgramInfo *
renatofilho@750
   742
gmyth_util_get_recording_from_channel(GMythBackendInfo * backend_info,
renatofilho@754
   743
                                      const gchar * channel)
rosfran@568
   744
{
renatofilho@754
   745
    GSList         *program_list = NULL;
renatofilho@754
   746
    GMythProgramInfo *program = NULL;
rosfran@568
   747
renatofilho@754
   748
    program_list = gmyth_util_get_all_recordings(backend_info);
rosfran@698
   749
renatofilho@754
   750
    if (program_list != NULL && g_slist_length(program_list) > 0) {
renatofilho@754
   751
        GMythProgramInfo *program = NULL;
renatofilho@754
   752
        guint           pos = 0;
rosfran@698
   753
renatofilho@754
   754
        gmyth_debug("OK! Got the program list [size=%d].",
renatofilho@754
   755
                    g_slist_length(program_list));
rosfran@698
   756
renatofilho@754
   757
        while (pos < g_slist_length(program_list)) {
renatofilho@754
   758
            program =
renatofilho@754
   759
                (GMythProgramInfo *) g_slist_nth_data(program_list, pos);
rosfran@698
   760
renatofilho@754
   761
            if (program != NULL && program->channame != NULL &&
renatofilho@754
   762
                g_ascii_strncasecmp(program->channame->str, channel,
renatofilho@754
   763
                                    strlen(channel)) == 0) {
renatofilho@754
   764
                break;
renatofilho@754
   765
            }
rosfran@698
   766
renatofilho@754
   767
            ++pos;
rosfran@698
   768
renatofilho@754
   769
        }                       /* while */
rosfran@698
   770
renatofilho@754
   771
    }
renatofilho@754
   772
    /*
renatofilho@754
   773
     * if 
renatofilho@754
   774
     */
renatofilho@754
   775
    return program;
rosfran@568
   776
}
rosfran@568
   777
ali@951
   778
GSList *
ali@951
   779
gmyth_util_get_all_pending(GMythBackendInfo * backend_info,
ali@951
   780
                           gboolean * conflicts)
ali@951
   781
{
ali@951
   782
    GSList         *program_list = NULL;
ali@951
   783
    GMythSocket    *socket;
ali@951
   784
    gboolean        res;
ali@951
   785
ali@951
   786
    socket = gmyth_socket_new();
ali@951
   787
    res = gmyth_socket_connect_to_backend(socket, backend_info->hostname,
ali@951
   788
                                          backend_info->port, TRUE);
ali@951
   789
ali@951
   790
    if (res) {
ali@951
   791
        GMythStringList *slist = gmyth_string_list_new();
ali@951
   792
        int              pos, lines, count;
ali@951
   793
ali@951
   794
        gmyth_string_list_append_char_array(slist, "QUERY_GETALLPENDING");
ali@951
   795
ali@951
   796
        gmyth_socket_sendreceive_stringlist(socket, slist);
ali@951
   797
ali@951
   798
        if (gmyth_string_list_length(slist) > 1) {
ali@951
   799
            GMythProgramInfo *program;
ali@951
   800
ali@951
   801
            *conflicts = gmyth_string_list_get_int(slist, 0);
ali@951
   802
            count = gmyth_string_list_get_int(slist, 1);
ali@951
   803
            lines = (gmyth_string_list_length(slist) - 2) / count;
ali@951
   804
ali@951
   805
            for(pos = 2; pos < 2 + count * lines; pos += lines) {
ali@951
   806
                program =
ali@951
   807
                  gmyth_program_info_from_string_list_from_pos(slist, pos);
ali@951
   808
ali@951
   809
                if (program)
ali@951
   810
                    program_list = g_slist_prepend(program_list, program);
ali@951
   811
            }
ali@951
   812
        }
ali@951
   813
        g_object_unref(slist);
ali@951
   814
ali@951
   815
        gmyth_socket_close_connection(socket);
ali@951
   816
    }
ali@951
   817
    g_object_unref(socket);
ali@951
   818
ali@951
   819
    return g_slist_reverse(program_list);
ali@951
   820
}
ali@951
   821
renatofilho@222
   822
#if !GLIB_CHECK_VERSION (2, 10, 0)
renatofilho@222
   823
renatofilho@754
   824
/*
renatofilho@754
   825
 * Hacked from glib 2.10 <gtime.c> 
renatofilho@754
   826
 */
renatofilho@222
   827
renatofilho@754
   828
static          time_t
renatofilho@750
   829
mktime_utc(struct tm *tm)
renatofilho@222
   830
{
renatofilho@754
   831
    time_t          retval;
rosfran@698
   832
renatofilho@222
   833
#ifndef HAVE_TIMEGM
renatofilho@754
   834
    static const gint days_before[] = {
renatofilho@754
   835
        0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
renatofilho@754
   836
    };
renatofilho@222
   837
#endif
renatofilho@222
   838
renatofilho@222
   839
#ifndef HAVE_TIMEGM
renatofilho@754
   840
    if (tm->tm_mon < 0 || tm->tm_mon > 11)
renatofilho@754
   841
        return (time_t) - 1;
renatofilho@222
   842
renatofilho@754
   843
    retval = (tm->tm_year - 70) * 365;
renatofilho@754
   844
    retval += (tm->tm_year - 68) / 4;
renatofilho@754
   845
    retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
rosfran@698
   846
renatofilho@754
   847
    if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
renatofilho@754
   848
        retval -= 1;
rosfran@698
   849
renatofilho@754
   850
    retval =
renatofilho@754
   851
        ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 +
renatofilho@754
   852
        tm->tm_sec;
renatofilho@222
   853
#else
renatofilho@754
   854
    retval = timegm(tm);
renatofilho@754
   855
#endif                          /* !HAVE_TIMEGM */
rosfran@698
   856
renatofilho@754
   857
    return retval;
renatofilho@222
   858
}
renatofilho@222
   859
renatofilho@222
   860
gboolean
renatofilho@750
   861
g_time_val_from_iso8601(const gchar * iso_date, GTimeVal * time_)
renatofilho@222
   862
{
renatofilho@754
   863
    struct tm       tm;
renatofilho@754
   864
    long            val;
renatofilho@222
   865
renatofilho@754
   866
    g_return_val_if_fail(iso_date != NULL, FALSE);
renatofilho@754
   867
    g_return_val_if_fail(time_ != NULL, FALSE);
renatofilho@222
   868
renatofilho@754
   869
    val = strtoul(iso_date, (char **) &iso_date, 10);
renatofilho@754
   870
    if (*iso_date == '-') {
renatofilho@754
   871
        /*
renatofilho@754
   872
         * YYYY-MM-DD 
renatofilho@754
   873
         */
renatofilho@754
   874
        tm.tm_year = val - 1900;
renatofilho@754
   875
        iso_date++;
renatofilho@754
   876
        tm.tm_mon = strtoul(iso_date, (char **) &iso_date, 10) - 1;
rosfran@698
   877
renatofilho@754
   878
        if (*iso_date++ != '-')
renatofilho@754
   879
            return FALSE;
rosfran@698
   880
renatofilho@754
   881
        tm.tm_mday = strtoul(iso_date, (char **) &iso_date, 10);
renatofilho@754
   882
    } else {
renatofilho@754
   883
        /*
renatofilho@754
   884
         * YYYYMMDD 
renatofilho@754
   885
         */
renatofilho@754
   886
        tm.tm_mday = val % 100;
renatofilho@754
   887
        tm.tm_mon = (val % 10000) / 100 - 1;
renatofilho@754
   888
        tm.tm_year = val / 10000 - 1900;
renatofilho@754
   889
    }
renatofilho@222
   890
renatofilho@754
   891
    if (*iso_date++ != 'T')
renatofilho@754
   892
        return FALSE;
rosfran@698
   893
renatofilho@754
   894
    val = strtoul(iso_date, (char **) &iso_date, 10);
renatofilho@754
   895
    if (*iso_date == ':') {
renatofilho@754
   896
        /*
renatofilho@754
   897
         * hh:mm:ss 
renatofilho@754
   898
         */
renatofilho@754
   899
        tm.tm_hour = val;
renatofilho@754
   900
        iso_date++;
renatofilho@754
   901
        tm.tm_min = strtoul(iso_date, (char **) &iso_date, 10);
rosfran@698
   902
renatofilho@754
   903
        if (*iso_date++ != ':')
renatofilho@754
   904
            return FALSE;
rosfran@698
   905
renatofilho@754
   906
        tm.tm_sec = strtoul(iso_date, (char **) &iso_date, 10);
renatofilho@754
   907
    } else {
renatofilho@754
   908
        /*
renatofilho@754
   909
         * hhmmss 
renatofilho@754
   910
         */
renatofilho@754
   911
        tm.tm_sec = val % 100;
renatofilho@754
   912
        tm.tm_min = (val % 10000) / 100;
renatofilho@754
   913
        tm.tm_hour = val / 10000;
renatofilho@754
   914
    }
renatofilho@222
   915
renatofilho@754
   916
    time_->tv_sec = mktime_utc(&tm);
renatofilho@754
   917
    time_->tv_usec = 1;
renatofilho@222
   918
renatofilho@754
   919
    if (*iso_date == '.')
renatofilho@754
   920
        time_->tv_usec = strtoul(iso_date + 1, (char **) &iso_date, 10);
rosfran@698
   921
renatofilho@754
   922
    if (*iso_date == '+' || *iso_date == '-') {
renatofilho@754
   923
        gint            sign = (*iso_date == '+') ? -1 : 1;
rosfran@698
   924
renatofilho@754
   925
        val = 60 * strtoul(iso_date + 1, (char **) &iso_date, 10);
rosfran@698
   926
renatofilho@754
   927
        if (*iso_date == ':')
renatofilho@754
   928
            val = 60 * val + strtoul(iso_date + 1, NULL, 10);
renatofilho@754
   929
        else
renatofilho@754
   930
            val = 60 * (val / 100) + (val % 100);
rosfran@698
   931
renatofilho@754
   932
        time_->tv_sec += (time_t) (val * sign);
renatofilho@754
   933
    }
renatofilho@222
   934
renatofilho@754
   935
    return TRUE;
renatofilho@222
   936
}
renatofilho@222
   937
renatofilho@222
   938
renatofilho@754
   939
gchar          *
renatofilho@750
   940
g_time_val_to_iso8601(GTimeVal * time_)
renatofilho@222
   941
{
renatofilho@754
   942
    gchar          *retval;
renatofilho@222
   943
renatofilho@754
   944
    g_return_val_if_fail(time_->tv_usec >= 0
renatofilho@754
   945
                         && time_->tv_usec < G_USEC_PER_SEC, NULL);
renatofilho@222
   946
renatofilho@222
   947
#define ISO_8601_LEN    21
renatofilho@222
   948
#define ISO_8601_FORMAT "%Y-%m-%dT%H:%M:%SZ"
renatofilho@754
   949
    retval = g_new0(gchar, ISO_8601_LEN + 1);
rosfran@698
   950
renatofilho@754
   951
    strftime(retval, ISO_8601_LEN, ISO_8601_FORMAT,
renatofilho@754
   952
             gmtime(&(time_->tv_sec)));
rosfran@698
   953
renatofilho@754
   954
    return retval;
renatofilho@222
   955
}
renatofilho@222
   956
renatofilho@222
   957
renatofilho@754
   958
/*
renatofilho@754
   959
 * Hacked from glib 2.10 <gdate.c> 
renatofilho@754
   960
 */
renatofilho@222
   961
rosfran@698
   962
void
renatofilho@750
   963
g_date_set_time_t(GDate * date, time_t timet)
renatofilho@222
   964
{
renatofilho@754
   965
    struct tm       tm;
rosfran@698
   966
renatofilho@754
   967
    g_return_if_fail(date != NULL);
rosfran@698
   968
renatofilho@222
   969
#ifdef HAVE_LOCALTIME_R
renatofilho@754
   970
    localtime_r(&timet, &tm);
renatofilho@222
   971
#else
renatofilho@754
   972
    {
renatofilho@754
   973
        struct tm      *ptm = localtime(&timet);
renatofilho@222
   974
renatofilho@754
   975
        if (ptm == NULL) {
renatofilho@754
   976
            /*
renatofilho@754
   977
             * Happens at least in Microsoft's C library if you pass a
renatofilho@754
   978
             * negative time_t. Use 2000-01-01 as default date. 
renatofilho@754
   979
             */
renatofilho@222
   980
#ifndef G_DISABLE_CHECKS
renatofilho@754
   981
            g_return_if_fail_warning(G_LOG_DOMAIN, "g_date_set_time",
renatofilho@754
   982
                                     "ptm != NULL");
renatofilho@222
   983
#endif
renatofilho@222
   984
renatofilho@754
   985
            tm.tm_mon = 0;
renatofilho@754
   986
            tm.tm_mday = 1;
renatofilho@754
   987
            tm.tm_year = 100;
renatofilho@754
   988
        } else
renatofilho@754
   989
            memcpy((void *) &tm, (void *) ptm, sizeof(struct tm));
renatofilho@754
   990
    }
renatofilho@222
   991
#endif
rosfran@698
   992
renatofilho@754
   993
    date->julian = FALSE;
rosfran@698
   994
renatofilho@754
   995
    date->month = tm.tm_mon + 1;
renatofilho@754
   996
    date->day = tm.tm_mday;
renatofilho@754
   997
    date->year = tm.tm_year + 1900;
rosfran@698
   998
renatofilho@754
   999
    g_return_if_fail(g_date_valid_dmy(date->day, date->month, date->year));
rosfran@698
  1000
renatofilho@754
  1001
    date->dmy = TRUE;
renatofilho@222
  1002
}
renatofilho@222
  1003
renatofilho@222
  1004
renatofilho@222
  1005
void
renatofilho@750
  1006
g_date_set_time_val(GDate * date, GTimeVal * timeval)
renatofilho@222
  1007
{
renatofilho@754
  1008
    g_date_set_time_t(date, (time_t) timeval->tv_sec);
renatofilho@222
  1009
}
renatofilho@222
  1010
renatofilho@222
  1011
renatofilho@222
  1012
renatofilho@222
  1013
renatofilho@222
  1014
#endif