summaryrefslogtreecommitdiff
path: root/libgupnp-av/time-utils.c
blob: a143d02dc30b0043cc502e4b76d3c44dd195b04e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
 * Copyright (C) 2012 Intel Corporation.
 *
 * Authors: Jens Georg <jensg@openismus.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 */

#include <config.h>

#include <glib.h>

#include "time-utils.h"

#define SEC_PER_MIN 60
#define SEC_PER_HOUR 3600

long
seconds_from_time (const char *time_str)
{
        char **tokens;
        gdouble seconds = -1;

        if (time_str == NULL)
                return -1;

        tokens = g_strsplit (time_str, ":", -1);
        if (tokens[0] == NULL ||
            tokens[1] == NULL ||
            tokens[2] == NULL)
                goto return_point;

        seconds = g_strtod (tokens[2], NULL);
        seconds += g_strtod (tokens[1], NULL) * SEC_PER_MIN;
        seconds += g_strtod (tokens[0], NULL) * SEC_PER_HOUR;

return_point:
        g_strfreev (tokens);

        return (long) seconds;
}

char *
seconds_to_time (long seconds)
{
        char *str;

        if (seconds < 0)
                return NULL;

        str = g_strdup_printf ("%ld:%.2ld:%.2ld.000",
                               seconds / (60 * 60),
                               (seconds / 60) % 60,
                               seconds % 60);

        return str;
}