summaryrefslogtreecommitdiff
path: root/src/librygel-renderer/rygel-time-utils.vala
blob: 871358d491cf3ef6ab25dfab8cfc2c7b59c96639 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * Copyright (C) 2009 Nokia Corporation.
 * Copyright (C) 2012 Intel Corporation.
 *
 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 *                               <zeeshan.ali@nokia.com>
 *         Jens Georg <jensg@openismus.com>
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Rygel is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

internal abstract class Rygel.TimeUtils {
    public static int64 time_from_string (string str) {
        uint64 hours, minutes, seconds, msecs;
        string time_str = str;
        int sign = 1;

        switch (str[0]) {
            case '-':
                sign = -1;
                time_str = str.substring (1);

                break;
            case '+':
                time_str = str.substring (1);

                break;
            default:
                break;
        }

        time_str.scanf ("%llu:%2llu:%2llu.%3llu",
                        out hours,
                        out minutes,
                        out seconds,
                        out msecs);

        return sign * ((int64)(hours * 3600 + minutes * 60 + seconds) *
               TimeSpan.SECOND + (int64)msecs * TimeSpan.MILLISECOND);
    }

    public static string time_to_string (int64 time) {
        uint64 hours, minutes, seconds, totsecs, msecs;
        string sign = "";

        if (time < 0) {
            sign = "-";
            time = -time;
        };

        hours   = time / TimeSpan.SECOND / 3600;
        seconds = time / TimeSpan.SECOND % 3600;
        minutes = seconds / 60;
        seconds = seconds % 60;

        totsecs = (hours * 3600 + minutes * 60 + seconds) * TimeSpan.SECOND;
        msecs = (time - totsecs) / TimeSpan.MILLISECOND;

        return "%s%llu:%.2llu:%.2llu.%.3llu".printf (sign, hours, minutes,
                                                     seconds, msecs);
    }
}