aboutsummaryrefslogtreecommitdiff
path: root/libcontextsubscriber/src/duration.cpp
blob: 0cd618e21f612448852401c2c1f9865ea236fa6b (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * Copyright (C) 2008,2009 Nokia Corporation.
 *
 * Contact: Marius Vollmer <marius.vollmer@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * 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
 * 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 St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <QRegExp>
#include <QTime>
#include <QDate>
#include <QStringList>

#include "duration.h"

/*!
  \class Duration

  \brief The Duration class represents a period of elapsed time between two events.

  A Duration object is created either by giving the number of nanoseconds explicitly or a string representation of a Duration.

  The Duration object assumes the following definitions:

  A year   = 52 weeks and 1 day OR 365 days

  A week   = 7 days

  A day    = 24 hours

  A hour   = 60 minutes

  A minute = 60 seconds

  A second = 10e9 nanoseconds

*/

///Convenient time conversions
const qint64 Duration::NANOSECS_PER_MSEC;
const qint64 Duration::NANOSECS_PER_SEC;
const qint64 Duration::NANOSECS_PER_MIN;
const qint64 Duration::NANOSECS_PER_HOUR;
const qint64 Duration::NANOSECS_PER_DAY;
const qint64 Duration::NANOSECS_PER_WEEK;
const qint64 Duration::MSECS_PER_DAY;
const qint64 Duration::SECS_PER_DAY;
const qint64 Duration::DAYS_PER_YEAR;
const qint64 Duration::DAYS_PER_WEEK;

/// Constructs a Duration with an initial value of 0
Duration::Duration()
{
    totalNanoSecs_p = 0;
}

/// Constructs a Duration with the given number of nanoseconds
Duration::Duration(quint64 nanoSecs) : totalNanoSecs_p(nanoSecs)
{
    QTime reftime = QTime().addMSecs((nanoSecs/NANOSECS_PER_MSEC)% MSECS_PER_DAY);
    quint64 days = nanoSecs/NANOSECS_PER_DAY;

    nanoSecs_p = totalNanoSecs_p%(NANOSECS_PER_SEC);
    seconds_p = reftime.second();
    minutes_p = reftime.minute();
    hours_p = reftime.hour();
    days_p = (days%DAYS_PER_YEAR)%DAYS_PER_WEEK;
    weeks_p = (days%DAYS_PER_YEAR)/DAYS_PER_WEEK;
    years_p = days/DAYS_PER_YEAR;
}

/// Constructs a copy of the other Duration.
Duration::Duration(const QString &duration)
{
    QRegExp re("(?:(\\d+)Y)?\\s*(?:(\\d+)W)?\\s*(?:(\\d+)D)?\\s*(?:(\\d+)H)?\\s*(?:(\\d+)M)?\\s*(?:(\\d+)S)?");
    re.setCaseSensitivity(Qt::CaseInsensitive);

    if (re.exactMatch(duration.simplified()) && re.matchedLength()){
        QStringList list;
        list = re.capturedTexts();
        list.removeFirst();

        qint64 nanosec_year = list.at(0).toLongLong()*(52*NANOSECS_PER_WEEK+NANOSECS_PER_DAY);
        qint64 nanosec_week = list.at(1).toLongLong()*NANOSECS_PER_WEEK;
        qint64 nanosec_day = list.at(2).toLongLong()*NANOSECS_PER_DAY;
        qint64 nanosec_hour = list.at(3).toLongLong()*NANOSECS_PER_HOUR;
        qint64 nanosec_min = list.at(4).toLongLong()*NANOSECS_PER_MIN;
        qint64 nanosec_sec = list.at(5).toLongLong()*NANOSECS_PER_SEC;
        *this = Duration(nanosec_year+nanosec_week+nanosec_day+nanosec_hour+nanosec_min+nanosec_sec);
    } else {
        totalNanoSecs_p = 0;
    }
}
/// Returns true if this Duration is equal to the other Duration
bool Duration::operator==(const Duration &other) const {
    return (totalNanoSecs_p == other.totalNanoSecs_p);
}

/// Returns the number of nanoseconds in the duration
int Duration::nanoSecs() const
{
    return totalNanoSecs_p%(NANOSECS_PER_SEC);
}

/// Returns the number of seconds in the duration
int Duration::seconds() const
{
    return seconds_p;
}

/// Returns the number of minutes in the duration
int Duration::minutes() const
{
    return minutes_p;
}

/// Returns the number of hours in the duration
int Duration::hours() const
{
    return hours_p;
}

/// Returns the number of days in the duration
int Duration::days() const
{
    return days_p;
}

/// Returns the number of weeks in the duration
int Duration::weeks() const
{
    return weeks_p;
}

/// Returns the number of years in the duration
int Duration::years() const
{
    return years_p;
}
/// Returns a string representation of the duration
/// using the following format: Y W D H M S
QString Duration::toString() const
{
    QString retval;
    if(years_p) {
        retval.append(QString::number(years_p));
        retval.append(QString("Y "));
    }

    if(weeks_p) {
        retval.append(QString::number(weeks_p));
        retval.append(QString("W "));
    }

    if(days_p) {
        retval.append(QString::number(days_p));
        retval.append(QString("D "));
    }

    if(hours_p) {
        retval.append(QString::number(hours_p));
        retval.append(QString("H "));
    }

    if(minutes_p) {
        retval.append(QString::number(minutes_p));
        retval.append(QString("M "));
    }

    if(seconds_p) {
        retval.append(QString::number(seconds_p));
        retval.append(QString("S"));
    } else if (!years_p && !weeks_p && !days_p && !hours_p && !minutes_p && !seconds_p) {
        retval.append(QString("0S"));
    }
    return retval.simplified();
}

/// Returns the Duration expressed in nanoseconds
quint64 Duration::toNanoSeconds() const
{
    return totalNanoSecs_p;
}

/// Returns true if the string format used to represent the Duration is valid i.e. Y W D H M S , each being optional
bool Duration::isDuration(const QString &duration)
{
    QRegExp re("(?:(\\d+)Y)?\\s*(?:(\\d+)W)?\\s*(?:(\\d+)D)?\\s*(?:(\\d+)H)?\\s*(?:(\\d+)M)?\\s*(?:(\\d+)S)?");
    re.setCaseSensitivity(Qt::CaseInsensitive);
    return re.exactMatch(duration.simplified()) && re.matchedLength();
}