summaryrefslogtreecommitdiffstats
path: root/examples/statemachine/citizenquartz/timeperiod.h
blob: c5a3a16d95c9810fe5202ddfc4e55ea07f98bff1 (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
77
78
79
80
81
82
83
84
#ifndef TIMEPERIOD_H
#define TIMEPERIOD_H

#include <QDateTime>
#include <QTime>
#include <QDebug>

class TimePeriod
{
public:
    TimePeriod() : m_seconds(0), m_minutes(0), m_hours(0), m_days(0), m_months(0), m_years(0)
    {
    }

    TimePeriod &setSeconds(int seconds) { m_seconds = seconds; return *this; }
    int seconds() const { return m_seconds; }

    TimePeriod &setMinutes(int minutes) { m_minutes = minutes; return *this; }
    int minutes() const { return m_minutes; }

    TimePeriod &setHours(int hours) { m_hours = hours; return *this; }
    int hours() const { return m_hours; }

    TimePeriod &setDays(int days) { m_days = days; return *this; }
    int days() const { return m_days; }

    TimePeriod &setMonths(int months) { m_months = months; return *this; }
    int months() const { return m_months; }

    TimePeriod &setYears(int years) { m_years = years; return *this; }
    int years() const { return m_years; }

    operator QVariant() const 
    {
        QVariant v;
        qVariantSetValue<TimePeriod>(v, *this);
        return v;
    }

private:
    int m_seconds;
    int m_minutes;
    int m_hours;
    int m_days;
    int m_months;
    int m_years;
};

inline void operator+=(QDateTime &dateTime, const TimePeriod &timePeriod)
{
    dateTime = dateTime.addSecs(timePeriod.seconds());
    dateTime = dateTime.addSecs(timePeriod.minutes() * 60);
    dateTime = dateTime.addSecs(timePeriod.hours() * 3600);
    dateTime = dateTime.addDays(timePeriod.days());
    dateTime = dateTime.addMonths(timePeriod.months());
    dateTime = dateTime.addYears(timePeriod.years());
}

inline QDateTime operator+(const QDateTime &dateTime, const TimePeriod &timePeriod)
{
    QDateTime result(dateTime);
    result += timePeriod;

    return result;
}

inline void operator+=(QTime &time, const TimePeriod &timePeriod)
{
    time = time.addSecs(timePeriod.seconds());
    time = time.addSecs(timePeriod.minutes() * 60);
    time = time.addSecs(timePeriod.hours() * 3600);    
}

inline QTime operator+(const QTime &time, const TimePeriod &timePeriod)
{
    QTime result(time);
    result += timePeriod;

    return result;
}

Q_DECLARE_METATYPE(TimePeriod)

#endif \\ TIMEPERIOD_H