timeutil.h 3.24 KB
Newer Older
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
/*
    KSysGuard, the KDE System Guard

    Copyright (C) 2014 Gregor Mi <codestruct@posteo.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

*/

#ifndef TIMEUTIL_H
#define TIMEUTIL_H

26
#include <cmath> // floor
27
28
29
30
31

#ifdef Q_OS_OSX
#include <mach/clock.h>
#include <mach/mach.h>
#else
32
#include <time.h>
33
#endif
34
35
36
37
38
39
40
41
42
43
44
45
46

#include <QDateTime>

#include <klocalizedstring.h> // KF5::I18n

class TimeUtil
{
public:
    /**
     * @Returns the amount of seconds passed since the system was booted
     */
    static long systemUptimeSeconds()
    {
47
48
49
50
51
52
53
54
#ifdef Q_OS_OSX
        clock_serv_t cclock;
        mach_timespec_t tp;

        host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
        clock_get_time(cclock, &tp);
        mach_port_deallocate(mach_task_self(), cclock);
#else
55
        timespec tp;
56
        int isSuccess = clock_gettime(CLOCK_MONOTONIC, &tp); // see https://stackoverflow.com/questions/8357073/get-uptime-in-seconds-or-miliseconds-on-unix-like-systems
57
        Q_ASSERT(isSuccess == 0);
58
#endif
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
        return tp.tv_sec;
    }

    /**
     * @Returns the point in time when the system was booted
     */
    static QDateTime systemUptimeAbsolute()
    {
        auto now = QDateTime::currentDateTime();
        return now.addSecs(-systemUptimeSeconds());
    }

    /**
     * Converts the given @param seconds into a human readable string.
     * It represents an elapsed time span, e.g. "3m 50s ago".
     */
    static QString secondsToHumanElapsedString(long seconds)
    {
        const int s_abs = seconds;
        const int m_abs = floor(seconds / 60.0);
        const int h_abs = floor(seconds / 60.0 / 60.0);
        const int d_abs = floor(seconds / 60.0 / 60.0 / 24.0);

        if (m_abs == 0) {
            return i18nc("contains a abbreviated time unit: (s)econds", "%1s ago", s_abs);
        } else if (h_abs == 0) {
            const int s = s_abs - m_abs * 60;
            return i18nc("contains abbreviated time units: (m)inutes and (s)econds", "%1m %2s ago", m_abs, s);
        } else if (d_abs == 0) {
            const int s = s_abs - m_abs * 60;
            const int m = m_abs - h_abs * 60;
            return i18nc("contains abbreviated time units: (h)ours, (m)inutes and (s)econds)", "%1h %2m %3s ago", h_abs, m, s);
        } else { // d_abs > 0
            const int m = m_abs - h_abs * 60;
            const int h = h_abs - d_abs * 24;
            return i18ncp("contains also abbreviated time units: (h)ours and (m)inutes",
                          "%1 day %2h %3m ago", "%1 days %2h %3m ago", d_abs, h, m);
        }
    }
};

#endif